I need help with the push_back command

I can't get the pushback to work. The dot '.' between my vector and the pushback command is giving me an error. Here is the relevant code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  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...
Last edited on
you have to push something onto it!

in your case a vector of ints.

closed account (o3hC5Di1)
Hi there,

As you can see here:

vector<vector<int>> Solutions

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.

All the best,
NwN
Last edited on
I tried

1
2
 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 


and I get the another mistake on the same line.

Here is the exact error:

error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'int' to 'std::vector<_Ty> &&'

Could it be because I have a multidimensional vector? :/ If so, how would I go about pushing it?

Here is what it looks like when I try to compile it. Notice the red underlining the dot?
http://gyazo.com/bfe98a43770cb94ab730f0955d911a3d.png

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.
Last edited on
a quick example of how you'd build a simple vector of vector of integers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#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?
I tried what you did, no more compilation error, but I get a runtime error:

http://gyazo.com/36d183869137a141f85117d8860d8bff.png

(For reference, top is program running, middle is code, bottom is error)

I am not supposed to see the last line 1 (1,2). I don't know what that is.

If I add your whole code, I get 1 (1,2)(2,3)(3,4)(4,5), etc...
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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.
Last edited on
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...

Last edited on
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!
Topic archived. No new replies allowed.