C 两个派生的类中有彼此的对象(包括问题)
C++ two derived classes have objects of each other in them (include issue)
我有以下文件
- 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.
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- 从多个源构造一个对象,包括一个对象向量
- C 两个派生的类中有彼此的对象(包括问题)
- 包括一个大标头以使用对象作为默认参数
- 哪个C 标准包括要添加到对象文件中的文件强制代码 /数据
- 为什么在对自定义对象的向量进行分类时,我们将在struct的定义中包括比较函数
- C++ - 相互对象包括
- 包括从C++存档 (.a) 到共享库的对象
- 如何强制依赖静态对象(包括模板成员)的初始化顺序
- 视觉对象包括 C++ 中单独文件夹中的文件
- 在对象中存储一个std::函数,该函数包括std::占位符
- 谁包括了要定义的对象
- 在c++ 11中,是否有一种方法可以在调用以任何可调用对象(包括绑定方法)作为参数的函数时不需要模板参数
- 是否有任何cpp函数或对象(不包括从c继承的)不是线程安全的,即使每个线程对自己的数据进行操作
- 包括头文件和.cpp文件到主.cpp文件LNK2005错误已经在对象中定义