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.
CodePudding user response:
Since as you stated your class will not be the owner of the struct but just needs to have a reference to it a forward declaration of the external struct in your header file is enough. (https://www.learncpp.com/cpp-tutorial/forward-declarations/) If you would need implementation details in your header file you can use the pimpl pattern : https://en.cppreference.com/w/cpp/language/pimpl
#include <iostream>
// myclass.h
//-----------------------------------------------------------------------------
//
// do NOT include "external_struct.h" but add a forward declaration to the struct.
struct external_struct;
class MyClass final
{
public:
explicit MyClass(const external_struct& s);
void f();
private:
const external_struct& m_struct;
};
// myclass.cpp
//-----------------------------------------------------------------------------
// #include "external_struct.h"
struct external_struct
{
int x = 0;
int y = 0;
};
MyClass::MyClass(const external_struct& s) :
m_struct{ s }
{
}
void MyClass::f()
{
std::cout << m_struct.x << "\n";
std::cout << m_struct.y << "\n";
}
//-----------------------------------------------------------------------------
// #include "myclass.h"
int main()
{
external_struct s;
MyClass o{ s };
o.f();
}
CodePudding user response:
Take it easy, I modified part of the code and now the code can run, I uploaded the modified code, please compare carefully.
// myClass.h
#include "myStruct.h"
#include"otherClass.h"
class MYCLASS {
SUB subObject;
Circle circleObject;
OTHERCLASS otherInstance;
int someValue;
public:
MYCLASS(int someValue, OTHERCLASS otherInstance);
void someFunction();
};
// otherClass.h
#pragma once
typedef struct OTHERCLASS
{
} other;
// 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?
}
void MYCLASS::someFunction()
{
}
// main.cpp
#include "myClass.h"
#include "otherClass.h"
int main(int argc, char* argv[]) {
MYCLASS instance(2, OTHERCLASS());
return 0;
}