How to define external struct in a class constructor in c++?

437 views Asked by At

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.

2

There are 2 answers

2
Pepijn Kramer On

Updated example : pimpl pattern to hide details of external struct from clients of your class. (https://en.cppreference.com/w/cpp/language/pimpl)

#include <iostream>

// myclass.h
//-----------------------------------------------------------------------------
// this header file is to be used by the rest of your code.
// e.g. put it in a "public" or sdk folder
//
// this is where the pimpl pattern starts
// basically you split your class into a public facing part
// without implementation details which decouples it from
// the rest of the code (hence compilation firewall)

// forward declaration
class MyClassImpl;

class MyClass final
{
public:
    MyClass();
    ~MyClass() = default;
    void f();

private:
    std::unique_ptr<MyClassImpl> m_impl;
};

// external_struct.h
//-----------------------------------------------------------------------------
struct external_struct
{
    int x = 0;
    int y = 0;
};

// myclass_impl.h
//-----------------------------------------------------------------------------
// this header is internal and should not be in a private folder
// e.g put it in the folder with the rest of the cpp source files

// in this header file it is ok to include "implementation details"
// like the declaration of the external struct
// #include "external_struct.h"

class MyClassImpl final
{
public:
    MyClassImpl() :
        m_struct{ 1,2 }
    {
    }

    ~MyClassImpl() = default;
    
    void f();

private:
    external_struct m_struct;
};

// myclass.cpp
//-----------------------------------------------------------------------------
//#include <myclass.h>
//#include <myclass_impl.h>

MyClass::MyClass() :
    m_impl{ std::make_unique<MyClassImpl>() }
{
}

void MyClass::f()
{
    m_impl->f();
}

// myclass_impl.cpp
//-----------------------------------------------------------------------------

void MyClassImpl::f()
{
    std::cout << m_struct.x << "\n";
    std::cout << m_struct.y << "\n";
}

//-----------------------------------------------------------------------------
// #include "myclass.h"

int main()
{
    MyClass o;
    o.f();
}
0
Yujian Yao - MSFT On

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;
}