I'm trying to initialize a struct object defined in an external header file inside a class constructor. The problem is I need to include the header file inside the class header file myClass.h, which is causing already defined error
I have tried to encapsulate the #include structHeader.h inside #ifndef #endif and that worked but for some reason, It stopped working after some other changes.
The struct works well inside myClass.cpp but I have to initialize it outside the class, which means it will belong to the class and not to an instance of the class. Of course, I will have to include the struct header file in myClass.cpp instead of myClass.h even though I'm including the myClass.h inside myClass.cpp in this case.
I couldn't find any examples with this case, I would appreciate any help.
// myStruct.h
#pragma once
#ifndef H
#define H
#define NUM 10
typedef struct SUB
{
int exmaple;
int num;
} sub;
typedef struct Circle
{
float circleC;
float circlePoints[NUM];
} Circle;
#endif
// myClass.h
#include "myStruct.h"
class MYCLASS{
private:
Sub subObject;
Circle circleObject;
OTHERCLASS otherInstance;
int someValue;
public:
MYCLASS::MYCLASS(int someValue);
void someFunction();
// myClass.cpp
#include "myClass.h"
#include "otherClass.h"
MYCLASS::MYCLASS(int someValue, OTHERCLASS otherInstance){
this->someValue = someValue;
this->otherInstance = otherInstance;
// DO I need to initialize struct here?
}
MYCLASS::someFunction(){
}
// main.cpp
#include "myClass.h"
#include "otherClass.cpp"
int main(int argc, char* argv[]){
MYCLASS instance(2, OTHERCLASS());
return 0;
{
This is a sample of what I want to do, also if you could tell me how to add an instance of an external class in myClass constructor instead of passing the instance as a constructor parameter would be great I have tried many things but still getting some errors.
Updated example : pimpl pattern to hide details of external struct from clients of your class. (https://en.cppreference.com/w/cpp/language/pimpl)