【问题标题】:How to define external struct in a class constructor in c++?如何在 C++ 的类构造函数中定义外部结构?
【发布时间】:2021-12-24 12:51:59
【问题描述】:

我正在尝试初始化在类构造函数内的外部头文件中定义的结构对象。问题是我需要在类头文件myClass.h 中包含头文件,这导致already defined error 我试图将#include structHeader.h 封装在#ifndef #endif 中,但由于某种原因,它在其他一些更改后停止工作。

该结构在myClass.cpp 内部运行良好,但我必须在类外部对其进行初始化,这意味着它将属于该类而不是该类的实例。当然,我必须在 myClass.cpp 中包含结构头文件而不是 myClass.h,即使在这种情况下我在 myClass.cpp 中包含 myClass.h

我找不到任何有关此案例的示例,我将不胜感激。

// 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;  
{

这是我想要做的一个示例,如果你能告诉我如何在 myClass 构造函数中添加外部类的实例而不是将实例作为构造函数参数传递会很棒我已经尝试了很多事情但是仍然出现一些错误。

【问题讨论】:

  • 你说的“初始化一个结构”是什么意思?我想出的最接近的是“初始化类型为结构的对象”。示例代码将有助于说明您要描述的内容。
  • “我需要在类头文件中包含头文件” - 您的问题描述不能证明这种需要。也许您没有在头文件中定义构造函数?但如果是这样的话,为什么这种愿望比它造成的问题更强烈呢?
  • "我已经尝试将#include structHeader.h 封装在#ifndef #endif 中" -- 你应该将#ifndef #define #endif 添加到标题中,而不是包含。

标签: c++ visual-studio class struct lnk2005


【解决方案1】:

更新示例:pimpl 模式可向您的类的客户隐藏外部结构的详细信息。 (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();
}

【讨论】:

  • 感谢您的回复,我之前的问题不清楚,我更新了。您的答案看起来不错,但我想做的不是在构造函数参数中传递结构,而是将其作为类中的私有对象,如果您能更新您的答案,我将不胜感激,因为我仍在寻找合适的解决方案。我已经尝试过你写的东西,我得到了s 未声明,但无论如何,这不是我想要的。我还查看了pointer to implementation,但这又与我需要的不同。我可能在这里遗漏了一些基本的东西,因为我不经常使用 c++。
  • 我更新了示例,如果您还有任何问题,请告诉我。
【解决方案2】:

放心,我修改了部分代码,现在代码可以运行了,我上传了修改后的代码,请仔细对比。

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

【讨论】:

  • @George A....你有任何更新吗?如果您的案例已经解决,请帮忙标记答案。如果没有,请随时与我们联系。您的理解与合作将不胜感激。
猜你喜欢
  • 2015-12-14
  • 1970-01-01
  • 2019-03-30
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2021-06-18
相关资源
最近更新 更多