简单的toString()问题 [英] Simple toString() problem

查看:59
本文介绍了简单的toString()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




< code snippet>

char * Car :: toString()

{

//示例[XR,Holden]

char * str = {" [",carID,",",carName,"]"};

返回str;

}


注意:carID和carName是类Car的私有变量


编译错误:标量变量的Car.cpp初始化程序需要一个元素


基本上,toString()方法不起作用。任何帮助赞赏。注意,

我对使用较新的字符串数据类型不感兴趣,因为我想要

来学习使用指向char的指针。

问候

wewewe


Hi,

<code snippet>
char* Car::toString()
{
//Example [XR, Holden]
char* str = {"[", carID, ",", carName, "]"};
return str;
}

Note: carID and carName are private variables of class Car

Compile Error: Car.cpp initializer for scalar variable requires one element

Basically, the toString() method doesn''t work. Any help appreciated. Note,
I''m not interested in working with the newer string data type because I want
to learnto do it with pointers to char.
Regards
wewewe



推荐答案

Ying Yang写道:
Ying Yang wrote:


<代码片段>
char * Car :: toString()
{
//示例[XR,Holden]
char * str = {" [",carID,",",carName,"]"};
Hi,

<code snippet>
char* Car::toString()
{
//Example [XR, Holden]
char* str = {"[", carID, ",", carName, "]"};




我看到它的方式,你想要返回类似的东西:


" [carId,carName]"


with当然,carId和carName的真正价值。我假设carId和

carName也是char *。


好​​吧,

1){" [",carID ,,,carName,""}基本上是一个

的数组的语法(如果carId和carName也是char *,可能是char *)。在其他

字样中,如果我们称之为str,其内容为:

str [0] ==" {"

str [1] == carId

str [2] ==","

str [3] == carName

str [ 4] =="]"


无论如何,它的类型是char * [5]而不是char *,这就是为什么你得到

错误。你试图将一个数组分配给一个标量变量。


2)你怎么能用旧式的东西来做呢:

//首先你会需要一个缓冲区:

char * buf = new char [30]; //让我们说一个30个字符的缓冲区

//然后填充缓冲区

sprintf(buf," [%s,%s]",carId ,carName); // sprintf需要#include< cstdio>

返回buf;

请注意我用new分配了buf,否则它将被销毁为

很快toString()退出,返回的指针指向无处。


我认为你需要一本好的C ++书。请参阅常见问题解答。




The way I see it, you want to return something like:

"[carId,carName]"

with the real values of carId and carName, of course. I assume carId and
carName are also char*.

Well,
1) {"[", carID, ",", carName, "]"} is basically the syntax for an array of
stuff (presumably char* if carId and carName are also char*). In other
words, if we call it str, its contents are:
str[0] == "{"
str[1] == carId
str[2] == ","
str[3] == carName
str[4] == "]"

Anyway, its type is char*[5] not char*, which is why you are getting the
error. You are trying to assign an array to a scalar variable.

2) How you could do it with old-styled stuff:
//first you would need a buffer:
char * buf = new char[30]; //let''s say a 30 character buffer
//then fill the buffer
sprintf(buf,"[%s,%s]", carId, carName); //sprintf requires #include <cstdio>
return buf;
Notice that I allocated buf with new, otherwise it would be destroyed as
soon as toString() exits and the returned pointer would point to nowhere.

I think you need a good C++ book. See the FAQ.






< code snippet>
char * Car :: toString()
//示例[XR,Holden]
char * str = { " [",carID,",",carName,"]"};
返回str;
}
注意:carID和carName是私有的类Car的变量
使用ostringstream,并切换到使用std :: string作为返回
Hi,

<code snippet>
char* Car::toString()
{
//Example [XR, Holden]
char* str = {"[", carID, ",", carName, "]"};
return str;
}

Note: carID and carName are private variables of class Car
Use an ostringstream, and switch to using std::string for the return



的值。
ostringstream oss;
oss< ;< ''[''<< carID<< '',''<< carName<< ]; //就像使用cout
return oss.str();

如果你/必须/使用C风格的字符串,你将不得不解决动态问题
内存管理。不过,这是一个完整的讨论。

Josh


value.
ostringstream oss;
oss << ''['' << carID << '','' << carName << '']''; // just like using cout
return oss.str();

If you /must/ use C-style strings, you''re going to have to tackle dynamic
memory management. That''s a whole ''nother discussion, though.

Josh




我需要包含什么才能使用ostringstream oss?使用指向char的指针看起来不像是一个正常的方式。


wewewe



What do i have to include to use ostringstream oss?? doesn''t not seem like a
normal way of doing it with pointers to char.

wewewe

On Sun,07 Sep 2003 23:37:44 +0930,Ying Yang写道:
On Sun, 07 Sep 2003 23:37:44 +0930, Ying Yang wrote:
>
>
> < code snippet>
> char * Car :: toString()
> {
> //示例[XR,Holden]
> char * str = {" [",carID,",",carName,"]"};
> return str;
> }
>
>注意:carID和carName是类Car的私有变量
使用ostringstream,并切换到使用std :: string作为返回
> Hi,
>
> <code snippet>
> char* Car::toString()
> {
> //Example [XR, Holden]
> char* str = {"[", carID, ",", carName, "]"};
> return str;
> }
>
> Note: carID and carName are private variables of class Car
Use an ostringstream, and switch to using std::string for the return


值。


ostringstream oss;
oss<< ''[''<< carID<< '',''<< carName<< ]; //就像使用cout
return oss.str();

如果你/必须/使用C风格的字符串,你将不得不解决动态问题
内存管理。不过,这是一个完整的讨论。

Josh

ostringstream oss;
oss << ''['' << carID << '','' << carName << '']''; // just like using cout
return oss.str();

If you /must/ use C-style strings, you''re going to have to tackle dynamic
memory management. That''s a whole ''nother discussion, though.

Josh



我需要包含什么才能使用ostringstream oss ??



What do i have to include to use ostringstream oss??




< sstream>。

看起来并不像是指向char的正常方式。



<sstream>.
doesn''t not seem like a
normal way of doing it with pointers to char.




不,它使用C ++风格的字符串,这就是为什么我说要将你的返回

类型转换为std :: string。指向char的指针不是正常的方式,它是C

的方式。在C ++程序中,你应该使用std :: string。


Josh



No, it uses C++-style strings, which is why I said to switch your return
type to std::string. Pointers to char is not the normal way, it is the C
way. In a C++ program, you should use std::string.

Josh


这篇关于简单的toString()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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