阿莫·西林 2022-03-26 11:42 采纳率: 90%
浏览 12
已结题

关于类相互依赖的问题

//type.hpp

#pragma once

namespace amxl_lib
{
    typedef signed char int8;
    typedef unsigned char uint8;

    typedef short int16;
    typedef unsigned short uint16;

    typedef int int32;
    typedef unsigned int uint32;

    typedef long long int64;
    typedef unsigned long long uint64;

    typedef unsigned short wchar;

#define interface class
}
// comparable.hpp

#pragma once

#include "type.hpp"

namespace amxl_lib
{
    template <typename T>
    interface IComparable
    {
    public:
        virtual int32 compare_to(T* t) = 0;
    };
}
// object.hpp

#ifndef _AMXL_LIB_OBJECT_HPP

#define _AMXL_LIB_OBJECT_HPP "amxl_lib::Object"

#include "comparable.hpp"

#ifdef _AMXL_LIB_STRING_HPP
namespace amxl_lib
{
    class String;
}
#else
#include "string.hpp"
#endif

namespace amxl_lib
{
    class Object : public IComparable<Object>
    {
    public:
        Object()
        {
        }

        virtual uint64 hash_code()
        {
            return (uint64)this;
        }

        virtual bool equals(Object* o)
        {
            return this == o;
        }

        virtual String* get_name()
        {
            return new String("Object");
        }

        int32 compare_to(Object* o) override // IComparable
        {
            return (this > o ? 1 : (this == o ? 0 : -1));
        }
    };
}

#endif
// string.hpp


#ifndef _AMXL_LIB_STRING_HPP

#define _AMXL_LIB_STRING_HPP "amxl_lib::String"

#ifdef _AMXL_LIB_OBJECT_HPP
namespace amxl_lib
{
    class Object;
}
#else
#include "object.hpp"
#endif

//#include <string.h>

namespace amxl_lib
{
    class String : public Object
    {
    private:
        char* m_str;
        uint64 m_size;

    public:
        explicit String()
        {
            m_size = 0;
            m_str = new char[m_size + 1];
            m_str[m_size] = '\0';
        }

        explicit String(uint64 size)
        {
            m_size = size;
            m_str = new char[m_size + 1];
            m_str[m_size] = '\0';
        }

        explicit String(uint64 size, char fill)
        {
            m_size = size;
            m_str = new char[m_size + 1];
            m_str[m_size] = '\0';

            for (char* p = m_str; *p != '\0'; ++p)
            {
                *p = fill;
            }
        }

        explicit String(const char* cstr)
        {
            /*m_size = strlen(cstr);
            m_str = new char[m_size + 1];
            m_str[m_size] = '\0';

            for (uint64 i = 0; i < m_size; ++i)
            {
                m_str[i] = cstr[i];
            }*/
        }

        const char* cstr()
        {
            return m_str;
        }

        uint64 size()
        {
            return m_size;
        }

        char char_at(uint64 index)
        {
            return m_str[index];
        }

        /*void set(const char* cstr)
        {
            m_size = strlen(cstr);
            m_str = new char[m_size + 1];
            m_str[m_size] = '\0';

            for (uint64 i = 0; i < m_size; ++i)
            {
                m_str[i] = cstr[i];
            }
        }*/
    };
}

#endif
// test.cpp


#include <stdio.h>

#include "object"
using namespace amxl_lib;
int main()
{
    Object* obj = new Object();

    return 0;
}

编译的时候会显示找不到Object,在string.hpp的第20行

  • 写回答

1条回答 默认 最新

  • wliafe 2022-03-26 12:19
    关注

    #include "object"这么引用可以???
    在object里include string这时后object类还没构建好呢,而构建string需要object,这两个.h文件相互嵌套是不可以的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月26日

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题