I’m trying to declare a class object inside a header but I can’t get it working. I currently have 2 header files and 2 cpp files that defines what each function does. The classes are called Heltal and Array, and both of them are in their own header file (heltal.h and array.h).
I’m trying to declare the Heltal class object inside the private part of the Array class, but whatever I do I can’t find a way to declare it. I’ve tried including the heltal.h header to the array.h header but then it starts complaining about being redefined.
Declaring it in the array.cpp however works just fine but I would like to have it defined in the header instead.
Here’s what the files look like at the moment:
heltal.h
class Heltal {
public:
Heltal();
Heltal(int tal);
~Heltal();
void set(int tal);
bool operator < (const Heltal &heltal) const
{
return (heltal < heltal.heltal);
}
bool operator > (const Heltal &heltal) const
{
return (heltal > heltal.heltal);
}
private:
int heltal;
};
array.h
#include <vector>
class Array {
public:
Array();
Array(int in);
~Array();
int Random(int min, int max);
private:
Heltal h;
int size;
};
Both headers are included in the main.cpp
You started along the right path when you included
Heltal.hinsideArray.h.Add the include guards to your headers, this will help you avoid duplicate inclusions:
Now you can safely include
Heltal.hat the top ofArray.h, and your problem will be solved.