1
我有一个静态变量的链接问题。这是我第一次尝试使用静态变量。我正在创建一个矢量,并希望cnt变量在所有Student对象中都是静态的。链接器错误无法解析的外部符号与我的cnt变量
我已搜索周围试图弄清楚这一点。我读过其他人有这个问题,他们没有声明静态变量,他们需要专门为静态变量创建一个新的对象。
我在构造函数中认为SCNT变量声明和设置。什么是在类中实现静态成员变量的正确方法?
Student.h
#pragma once
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(string ID);
virtual ~Student(void);
void cntReset();
int getCnt() const;
int getID() const;
bool operator< (const Student& s) const;
bool operator== (const Student& s) const;
protected:
int id;
static int sCnt;
private:
};
Student.cpp
#include "Student.h"
Student::Student()
{
id = 0;
sCnt = 0;
}
Student::Student(string ID)
{
id = atoi(ID.c_str());
sCnt = 0;
}
哇,好吧,这令人困惑,我从来没有想过这是如何安装。谢谢你解决了这个问题。 – LF4