字符串类问题.. [英] string class question..

查看:41
本文介绍了字符串类问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构


typedef struct

{

string a;

string b;

} atype;


string ABC =(" 123");

string DEF =(" 456" ;);

atype mylist [] = {

" ABC",ABC

" DEF",DEF

};


在gcc中运行良好,但是mylist无法在vc ++中编译。

错误信息类似......

structs.h(171):错误C2440:''初始化'':无法转换为''类

std :: basic_string< char,struct std :: char_traits< char>, class

std :: allocator< char> >''到''atype''

I have a structure

typedef struct
{
string a;
string b;
} atype;

string ABC = ("123");
string DEF = ("456");
atype mylist[] = {
"ABC", ABC
"DEF", DEF
};

Works well in gcc but mylist fails to compile in vc++.
Error message is something like....
structs.h(171) : error C2440: ''initializing'' : cannot convert from ''class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >'' to ''atype''

推荐答案

" JustSomeGuy" <无** @ nottelling.com>在留言中写道

news:br *********** @ nserve1.acs.ucalgary.ca ...
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
我有一个结构

typedef struct
{
字符串a;
字符串b;
} atype;


假设" string"表示std :: string这是有效的C ++。但它肯定看起来像B一样美元。如果你想将两个字符串打包在一起,那么使用C ++中的类可能会更好。



string ABC =(" 123");


为什么括号? ITYM


string ABC =" 123";

string DEF =(" 456");
atype mylist [] = {
ABC,ABC


此行之后真的有逗号吗?

DEF,DEF
};

适用于gcc


确实如此?

但是mylist无法在vc ++中编译。


再次来自Redmond的那些歹徒!

错误信息类似......
structs.h(171):错误C2440 :''初始化'':无法转换为''类
std :: basic_string< char,struct std :: char_traits< char>,class
std :: allocator< char> >''到''atype''
I have a structure

typedef struct
{
string a;
string b;
} atype;
Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to
use a class in C++.

string ABC = ("123");
Why the parentheses? ITYM

string ABC = "123";
string DEF = ("456");
atype mylist[] = {
"ABC", ABC
Is there really a comma after this line?
"DEF", DEF
};

Works well in gcc
It does?
but mylist fails to compile in vc++.
Those miscreants from Redmond at it again!
Error message is something like....
structs.h(171) : error C2440: ''initializing'' : cannot convert from ''class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >'' to ''atype''




我不知道为什么这段代码适用于任何编译器。一个C ++解决方案

如下所示:


#include< string>


class atype

{

私人:

std :: string m_a;

std :: string m_b;

public:

atype(){}

atype(const std :: string& i_a,const std :: string& i_b):

m_a(i_a),m_b(i_b){}

const std :: string& a()const {return m_a;}

const std :: string& b()const {return m_b;}

};


atype mylist [] = {atype(" ABC"," ; 123",atype(" DEF"," 456")};


或更好


#include<向量>


std :: vector< atype> avec;

avec.push_back(atype(" ABC"," 123"));

avec.push_back(atype(" DEF"," 456") ;));


-

Cy
http://home.rochester.rr.com/cyhome/


" Cy Edmunds" < CE ****** @ spamless.rochester.rr.com>在消息新闻中写道:< op ******************** @ twister.nyroc.rr.com> ...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<op********************@twister.nyroc.rr.com> ...
" JustSomeGuy" <无** @ nottelling.com>在消息中写道
新闻:br *********** @ nserve1.acs.ucalgary.ca ...
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:br***********@nserve1.acs.ucalgary.ca...
我有一个结构

typedef struct
{
字符串a;
字符串b;
} atype;
假设" string"表示std :: string这是有效的C ++。但它肯定看起来像C.如果你想将两个字符串打包在一起,那么在C ++中使用一个类可能会更好。
I have a structure

typedef struct
{
string a;
string b;
} atype;
Assuming "string" means std::string this is valid C++. But it sure looks
like C. If you want to package two strings together it might be better to
use a class in C++.




为什么课程更好?除了默认访问说明符是

类和公共结构的默认访问说明符之外,C ++中的struct和

类没有区别。

我不知道为什么这段代码适用于任何编译器。一个C ++解决方案将会是这样的:

[剪断怪物]


为什么你认为这是一个更好的方法把字符串

放在一起?私有数据成员和两个

构造函数和getter的所有开销,当一个普通的结构可以正常工作。

更不用说你没有提供setter和你的只有getter

使用const引用,所以你的类甚至不提供与他的结构相同的

功能,因为你不能修改
你的版本。


struct atype {

std :: string a;

std :: string b;


atype(const std :: string& a,const std :: string& b):a(a),b(b){}

};


根据用途,这个小结构可能是完美的。

没有理由让它成为一个类与私人数据成员,除非

封装或数据隐藏是一个问题。上帝帮助你,如果你将b $ b私密化,然后为他们包括吸气剂和制定者。

或更好

#include< vector> ;

std :: vector< atype> avec;
avec.push_back(atype(" ABC"," 123"));
avec.push_back(atype(" DEF"," 456"));



Why is a class better? There is no difference between a struct and a
class in C++ aside from the default access specifier being private for
classes and public for structs.
I don''t know why this code works with any compiler. A C++ solution would
look like this:

[ snip the monstrosity ]
Why the heck do you think that''s a better way to put to strings
together? All that overhead with private data members and two
constructors and getters when a plain struct would work just fine.
Not to mention that you didn''t provide setters and your only getter
uses const references so your class doesn''t even provide the same
functionality as his struct because you can''t modify the a and b in
your version.

struct atype {
std::string a;
std::string b;

atype(const std::string &a, const std::string &b) : a(a), b(b) { }
};

Depending on the usage, this little struct may be just perfect.
There''s no reason to make it a class with private data members unless
encapsulation or data hiding is an issue. And god help you if you
make them private AND then include getters and setters for them.
or better yet

#include <vector>

std::vector<atype> avec;
avec.push_back(atype("ABC", "123"));
avec.push_back(atype("DEF", "456"));




为什么这个更好呢?是的,矢量可以调整大小。它还有
在内存和速度方面都有开销。如果他不需要

调整大小怎么办?你的方式还好吗?


有时间和超级OO模式的地方,但这可能

不一定是它。


Justin Dubs



Why is this "better yet?" Yeah, the vector is resizeable. It also
has overhead both in memory and speed. What if he didn''t need to
resize it. Is your way still better then?

There''s a time and a place for super-dooper OO patterns, but this may
not necessarily be it.

Justin Dubs




" JustSomeGuy" <无** @ nottelling.com>在消息新闻中写道:br *********** @ nserve1.acs.ucalgary.ca ...

"JustSomeGuy" <no**@nottelling.com> wrote in message news:br***********@nserve1.acs.ucalgary.ca...
在gcc中运行良好,但是mylist无法编译vc ++。
错误信息类似....
structs.h(171):错误C2440:''初始化'':无法转换为''类
std :: basic_string< char,struct std :: char_traits< char>,class
std :: allocator< char> >''到''atype''
Works well in gcc but mylist fails to compile in vc++.
Error message is something like....
structs.h(171) : error C2440: ''initializing'' : cannot convert from ''class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >'' to ''atype''




如果您定义了字符串,并且插入了缺少逗号的逗号,则可以正常使用

行结束

" ABC",ABC



Works fine for me provided you define string and you insert hte comma missing
at the end of the line
"ABC", ABC


这篇关于字符串类问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆