【发布时间】:2015-02-05 04:34:53
【问题描述】:
我是 C++ 新手,当我尝试运行此程序时,它告诉我:“错误 LNK2001:无法解析的外部符号“私有:静态 int Plate::plate_nID”。我现在只是尝试创建板类和打印出身份证。不知道哪里出错了。
#pragma once
#include <string>
using namespace std;
class Plate{
private:
int id;
string plateName;
static int plate_nID;
int generateID(){
plate_nID++;
return plate_nID;
}
public:
Plate(string name){
plateName = name;
id = generateID();
}
~Plate(){}
int getID(){
return id;
}
string getName(){
return plateName;
}
};
这是我的主要内容:
#include "Plate.cpp"
#include "PlateNode.cpp"
using namespace std;
int main(){
Plate s=Plate::Plate("p1");
cout << s.getID();}
我看过这个问题: Undefined reference to static class member 与我的类似问题被标记为重复,但是当我尝试这样做时,它告诉我:不能在类之外实例化非静态成员。请帮忙!
【问题讨论】:
-
或者在函数
generateID中将plate_nID设为静态(其本身应该是静态的) -
考虑
Plate(const string& name)和const string& getName() const
标签: c++