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

问题描述




我有以下代码:


//所有变量都在代码中定义


类CMessage {


string CMessage :: GenerateString()

{


string finalmessage;

stringstream s;

s<< codeClass;

finalmessage =" CODE:" + s.str()+" - " ;;

// finalmessage = codeClass +" ----" ;;

finalmessage = finalmessage + messageClass +" - " ;;


返回finalmessage;

}


}


//啊啊


A类{

公开:

string StringToLogger(MessagesNotifier_Client * client,CMessage

* msg)

private:

string msgToLogger;

}

// A. cpp


string A :: StringToLogger(MessagesNotifier_Client * client,CMessage

* msg)

{

msgToLogger = msg-> GenerateString(); //这里错误。

invokerClass = typeid(client).name();

msgToLogger = msgToLogger + invokerClass;

msgToLogger = msgToLogger + " ----";

// Enviamos:" Codigo_de_error" + ---- +miss de error + ---- +

" Nombre de la classe"

返回msgToLogger;


}


}

内部类A我正在调用方法GenerateString()来自班级

CMessage。此方法必须返回一个字符串。该方法生成

字符串没有问题。错误是当结果来自

" msg-> GenerateString()"必须放在msgToLogger中。


错误是:

0xcdcdcdf1中的访问冲突读数。

0xcdcdcdcd中的访问冲突写入。

任何想法或提示要解决它?

解决方案



[snip] ]



你需要传递一个CMessage对象。


HTH,

Michiel Salters




你的意思?我在方法中收到一个CMessage对象

StringToLogger(执行失败)我正在使用它。然后,我

使用CMessage对象来使用该方法(在CMessage中声明)

GenerateString。




你的意思?我在方法中收到一个CMessage对象

StringToLogger(执行失败)我正在使用它。然后,我用
使用CMessage对象来使用该方法(在CMessage中声明)

GenerateString。



不 - 你没有收到CMessage对象:你收到的是一个

指向CMessage对象的无效指针(也许对象是删除

某点)。至少这就是我如何解释michiels的帖子 - 而且我认为他是正确的。


/ Peter


Hi,

I''ve the following code:

//all variables are defined in the code

Class CMessage{

string CMessage::GenerateString()
{

string finalmessage;
stringstream s;
s << codeClass;
finalmessage = "CODE: "+s.str() + " - ";
//finalmessage = codeClass + "----";
finalmessage = finalmessage + messageClass + " - ";

return finalmessage;
}

}

//A.h

Class A {
public:
string StringToLogger(MessagesNotifier_Client* client, CMessage
*msg)
private:
string msgToLogger;
}
// A.cpp

string A::StringToLogger(MessagesNotifier_Client* client,CMessage
*msg)
{
msgToLogger = msg->GenerateString(); // Error here.
invokerClass = typeid( client ).name();
msgToLogger = msgToLogger + invokerClass;
msgToLogger = msgToLogger + "----";
// Enviamos: "Codigo_de_error" + ---- + "miss de error" + ---- +
"Nombre de la classe"
return msgToLogger;

}

}
Inside de class A I''m calling the method "GenerateString()" from class
CMessage. This method must return a string. The method is generates
the string without problems. The error is when the result from
"msg->GenerateString()" must be placed in msgToLogger.

the Error is:
Access violation reading in 0xcdcdcdf1.
Access violation writting in 0xcdcdcdcd.
Any ideas or hints to solve it?

解决方案

[snip]

You need to pass a CMessage object.

HTH,
Michiel Salters


What you mean? I''m receiving a CMessage object in the method
StringToLogger (where the execution fails) and i''m using it. Then , I
use the CMessage object to use the method (declared in CMessage)
GenerateString.



What you mean? I''m receiving a CMessage object in the method
StringToLogger (where the execution fails) and i''m using it. Then , I
use the CMessage object to use the method (declared in CMessage)
GenerateString.

No -you are not receiving a CMessage object: what you receive is an
invalid pointer to a CMessage object (perhaps the object was deleted at
some point). At least this is how i interpret michiels post - and i
believe he is correct.

/Peter


这篇关于返回字符串时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:00