简体   繁体   English

std :: cout的字符串不起作用

[英]std::cout of string not working

I have a class State that has a string data type called moveType . 我有一个类State ,它有一个名为moveTypestring数据类型。 In the implementation of my code, I am calling a setter void setMoveType(string _moveType); 在我的代码的实现中,我调用了一个setter void setMoveType(string _moveType); and it's implemented with just moveType = _moveType; 它只用moveType = _moveType;

When I call my getter string getMoveType() const; 当我调用我的getter string getMoveType() const; on an instance of State and output it to cout, nothing is displayed. 在一个State实例上并将其输出到cout,不显示任何内容。

I am couting upon entering the getMoveType() function. 我在输入getMoveType()函数时大声疾呼。 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. 我觉得这在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);
    }

In the above code, _minState and up are instances of State . 在上面的代码中, _minStateupState实例。 Also, I have made sure that my copy constructor and assignment operator have been modified to include moveType assignments. 此外,我已确保我的复制构造函数和赋值运算符已被修改为包含moveType赋值。

没有足够的代码可以肯定地知道,但我有一个猜测:要么你实际上分配给“set”函数中的阴影变量而根本没有设置class属性,或者你的State对象实际上已经被破坏了字符串变为空(因为在使用销毁的内存时,为空是一种可能的选项)。

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. 同样取决于你如何调用set,可能会有问题。

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 像setMoveType(string s)那样按值传递参数不是
    adviseable, pass const references instead. 建议,改为传递const引用 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. 注意包含和名称空间,如果你打算使用在命名空间std中定义的功能,并且永远不要在头文件中使用命名空间std键入,那么请花费额外的时间来输入std :: ...
  • initialize private members to a sensible default and do it in the class initializer list 将私有成员初始化为合理的默认值,并在类初始化列表中执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM