1

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++/

1
  • I don't understand, where did you define Grid::dimension? shouldn't you define "static const int Grid::dimension = 10" in the .cpp file? Commented May 26, 2014 at 9:49

2 Answers 2

2

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)); // 
Sign up to request clarification or add additional context in comments.

6 Comments

Compiler now says: error: expected ')' before 'grid' error: cannot resolve overloaded function 'get' based on conversion to type 'int'
It refers to (std::get<(i* Grid::dimension +j)>grid) line, you are not using std::get correctly.
Do you know how Im not using it correctly? I changed it to the bracket notation and it works but Idk what I did wrong with that std::get notation
After adding the parens to grid, im getting error: no matching function for call to 'get(std::array<int, 100ull>&)' error: 'i' is not a constant expression
const int index = i*dimension +j; makes index constant, but its value can only be computed at runtime, not compile time. you just try with a constant value (like 0), and see the errors will be gone
|
2
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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.