阿莫·西林 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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上