作为空间如何更改?
程序代码:
//ElevatorClass.h #include <iostream> using namespace std; //--------声明类 Elevator ------------ class Elevator { private: int CurrentFloor; void Move(int); public: Elevator(); //缺省的构造函数 Elevator(int); //构造函数(可设置初值) ~Elevator(); //析构函数 void Call(int); void Select(int); static int Count; //static 变量 }; //---------- 设置 static 变量初始值 ----------- int Elevator::Count = 0; //ElevvatorDef.cpp Elevator::Elevator() //定义构造函数 { cout << "构造函数被调用" << endl; CurrentFloor = 1; Count ++; return; } Elevator::Elevator(int N) //定义构造函数(可设置初值) { cout << "构造函数被调用" << endl; CurrentFloor = N; Count ++; return; } Elevator::~Elevator() //定义析构函数 { cout << "析构函数被调用。" << endl; Count --; return; } void Elevator::Call(int N) //定义成员函数 Call() { Move(N); cout << "电梯到了,请进。\n"; return; } //---------定义成员函数 Select() ---------- void Elevator::Select(int N) { cout << "载您到 " << N << " 楼。\n"; if(N > CurrentFloor) cout << "电梯向上。\n"; else cout << "电梯向下。\n"; Move(N); cout << "电梯已到 " << CurrentFloor << " 楼,谢谢观临。\n"; } //--------- 定义 private 成员函数 Move() ----------- void Elevator::Move(int Target) { cout << "电梯门要关了,请小心。" << endl; int Start = CurrentFloor; cout << "电梯目前在 " << CurrentFloor << " 楼。\n"; if(Target >= CurrentFloor) { for(CurrentFloor = Start; CurrentFloor <= Target; CurrentFloor ++) cout << " 灯号:" << CurrentFloor << " 楼\n"; CurrentFloor --; } else { for(CurrentFloor = Start; CurrentFloor >= Target; CurrentFloor --) cout << " 灯号:" <<CurrentFloor << " 楼" << endl; CurrentFloor ++; } cout << "电梯门要开了,请小心。" << endl; return; } //ElevatorMain.cpp //-------------主程序------------- int main() { //-----(1)------ cout << "(1)" << endl; Elevator A; //定义电梯 A cout << "从 5 楼调用电梯:" << endl; A.Call(5); //调用电梯 A A.Select(2); //设置电梯到二楼 cout << "目前 Count 的值是:" << A.Count << endl; //--------(2)--------- cout << "(2)" << endl; Elevator B(3); //定义电梯 B cout << "从 4 楼调用:" << endl; B.Call(4); //调用电梯 B B.Select(8); //设置电梯 B 到八楼 cout << "目前 Count 的值是:" << B.Count << endl; return 0; }
这段程序如果合在一起作为.cpp的文件就可以运行,但是如果以一下//ElevatorClass.h //ElevatorMain.cpp //ElevatorMain.cpp 文件建立一个空间会出现以下错误:
Linking...
ElevatorMain.obj : error LNK2005: "public: static int Elevator::Count" (?Count@Elevator@@2HA) already defined in ElevatorDef.obj
Debug/电梯.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
求解????
电梯.exe - 1 error(s), 0 warning(s)