std::cout 字符串不起作用
我有一个类 State
,它有一个名为 moveType
的 string
数据类型。在我的代码的实现中,我调用了一个 setter void setMoveType(string _moveType);
并且它是通过 moveType = _moveType;
当我调用 getter 时 实现的string getMoveType() const;
放在 State
的实例上并将其输出到 cout,没有任何显示。
我在进入 getMoveType()
函数时进行计算。该参数确实具有正确的值,但似乎根本没有设置。
有人知道吗?我觉得这是 c++ 中简单/琐碎的事情,我完全忘记了。
string State::getMoveType() const {
return moveType;
}
void State::setMoveType(string move_type) {
cout << "In setMoveType and param = " << move_type << endl;
moveType = move_type;
}
std::cout << vec_possibleSuccessors[i].getMoveType() << endl; // within loop;
vector<State> vec_possibleSuccessors;
if (_minState.canMoveUp()) {
up = _minState.moveUp();
up.setMoveType("UP");
up.setF(f(up));
vec_possibleSuccessors.push_back(up);
}
在上面的代码中,_minState 和 up 是 State
的实例。另外,我还确保我的复制构造函数和赋值运算符已被修改为包含 moveType
赋值。
I have a class State
that has a string
data type called moveType
. In the implementation of my code, I am calling a setter void setMoveType(string _moveType);
and it's implemented with just moveType = _moveType;
When I call my getter string getMoveType() const;
on an instance of State
and output it to cout, nothing is displayed.
I am couting upon entering the getMoveType()
function. The parameter indeed has the correct value, but it appears that it's not getting set at all.
Does anyone have any idea? I feel this is something simple/trivial in c++ that I'm just completely forgetting.
string State::getMoveType() const {
return moveType;
}
void State::setMoveType(string move_type) {
cout << "In setMoveType and param = " << move_type << endl;
moveType = move_type;
}
std::cout << vec_possibleSuccessors[i].getMoveType() << endl; // within loop;
vector<State> vec_possibleSuccessors;
if (_minState.canMoveUp()) {
up = _minState.moveUp();
up.setMoveType("UP");
up.setF(f(up));
vec_possibleSuccessors.push_back(up);
}
In the above code, _minState and up are instances of State
. Also, I have made sure that my copy constructor and assignment operator have been modified to include moveType
assignments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实没有足够的代码可以确定,但我有一个猜测:要么您实际上在“set”函数中分配了一个隐藏变量并且根本没有设置类属性,要么您的 State 对象实际上已被销毁并且字符串变为空(因为使用被破坏的内存时,空是一种可能的选择)。
There isn't really enough code to know for sure, but I have a guess: Either you actually assigned to a shadowed variable in the "set" function and never set the class attribute at all, or your State object has actually been destroyed and the string becomes empty (since being empty is one possible option when using destroyed memory).
好吧,不是一个答案,而是一个简短的示例,它按照您似乎想要的方式工作:
在您的主函数中,或者您需要一个状态向量,您可以这样写:
一些备注:
建议改为传递常量引用。按值传递会产生
传递对象的完整副本,
如果您打算使用命名空间中定义的功能,请输入 std::...
std,并且永远不要在头文件中键入 using namespace std 。
初始化列表
Well not an answer but a short example that works the way you seem to intend this to work:
In your main function or were else you need a vector of States you could write this:
A few remarks:
adviseable, pass const references instead. Passing by value incurrs a
full copy of the passed object
to type std::... if you intend to use a feature defined in namespace
std, and never type using namespace std in a header file.
initializer list
我也不确定这一点,但你似乎将此状态存储在向量中。您能否发布有关如何在向量中设置元素的代码?需要注意的是,一旦插入向量中的元素,就无法更新该元素(除非存储指向该元素的指针)。另外,根据您调用 set 的方式,可能会出现问题。
I'm not sure on this either, but you appear to be storing this State in a vector. Could you post the code to how you set elements in the vector? Its important to note that you can't update an element in a vector once its inserted (unless you store a pointer to the element). Also depending upon how you call set, there may be problems.