源码:
//student.h
#include<iostream>
using namespace std;
class Student;
class Student
{
const int id;
public:
Student(int d): id(d) { cout<<"student\n"; )
void print() { cout<<id<<"\n"; }
};
class Tutor
{
Student s;
public:
Tutor(Student & st):s(st)
{
cout<<"Tutor\n";
s.print();
}
};
//f0909.cpp
#include"student.h"
extern Student ra;
Tutor je(ra);
int main()
{
return 0;
}
//f0909_2.cpp
#include"student.h"
extern Student ra(18);
编译错误:
f:\题目\student.h(19) : error C2079: 's' uses undefined class 'Student'
f:\题目\f0909.cpp(1) : error C2059: syntax error : 'PCH creation point'
f:\题目\f0909.cpp(3) : error C2238: unexpected token(s) preceding ';'
f:\题目\f0909.cpp(5) : error C2059: syntax error : 'PCH creation point'
f:\题目\f0909.cpp(5) : error C2238: unexpected token(s) preceding ';'
f:\题目\f0909.cpp(6) : error C2059: syntax error : 'PCH creation point'
f:\题目\f0909.cpp(7) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
f:\题目\f0909.cpp(10) : fatal error C1004: unexpected end of file found
解决:绿色代码改为红色
Student(int d): id(d) { cout<<"student\n"; }
原因:缺少右 },或写成)
链接错误: LNK2001: unresolved external symbol class Student ra (?ra@@3VStudent@@A)
解决:运行f0909_2.cpp,再运行f0909.cpp
原因:未将f0909_2.cpp加到工程中去。