如何修复此回调包含问题



我对C++(和StackOverflow(有点陌生。我正在努力工作,但我有一些#include问题。

我想(从这里(回拨一个电话,但我很难做到。

到目前为止,这是我的代码。当我在someclass.hpp文件中包含child.hpp时(因为Callback<Child>需要关于Child的信息(,它有一个循环包含,编译器崩溃。

我读过关于前向声明(someclass.hpp文件中的class Child;(,在尝试之后,我发现这是可行的,但我也读到了关于这方面的不同意见。

我用#ifndef CLASSNAME #define CLASSNAME ... #endif保护所有.hpp文件

我需要改变我的整个设计吗?或者在我的情况下,什么是最好的选择?

基本.hpp

#include "someclass.hpp"
class Base
{
protected:
unique_ptr<SomeClass> someClass;
};

base.cpp

#include "base.hpp"
Base::Base()
{
this->someClass = make_unique<SomeClass>();
}

child.hpp

#include "base.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
unique_ptr<Callback<Child>> callback;
};

child.cpp

#include "child.hpp"
void Child::callbackFunction(std::string data)
{
/*does something*/
}
Child::Child()
{
this->callback = make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems

this->someClass->setCallback(this->callback);
//^^^^^^^^^^^^^^^ == base.someClass
}

someclass.hpp

#include "child.hpp" // < does crash compiler due to loop
//> someclass.hpp uses child.hpp
//> child.hpp uses base.hpp
//> base.hpp uses someclass.hpp
// and thus loop
class SomeClass
{
public:
void someFunction(std::string data);
void setCallback(unique_ptr<Callback<Child>> callback);
unique_ptr<Callback<Child>> callbackInstance;
};

someclass.cpp

//not 100% sure about the type of this parameter
void setCallback(unique_ptr<Callback<Child>> callback)
{
this->callbackInstance = callback;
}
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}

也在某些类别中。pp

template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};

解决上述错误("child.hpp上'{'标记前的预期类名">(,您应该base.hpp中删除#include "someclass.hpp",并将其替换为class SomeClass正向声明,如下所示。

基本.hpp

#ifndef BASE_H
#define BASE_H
//NO NEED TO INCLUDE someclass.hpp
#include <memory>
class SomeClass;//FORWARD DECLARE SomeClass
class Base
{
std::unique_ptr<SomeClass> someClass; 
public:
//add declaration for default constructor 
Base();
};
#endif

base.cpp

#include "base.hpp"
#include "someclass.hpp"
//other things here
Base::Base()
{
this->someClass = std::make_unique<SomeClass>();
}

child.hpp

#ifndef CHILD_H
#define CHILD_H
#include "base.hpp"
#include <memory>
#include "someclass.hpp"
class Child : public Base
{
public:
void callbackFunction(std::string data);
std::unique_ptr<Callback<Child>> callback;
//add declaration for default constrcutor 
Child();
};
#endif

child.cpp

#include "child.hpp"
void Child::callbackFunction(std::string data){
/*does something*/
}
Child::Child()
{
this->callback = std::make_unique<Callback<Child>>(this, &Child::callbackFunction);
//I can call this->callback->call(data); here without problems
}

someclass.hpp

#ifndef SOMECLASS_H
#define SOMECLASS_H
#include <string>
//REMOVED include child.hpp from here
class SomeClass
{
public:
void someFunction(std::string data);
//I think I need an instance of Callback<Child> here?
};
template<class T>
class Callback
{
public:
Callback(T* instance, void (T::*function)(std::string))
{
this->callbackInstance = instance;
this->callback = function;
}
void call(std::string data)
{
(callbackInstance->*callback)(data);
}
private:
T *callbackInstance;
void (T::*callback)(std::string);
};
#endif

someclass.cpp

#include "someclass.hpp"
void SomeClass::someFunction(std::string data)
{
//here I want to call this "Child::callbackFunction" which should go like "this->callbackInstance->call(data)" ?
}

上面的程序编译并执行成功,如图所示。

摘要

下面列出了我所做的一些更改:

  • 删除了不必要的includes
  • 在child.hpp和base.hpp中添加了默认构造函数的声明
  • 添加了所有标题中的包含防护

最新更新