C++ - 2 classes 1 file

后端 未结 8 2022
南旧
南旧 2021-01-19 03:11

Suppose I want something of this sort, in one .cpp source file:

class A {
    public:
        void doSomething(B *b) {};
};

class B {
    publi         


        
8条回答
  •  悲&欢浪女
    2021-01-19 03:18

    You need to forward declare B.

    class B; 
    
    class A
    {
    public:        
       void doSomething(B *b) {}
    };
    
    class B 
    {    
    public:        
       void doSomething(A *a) {}
    };
    

    (And BTW, you don't need the semi-colons after the member function curly braces. :) )

提交回复
热议问题