【发布时间】:2014-01-13 17:24:38
【问题描述】:
#include <iostream>
using namespace std;
int GenerateID()
{
static int nextID = 0;
return nextID++;
}
void PrintInformation(Employee EmployeeName)
{
cout << EmployeeName << "'s ID is: " << EmployeeName.ID << endl;
cout << EmployeeName << "'s age is: " << EmployeeName.age << endl;
cout << EmployeeName << "'s wage is: " << EmployeeName.wage << endl;
}
int main()
{
struct Employee
{
int ID;
int age;
float wage;
};
Employee Dominic;
Employee Jeffrey;
Dominic.ID = GenerateID();
Dominic.age = 22;
Dominic.wage = 7.10;
Jeffrey.ID = GenerateID();
Jeffrey.age = 28;
Dominic.wage = 7.10;
PrintInformation(Dominic);
PrintInformation(Jeffrey);
return 0;
}
/*
C:\CBProjects\Practise\main.cpp|11|error: variable or field 'PrintInformation' declared void|
C:\CBProjects\Practise\main.cpp|11|error: 'Employee' was not declared in this scope|
C:\CBProjects\Practise\main.cpp||In function 'int main()':|
C:\CBProjects\Practise\main.cpp|39|error: 'PrintInformation' was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
*/
上面的 pastebin 链接显示了我使用的代码和构建报告。在此报告之后,我尝试在不包含成员的情况下转发声明结构,然后出现“不完整类型”错误。
解决办法是什么?
编辑:我使用的是 c++11
编辑 2:如果我尝试转发声明结构,包括成员,会发生以下情况:
【问题讨论】:
-
你使用的是 C++11 还是 C++03?
-
显然你需要一个前向声明。由于
Employee是一个本地类,我不知道如何编写该前向声明,或者是否可能。 -
重新编辑2:这不是前向声明。那是一个完全独立的声明。现在你有两个东西叫做
Employee——一个在全局范围内声明,另一个在main范围内声明