有几个问题。初始化一个带有某个函数返回值的变量应该没有问题。我们一直这样做,即使通常不会像算术运算符或按位运算符那样看到典型的函数调用。
随着你的功能
double Class1::SetValue(double value2)
{
//generally you are setting some variable that is a part of Class1
this->value = value2;
//if you are returning something you might want to have some error code
//just in case something went wrong, but it looks like you are just
//returning the number that you are sending in as a parameter.
return value2;
}
我不知道什么是你的下一个功能回事,但我们可以对它进行解剖。
void Class2::CountValue(double a)
{
Class1 *object1;//this is a pointer to an object, not an instance of an object
// object1 is not initialized so you cannot
// use object1->value2 even if it is set in
// your default constructor
double value_received = object1->SetValue(object1->value2);
// do something with value_received
}
为了让你的代码工作,你可能需要将其改成
void Class2::CountValue(double a)
{
Class1 object1;//not a pointer to Class1 anymore
// This will work if object1.value2 is set in the default constructor
double value_received = object1.SetValue(object1.value2);
// do something with value_received
}
,或者如果你想同时设置value_recieved和object1.value2我们可以做
void Class2::CountValue(double a)
{
Class1 object1;//not a pointer to Class1 anymore
double value_received = object1.SetValue(12.2);
// do something with value_received
}
来源
2014-01-22 23:20:48
109
'object1'不是99,99%的有效指针。 – luk32
'object1'似乎未初始化。 –
这段代码是你的实际代码还是只是一个例子? – 0x499602D2