std::cout 字符串不起作用

发布于 2024-12-12 19:26:18 字数 1124 浏览 4 评论 0原文

我有一个类 State,它有一个名为 moveTypestring 数据类型。在我的代码的实现中,我调用了一个 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);
    }

在上面的代码中,_minStateupState 的实例。另外,我还确保我的复制构造函数和赋值运算符已被修改为包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-12-19 19:26:18

确实没有足够的代码可以确定,但我有一个猜测:要么您实际上在“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).

苍风燃霜 2024-12-19 19:26:18

好吧,不是一个答案,而是一个简短的示例,它按照您似乎想要的方式工作:

#include <string>

class State
{
  private:
    std::string m_moveType;

  public:
    State() : m_moveType( "unknown" ) {}

   std::string getMoveType() const { return m_moveType; }
   void setMoveType( const std::string& moveType ) { m_moveType = moveType; }
};

在您的主函数中,或者您需要一个状态向量,您可以这样写:

#include <iostream>
#include <vector>
#include "State.h"

int main()
{
  std::vector< State > states;
  for( int i=0; i<10; ++i )
  {
    State newState;
    newState.setMoveType( "state" );
    states.push_back( newState );
  }

  // do whatever you need to do....
  std::vector< State >::iterator it;
  std::vector< State >::iterator end = states.end();
  for( it=states.begin(); it != end; ++it )
    std::cout << (*it).getMoveType() << std::endl;

  return 0;
}

一些备注:

  • 按值传递参数,如 setMoveType( string s ) 不是
    建议改为传递常量引用。按值传递会产生
    传递对象的完整副本,
  • 请小心包含和命名空间,如有疑问,请花费额外的时间
    如果您打算使用命名空间中定义的功能,请输入 std::...
    std,并且永远不要在头文件中键入 using namespace std
  • 将私有成员初始化为合理的默认值并在类中执行
    初始化列表

Well not an answer but a short example that works the way you seem to intend this to work:

#include <string>

class State
{
  private:
    std::string m_moveType;

  public:
    State() : m_moveType( "unknown" ) {}

   std::string getMoveType() const { return m_moveType; }
   void setMoveType( const std::string& moveType ) { m_moveType = moveType; }
};

In your main function or were else you need a vector of States you could write this:

#include <iostream>
#include <vector>
#include "State.h"

int main()
{
  std::vector< State > states;
  for( int i=0; i<10; ++i )
  {
    State newState;
    newState.setMoveType( "state" );
    states.push_back( newState );
  }

  // do whatever you need to do....
  std::vector< State >::iterator it;
  std::vector< State >::iterator end = states.end();
  for( it=states.begin(); it != end; ++it )
    std::cout << (*it).getMoveType() << std::endl;

  return 0;
}

A few remarks:

  • passing parameters by value like setMoveType( string s ) is not
    adviseable, pass const references instead. Passing by value incurrs a
    full copy of the passed object
  • be careful with includes and namespaces, in doubt take the extra time
    to type std::... if you intend to use a feature defined in namespace
    std, and never type using namespace std in a header file.
  • initialize private members to a sensible default and do it in the class
    initializer list
二手情话 2024-12-19 19:26:18

我也不确定这一点,但你似乎将此状态存储在向量中。您能否发布有关如何在向量中设置元素的代码?需要注意的是,一旦插入向量中的元素,就无法更新该元素(除非存储指向该元素的指针)。另外,根据您调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文