Reputation: 42450
If I put this code in a .cpp file and run it, it runs just fine:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef vector<int> row;
typedef vector<row> myMatrix;
void main()
{
//cout << endl << "test" << endl;
myMatrix mat(2,2);
mat[0][1] = 2;
cout << endl << mat[0][1] << endl;
}
But, if I make a .h and a .cpp file with the .h file like this, it gives me boatloads of errors.
#ifndef _grid_
#define _grid_
#include<iostream>
#include<vector>
#include<string>
using namespace std;
typedef vector<int> row;
typedef vector<row> myMatrix;
class grid
{
public:
grid();
~grid();
int getElement(unsigned int ri, unsigned int ci);
bool setElement(unsigned int ri, unsigned int ci, unsigned int value);
private:
myMatrix sudoku_(9,9);
};
#endif
These are some of the errors I get:
warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Upvotes: 0
Views: 169
Reputation: 29055
You can't initialize your sudoku_
member object in its declaration like that. That initialization belongs in a constructor initializer block.
Upvotes: 0
Reputation: 42468
As well as qualifying the namespace, you do not supply the constructor arguments for sudoku_ in the header file. You need to define your own constructor for grid and construct sudoku_ in the initialiser list:
grid::grid() : sudoku_(9,9) { }
Upvotes: 1
Reputation: 355069
You need to qualify vector
as std::vector
.
It works in the .cpp file because you use using namespace std;
(do not use using namespace
in a header file).
Furthermore, your declaration of the member variable is incorrect. It should just be:
myMatrix sudoku_;
If you want to set its dimensions, you need to do so in the constructor.
Upvotes: 3