C 两个派生的类中有彼此的对象(包括问题)

C++ two derived classes have objects of each other in them (include issue)

本文关键字:对象 包括 问题 两个 派生      更新时间:2023-10-16

我有以下文件

  • json.hpp
    • jsonobject.hpp
    • jsonarray.hpp

在每个文件中都有一个类别的definiton(json,jsonobject:public json,jsonarray:public json(,后两个 ot ot otter 具有将另一个作为参数的函数(并且不是作为参考类型,那么简单的正向声明并不能解决问题(。这些文件是:

json.hpp:

#ifndef _JSON
#define _JSON
#include <string>
#include <iostream>
#include "../../lib/simplejson/json.hpp"
class JSON{
    protected:
        std::string content;
    public:
        JSON(std::string content) : content(content)
        {}
        operator std::string()
        {return content;}
        friend std::ostream& operator<<(std::ostream&, JSON&); 
};
inline std::ostream& operator<<(std::ostream& os, JSON& content)
{
    os<<content.content;
    return os;
}
#endif

jsonobject.hpp:

#ifndef _JSONOBJECT
#define _JSONOBJECT
#include "json.hpp"
class JSONArray;
class JSONObject : public JSON {
    public:
        JSONObject(std::string= "{}");
        JSONObject get(std::string);
        template<typename T> void put(std::string, T);
        operator int();
        JSONObject operator[](int);
};
inline JSONObject::operator int() {return atoi(content.c_str());}

inline JSONObject::JSONObject(std::string content) : JSON(content) {}
inline JSONObject JSONObject::get(std::string key) {
    json::JSON obj = json::JSON::Load(content);
    JSONObject json(obj[key].dump());
    return json; }
template<typename T> inline void JSONObject::put(std::string key, T value) {
    json::JSON obj = json::JSON::Load(content);
    obj[key] = value;
    content = obj.dump(); } template<> inline void JSONObject::put<JSON&>(std::string key, JSON& value) {
    json::JSON obj = json::JSON::Load(content);
    json::JSON obj2 = json::JSON::Load((std::string)value);
    std::cout<<obj2<<std::endl<<std::endl;
    obj[key] = obj2;
    content = obj.dump();     } template<> inline void JSONObject::put<JSONObject>(std::string key, JSONObject value) {  //here is JSONObject used in a manner which requires it not to only be forward-declared
    (*this).put(key, dynamic_cast<JSON&>(value)); } template<> inline void JSONObject::put<JSONArray&>(std::string key, JSONArray& value) {
    (*this).put(key, dynamic_cast<JSON&>(value)); }
#endif

jsonarray.hpp:

#ifndef _JSONARRAY
#define _JSONARRAY
#include "jsonobject.hpp"
class JSONArray : public JSON{
    public:
        JSONArray(std::string = "[]");
        JSON operator[](int n);
        template<typename T> void add(int, T);
};
inline JSONArray::JSONArray(std:: string content) : JSON(content)
{}
inline JSON JSONArray::operator[](int n)
{
    json::JSON obj = json::JSON::Load(content);
    JSONObject json(obj[n].dump());    //Here is JSONObject instantiated, which does not work with only forward-declarataion
    return json;
}
template<typename T> inline void JSONArray::add(int n, T t)
{
        json::JSON obj = json::JSON::Load(content);
        obj[n] = t;
        content = obj.dump();
}
template<> inline void JSONArray::add<JSON>(int n, JSON t)
{
        json::JSON obj = json::JSON::Load(content);
        json::JSON obj2 = json::JSON::Load((std::string)t);
        obj[n] = obj2;
        content = obj.dump();
}
#endif

我想念什么?我也尝试将它们全部放在一个文件中,也无济于事。

不,它不会帮助您。您需要将实现转移到单独的类中,以打破周期并在其中一个类中使用实现的正向参考。该技术称为柴郡的猫,它类似于:

A.hpp
// AImple uses B.
class AImpl;
class A
{
    AImpl* impl;
public:
    void f(); // f uses impl.
};
B.hpp
#include "a.hpp"
class B
{
    A a; // B can use A here.
};
A.cpp
#include "a.hpp"
#include "aimpl.hpp"
void A::f()
{
   impl -> f();
}
AImpl.cpp
#include "b.hpp"
#include "AImpl.hpp"
// implementation which uses B.