C++类没有返回我的私有变量的正确值
我试图让这个程序接受用户输入并将其放入公共函数中并将其分配给 privateVariable,然后我希望它将 privateVariable 的值返回到 main() 并将其输出到屏幕,但它显示的所有内容是未定义 int 的值 (-858993460)。我在这里遇到什么逻辑问题?
#include <iostream>
#include <string>
using namespace std;
class MyClass
{
private:
int privateVariable;
public:
int userVariable;
void setVariable(int userVariable)
{
privateVariable = userVariable;
}
int getVariable()
{
return privateVariable;
}
};
int main()
{
int userVariable;
cin >> userVariable;
MyClass object1;
MyClass object2;
object1.setVariable(userVariable);
object2.getVariable();
cout << object2.getVariable();
system("PAUSE");
return 0;
}
Im trying to get this program to take the users input and put that into a public function and assign it to the privateVariable, then I want it to return the value of privateVariable to main() and output it to the screen, but all it displays is the value of an undefined int ( -858993460 ). What logical problem am I having here ?
#include <iostream>
#include <string>
using namespace std;
class MyClass
{
private:
int privateVariable;
public:
int userVariable;
void setVariable(int userVariable)
{
privateVariable = userVariable;
}
int getVariable()
{
return privateVariable;
}
};
int main()
{
int userVariable;
cin >> userVariable;
MyClass object1;
MyClass object2;
object1.setVariable(userVariable);
object2.getVariable();
cout << object2.getVariable();
system("PAUSE");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在
object1
中进行设置并从object2
中获取。object1
和object2
是不同的对象。由于object2
中的变量未设置,因此您会得到一个垃圾值。我发现
MyClass
中没有使用 publicuserVariable
。You are setting in
object1
and getting fromobject2
.object1
andobject2
are different objects. As variable inobject2
is not set, you get a garbage value.And I see no use of public
userVariable
inMyClass
.您没有设置变量。您对
object1
调用setVariable
,对object2
调用getVariable
,因此object1
的成员仍未初始化。为此,取决于您想要什么:
这样, privateVariable 将是类范围的成员,而不是实例范围的成员。这意味着该类的所有实例都具有相同的值(即使未创建实例)。这也意味着您可以将两个函数设为静态:
并且可以在没有实例的情况下调用方法:
另一个选择是,如果您不需要静态成员,则为两个对象设置成员:
或者,您可以定义一个构造函数并设置那里的变量:
这样,您创建的每个对象都会将成员初始化为 5。
You are not setting the variable. You call
setVariable
onobject1
andgetVariable
onobject2
, so the member ofobject1
remains uninitialized.For this to work, depending on what you want:
This way, privateVariable will be a class-scoped member, not instance-scoped. That means it has the same value for all instances of the class (and even if instances were not created). This also means you can make both your functions static:
and you can call the methods without instances:
Another option is, if you don't want static members, to set the member for both objects:
Or, you could define a constructor and set the variable there:
With this, every object you create will have the member initialized to 5.
当您在 object1 上设置变量时,object2 没有初始化您的变量,您发布的代码仅在 privateVariable 为静态时才有效。
object2 does not have your variable initialized as you set it on object1, the code you posted would only work if privateVariable was static.