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