tags:

views:

82

answers:

2

Hey everyone!

I am trying to write a program that uses a base class to define an algorithm for solving a simple problem. I use a vector of ints as a 'game board'. My question is how can I create a function get_moves that returns a vector of game boards?

Here is the code I have for the function:

std::vector< <std::vector<int> > takeaway::generateMoves( std::vector<int> currBoard ) {


    if( currBoard[0] == 1 || currBoard[0] == 2 ) {
       moves.push_back( 1 );
    }
    else if( currBoard[0] == 3 ) {
       moves.push_back( 2 );
    }
    else if( currBoard[0] == 4 ) {
       moves.push_back( 3 );
    }
    else {
      moves.push_back( 1 );
      moves.push_back( 2 );
      moves.push_back( 3 );
    }

    std::vector< <std::vector <int > > toReturn( moves );

     for( int i = 0; i < moves.size(); i++ ) {

      std::cout << "MOVES: " << moves[i] << std::endl;
    }

  return toReturn;

The errors that I get are:

takeaway.cpp:55: error: template argument 1 is invalid takeaway.cpp:55: error: template argument 2 is invalid

So my question is how can I properly create and return a vector of vectors?

+1  A: 

It looks like you are trying to construct the vector of vector<int> using only a vector<int>. Cannot see how moves is declared but based on the rest of the code, I would think this will work.

std::vector<std::vector <int > > toReturn;
toReturn.push_back(moves);

Don't see the value in using vector of vectors here, but perhaps this is a partial sample.

Also - passing in the board by reference instead of by value would be more efficient, since this avoids the entire thing being copied. You are not altering it so const reference is better.

std::vector< std::vector<int> > takeaway::generateMoves( 
    const std::vector<int>& currBoard )

Returning by value (ie vector< vector as your return value) is probably OK since this code will likely by optimized to avoid taking a copy of toReturn according to the NRVO.

An alternative would be to pass in a non-const reference to the required structure, which should be empty on entry to match current logic:

void takeaway::generateMoves( 
    const std::vector<int>& currBoard, std::vector< std::vector<int> >& toReturn )
Steve Townsend
+4  A: 

You have too many <'s on your template declarations.

std::vector< std::vector<int> > takeaway::generateMoves( std::vector<int> currBoard ) 
{

    if( currBoard[0] == 1 || currBoard[0] == 2 ) {
       moves.push_back( 1 );
    }
    else if( currBoard[0] == 3 ) {
       moves.push_back( 2 );
    }
    else if( currBoard[0] == 4 ) {
       moves.push_back( 3 );
    }
    else {
      moves.push_back( 1 );
      moves.push_back( 2 );
      moves.push_back( 3 );
    }

    std::vector< std::vector<int> > toReturn;
    toReturn.push_back( moves );

     for( int i = 0; i < moves.size(); i++ ) {

      std::cout << "MOVES: " << moves[i] << std::endl;
    }

    return toReturn;
}

There might be more, but that is one of them.

Stargazer712