When creating a multidimensional array im getting the following error:
a non static member reference must be relative to a specific object c++
My code:
class Level
{
private:
int width = 6;
int height = 6;
Room rooms[width][height];
public:
Level();
~Level();
};
When width and height are replaced, when creating the array, with 6 (the value of the int's) it works. Why can't I use an int that holds the same value?
I don't need variable sizes (else I should use a vector), I thought using some variables would make it more readable for others when reading it.
In my constructor im going to fill the array in a for loop like:
Level::Level(int id)
{
for (int i = 0; i < levelWidth; i++)
{
for (int x = 0; x < levelHeight; x++)
{
//Do the filling here
}
}
}
If you have access to C++11 you can use constexpr
values
class Level
{
private:
static constexpr int width = 6;
static constexpr int height = 6;
Room rooms[width][height];
public:
Level();
~Level();
};
Please note that both member are now static. Constexpr means that the value can be evaluated at compile time, so it's not a variable size array.
If you want variable sized array you must use vector or (not recommended) use your own memory allocation strategy.
const
works, but constexpr
are better. constexpr
Says that the value can be evaluated at compile time, even with more complex types. You can even make them function, like
static constexpr int width() { return 6; }
And more importantly, you can use the values inside template parameters.
std::array<std::array<Room, width>, height>
If you can, I suggess you to read about std::array , because they provide a zero overhead alternative to raw arrays
This is not allowed in c++. You cannot create variable size array.
If you want to have a variable size I suggest allocating your memory in the constructor according to width and height and declare room as Room** rooms
;
If you really need variable length, you could also use std::vector
In your case you would have: std::vector<std::vector<Room>> rooms
Edit: If you don't need variable length, in that case you need to make width
and height
static members or you could use std::array which explicitly made for fixed-sized arrays.
Edit2:
Reason why what you are trying to do is not working is because VLA were not implemented in C++ (they are in C though) because if you have a variable size you can simply use the std::vector
. You can find some discussions here on that topic.
Also it would appear that what you want to achieve is ok if you use clang (see discussion here ) even though it is contrary to the standards.
if you need to provide dimension length you should define it as static const
or in c++11 static contexpr
or perhaps pass those argument as template parameters like this
template<std::size_t Width, std::size_t Height>
class Level
{
private:
Room rooms[Width][Height];
public:
Level();
~Level();
};
or even better use std::array
and if the dimension length not need you may use std::vector
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.