我正在尝试初始化一个在类构造函数内部的外部头文件中定义的结构对象。问题是我需要将头文件包含在类头文件myClass.h
中,这导致了已经定义的错误我曾尝试将#include structHeader.h
封装在#ifndef #endif
中,但由于某种原因,它在其他一些更改后停止了工作。
该结构在myClass.cpp
内部运行良好,但我必须在类外部对其进行初始化,这意味着它将属于该类,而不是该类的实例。当然,我将不得不在myClass.cpp
而不是myClass.h
中包含结构头文件,即使在这种情况下我将myClass.h
包含在myClass.cpp
中。
我找不到任何关于这个案例的例子,如果有任何帮助,我将不胜感激。
// 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构造函数中添加一个外部类的实例,而不是将该实例作为构造函数参数传递,那就太好了。
更新了示例:皮条模式向类的客户端隐藏外部结构的详细信息。(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();
}
请放心,我修改了部分代码,现在代码可以运行了,我上传了修改后的代码,请仔细比较。
// 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;
}