相关疑难解决方法(0)

交叉引用包含c ++程序中的头文件

我很好奇一个场景设置如下例:

以下是放在名为Header1.h的文件中的代码:

#ifndef HEADER1_H
#define HEADER1_H

#include "Header2.h"

class Class1
{
 Class2 class2Instance;
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是将放在名为Header2.h的文件中的代码:

#ifndef HEADER2_H
#define HEADER2_H

#include "Header1.h"

class Class2
{
 Class1 class1Instance;
};

#endif
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我收到错误消息(因为我假设的包含),但感觉我需要这样做才能在每个单独的类中包含每个对象.任何人都可以帮助我实现这个目标,我做错了什么?

c++ class include

21
推荐指数
2
解决办法
2万
查看次数

如何处理模板类标头中的循环 #include 调用?

与交叉引用标头中的错误“未终止的条件指令”相关

我有一个可序列化的模板类:

可序列化.h

#pragma once
#ifndef SERIALIZABLE_H
#define SERIALIZABLE_H
#include "Logger.h"
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception_ptr.hpp>

template<class T>
class Serializable {
public:
    static bool Deserialize(Serializable<T>* object, std::string serializedObject) {
        try {
            return object->SetValuesFromPropertyTree(GetPropertyTreeFromJsonString(serialized));
        } catch (...) {
            std::string message = boost::current_exception_diagnostic_information();
            Logger::PostLogMessageSimple(LogMessage::ERROR, message);
            std::cerr << message << std::endl;
        }
    }
private:
    static boost::property_tree::ptree GetPropertyTreeFromJsonString(const std::string & jsonStr) {
        std::istringstream iss(jsonStr);
        boost::property_tree::ptree pt;
        boost::property_tree::read_json(iss, pt);
        return pt;
    }
}
#endif // SERIALIZABLE_H
Run Code Online (Sandbox Code Playgroud)

但问题是 Logger 类使用了一个继承自 …

c++ templates circular-dependency template-classes

2
推荐指数
1
解决办法
204
查看次数