Reputation: 1958
The following is my .h
#ifndef GRID_H_
#define GRID_H_
#include <array>
namespace game{
class Grid{
public:
static const int dimension = 10;
std::array<int, dimension*dimension> grid;
Grid();
int get_cell(int x, int y);
};
}
#endif /* GRID_H_ */
The following is my .cpp
#include "Grid.h"
namespace game {
Grid::Grid() {
// TODO Auto-generated constructor stub
}
int get_cell(int i, int j){
return (std::get<(i*dimension+j)>grid);
}
}
Compiler says:
error: 'dimension' was not declared in this scope
.
I tried to add the scope resolution operator game::
, but it didn't work. How can I access this constant from the header file?
Shouldn't it be a global public variable?! And the include should copy and paste the code from the header file. I don;t understand what's wrong. Sorry for the basic question; I'm new to C++/
Upvotes: 1
Views: 387
Reputation: 7625
You have to use the class name to access a static
member:
int Grid::get_cell(int i, int j){
return (std::get<(i* Grid::dimension +j)>grid);
}
Note: ::
operator refers to global scope. But dimension
is in class scope, not global.
Your usage of std::get
is wrong. It should be
return (std::get< /*a constant value*/>(grid)); //
Upvotes: 2
Reputation: 45410
int get_cell(int i, int j){
return (std::get<(i*dimension+j)>grid);
}
This defines a global function, you could should refer to dimension
directly:
int get_cell(int i, int j){
return (std::get<(i* Grid::dimension+j) > grid);
// ^^^^^^
}
You should define get_cell
as a member of Grid
int Grid::get_cell(int i, int j){
// ^^^^
return (std::get<(i*dimension+j)>grid);
}
Upvotes: 2