为什么 A 和 B 类的前向声明不起作用?
#include <iostream>
using namespace std;
class A, B;
class A {
public:
A() {
new B();
}
};
class B {
public:
B() {
new A();
}
};
int main () {
A a = new A();
B b = new B();
};
编译器错误:
classes.cpp:4:8: error: expected unqualified-id before ‘,’ token
classes.cpp:4:10: error: aggregate ‘A B’ has incomplete type and cannot be defined
classes.cpp: In constructor ‘A::A()’:
classes.cpp:9:7: error: expected type-specifier before ‘B’
classes.cpp:9:7: error: expected ‘;’ before ‘B’
classes.cpp: In function ‘int main()’:
classes.cpp:22:14: error: conversion from ‘A*’ to non-scalar type ‘A’ requested
classes.cpp:23:4: error: expected ‘;’ before ‘b’