使用 new 关键字进行前向声明和类型名

发布于 2024-10-03 15:56:34 字数 430 浏览 4 评论 0原文

我在类 a 中声明类型 b 的新指针时遇到错误。请帮忙。

#include <iostream>

namespace B
{
    class b;
}
class a
{
    private:

    B::b* obj_b;

    public:

    a(){}
    ~a(){}
    void create()
    {
        b* obj_b = new b;
    }
};
class b
{
    private:

        a *obj_a;

    public:
        b()
        {
            obj_a->create();
        }
        ~b(){}
};
int main()
{
    b obj;

    return 0;
}

I'm getting an error below in the class a declaring a new pointer of type b. Please help.

#include <iostream>

namespace B
{
    class b;
}
class a
{
    private:

    B::b* obj_b;

    public:

    a(){}
    ~a(){}
    void create()
    {
        b* obj_b = new b;
    }
};
class b
{
    private:

        a *obj_a;

    public:
        b()
        {
            obj_a->create();
        }
        ~b(){}
};
int main()
{
    b obj;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-10-10 15:56:34
b* obj_b = new b;

这就是你的问题。您可以声明一个指向 B 的指针,因为指针的大小都相同,但如果不向编译器提供类定义,则无法构造指针或按值获取指针。如何知道如何为未知类型分配内存?

b* obj_b = new b;

And there is your problem. You can declare a pointer to a B because pointers are all the same size, but you cannot construct one or take one by value without providing the class definition to the compiler. How would it possible know how to allocate memory for an unknown type?

懒猫 2024-10-10 15:56:34

您的代码中有很多错误。这些与前向声明、完全限定名称使用等相关。

namespace B 
{ 
   class b; 
} 
class a 
{ 
private: 

   B::b* obj_b;            // change 1 (fully qualified name)

public: 
   void create();          // change 2 (can't use b's constructor now as B::b is not 
                           // yet defined)
   a(){} 
   ~a(){} 

}; 

class B::b                 // change 3 (fully qualified name)
{ 
private: 

   a *obj_a; 

public: 
   b() 
   { 
      obj_a->create(); 
   } 
   ~b(){} 
}; 

void a::create()             // definition of B::b's constructor visible now.    
{ 
   B::b* obj_b = new B::b;   // And here also use fully qualified name
} 

int main() 
{ 
   B::b obj; 

   return 0; 
} 

There were many errors in your code. These are related to forward declaration, fully qualified name usage etc.

namespace B 
{ 
   class b; 
} 
class a 
{ 
private: 

   B::b* obj_b;            // change 1 (fully qualified name)

public: 
   void create();          // change 2 (can't use b's constructor now as B::b is not 
                           // yet defined)
   a(){} 
   ~a(){} 

}; 

class B::b                 // change 3 (fully qualified name)
{ 
private: 

   a *obj_a; 

public: 
   b() 
   { 
      obj_a->create(); 
   } 
   ~b(){} 
}; 

void a::create()             // definition of B::b's constructor visible now.    
{ 
   B::b* obj_b = new B::b;   // And here also use fully qualified name
} 

int main() 
{ 
   B::b obj; 

   return 0; 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文