c ++前向声明

Adr*_*ian 2 c++

#include <iostream>
#include <cstring>
using namespace std;

class Obj;


class Test {
    friend class Obj;
public:
    Test()
    {

    }
    ~Test()
    {

    }
    void foo()
    {
        //print();
            //Obj::print();
            //Obj x;
            //x.print();
    }
};

class Obj {
public:
    void print()
    {
        cout << "print here" << endl;
    }
};

int main()
{
    Test test;
    test.foo();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

快速问题,如何在Test :: foo()中调用正确的打印方式?

Jam*_*lis 5

您需要在定义定义成员函数Obj:

class Test { 
public:
    void foo();
};

class Obj {
public:
    void print() { }
};

void Test::foo() { 
    Obj o;
    o.print();
}
Run Code Online (Sandbox Code Playgroud)