1

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.

Tyler Bell
  • 61
  • 7
  • some code would be helpful here. – vrrathod Oct 28 '11 at 18:01
  • Show the code for getMoveType, and also the place where you are calling it. – Benjamin Lindley Oct 28 '11 at 18:01
  • Some more context would be helpful. Can you show how you're declaring and populating vec_possibleSuccessors, as well as how you declare moveType in your class? Also, is there another function that modifies moveType? That is, is it possible that in between when you set it and print it, it's getting cleared? – Anson Oct 28 '11 at 18:10

3 Answers3

2

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).

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

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.

MJD
  • 1,183
  • 7
  • 13
1

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
Community
  • 1
  • 1
codencandy
  • 1,701
  • 1
  • 10
  • 20