class Board {
public:
Board(int BoardSize); // constructor takes the board size as input
void Solve();
private:
vector<vector<int>> Solutions; // Set of all solutions
}
void Board::Solve() /*Obviously this function not complete,
but the dot is underlined and I get an error message saying:
"ERROR No instance of overloaded function!"*/
{
Solutions.push_back();
}int main () {
// define the board size
int BoardSize;
std::cout << "Please enter the board size (or -1 to exit)" << endl;
// read in the board size from user
std::cin >> BoardSize;
// Build the board of given size
Board B(BoardSize);
// solve for N Queens
B.Solve();
}
I bolded my problem in the code. Thank you for your help! Been stuck for days...
Solutions is a std::vector containing int's. push_back() is used to add an element to a vector, so it needs a value as an argument:
1 2 3 4 5
void Board::Solve()
{
int result = 3*5*9-12; //whatever you need to do to create a solution
Solutions.push_back(result); //add the result to the container
}
More information about std::vector and its member functions is available here: http://www.cplusplus.com/reference/vector/vector/
Hope that helps, please do let us know if you require any further help.
If you need context: The size of the solutions vector will determine the number of solutions. The values in the vector spaces could all be 0 for all I care. Not my choice, it's a restriction given by the teacher.
#include <vector>
int main()
{
// a vector of ints
std::vector<int> intVectorOne;
// add some test data
intVectorOne.push_back(1);
intVectorOne.push_back(2);
intVectorOne.push_back(3);
// another vector of ints
std::vector<int> intVectorTwo;
// add some test data
intVectorTwo.push_back(6);
intVectorTwo.push_back(7);
intVectorTwo.push_back(8);
// we now have 2 integer vectors.
// Now, you you have declared something like this:
std::vector<std::vector<int>> Solutions;
// so we call push_back and add our vectors of ints.
Solutions.push_back(intVectorOne);
Solutions.push_back(intVectorTwo);
return 0;
}
mutexe... You just pushed back a vector inside a vector? Do I have to do that if I have a vector within a vector?
Yes. A vector of vectors is the same as a vector of any other type.
If you have a vector of ints, what do you push back onto it? An int.
If you have a vector of strings, what do you puch back onto it? A string.
If you have a vector of vectors, what do you push back onto it? A vector.
If having a vector of vectors is confusing you (and the syntax can certainly get confusing!) you can help yourself think of it in more abstract terms by using typedefs. So, you could have:
// This defines a type that represents a single solution.
typedef std::vector<int> Solution;
class Board {
public:
Board(int BoardSize); // constructor takes the board size as input
void Solve();
private:
vector<Solution> Solutions; // Set of all solutions
}; // <<< YOU NEED A SEMICOLON HERE
void Board::Solve()
{
Solution mySolution; // Remember, this is actually a vector!
// Do something to populate the solution with the right values.
Solutions.push_back(mySolution);
}
That might make it a bit clearer what exactly you're supposed to push_back into what.
Thanks mikeyboy, mutexe and NwN; I understand now the pushing vector with a vector; however, I still get a runtime error when trying out my code, while pushing a vector with a vector. Any idea why? And I get a line outputted that I have no idea what they mean...
I was trying to call members of Solutions that did not exist. I was pushing Solutions back by 1, then calling the Nth member that did not exist, thus the error. Thank you guys!