Using headers and structs in C++

Hey all,

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."

//in X.h
Position position; // 1 position

//in X.cpp
position.x = 0;
position.y = 0;
positions is not defined as a pointer, yet you are assigning it a pointer from new.

Why not just Positions positions[2], and get rid of the "new" altogether? It will make your life much easier.
I don't know any much about pointers (or C++), sorry. I'm more used to Java. Anyways, this works.

Continuing, I'd like to define the array size as a random value.
So I have

//X.h
int val;
Position positions[];

#include <cstdlib.h>
val = rand() % 4;
Position position[val];

Error >> Expected constant expression.

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.
I'm gonna use vectors now.

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;
}
Where do you initialize the size of the vector?

From your example above, this is more or less what I would expect to see.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//X.h
#ifndef X_H
#define X_H

#include <vector>

class X:
{
    struct Position
    {
        int x;
        int y;
    };
private:
    std::vector<Position> positions;
public:
    X();
    ~X();
};
#endif 


1
2
3
4
5
6
7
8
9
10
//X.cpp
X::X()
: positions(2)
{
//initiate array with random points
positions[0].x = 1;
positions[0].y = 2;
positions[1].x = 3;
positions[1].y = 3;
}
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).
Xivor wrote:
I prefer to use the vector as a global variable, so I can use it in a method (not per se the Constructor).


I think you misunderstand how member variables can be used. Member variables can be used in any method of the class.
Perhaps? What can I say?

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.
You will want to quickly learn the difference between a declaration and a definition. Very important terms and easy to confuse.

std::vector<int> v1; is a variable definition. Definitions are only permitted in source files.
Topic archived. No new replies allowed.