I cannot succeed at this (i thought easy to solve) problem.
I want to use a struct array in a .cpp file, declared in a .h file.
My main problem is that I do not have too much knowledge of C++, and the combination of seperating the declaration and initialization with struct arrays causes me troubles.
Thanks.
//X.h
#ifndef X_H
#define X_H
class X:
{
struct Position
{
int x;
int y;
};
private:
Position positions;
public:
X();
~X();
};
#endif
//X.cpp
X::X()
{
//initiate array with random points
positions = new Position[2];
positions[0].x = 1;
positions[0].y = 2;
positions[1].x = 3;
positions[1].y = 3;
}
I've tried some things (like below) but when I close the program I get this error:
"Unhandled exception at 0x00000000 in DebugGameLoop.exe: 0xC0000005: Access violation reading location 0x00000000."
Arrays can only be declared using constant expressions -- they have to be a fixed size at compile time. For variable sized arrays, you want to use a vector.
Now I get the same error. All I do in the header file is:
["Unhandled exception at 0x00000000 in DebugGameLoop.exe: 0xC0000005: Access violation reading location 0x00000000."]
include <vector>
using namespace std;
Class X{
private:
vector<int> v1;
}
or without using namespace
include <vector>
Class X{
private:
std::vector<int> v1;
}
I don't believe that the size of a vector is required to specify?
Just this single line causes me an error: std::vector<Position> positions;
When I quit the program (the X), I will get the message
[Unhandled exception at 0x00000000 in DebugGameLoop.exe: 0xC0000005: Access violation reading location 0x00000000.]
Maybe an option or something, I'll have a look.
I prefer to use the vector as a global variable, so I can use it in a method (not per se the Constructor).
In the header you could declare an int for global use. But why can I not declare a vector?
I can't even put "std::vector<int> v1;" in the header :/ My C++ knowledge doesn't reach any far I guess.