C++ 架构x86_64的未定义符号与静态类成员错误

C++ 架构x86_64的未定义符号与静态类成员错误,c++,static,x86-64,C++,Static,X86 64,我一直收到一个错误代码,我在这里看到了很多,但大多数答案似乎评论的问题与头文件,我没有(我不认为?)。sortKey是一个私有静态成员,我相信我的setter和getter中有一个错误 bool Student::setSortKey(int userKey) { sortKey = SORT_BY_LAST; if(!validSortKey(userKey)) return false; sortKey = userKey; return false;

我一直收到一个错误代码,我在这里看到了很多,但大多数答案似乎评论的问题与头文件,我没有(我不认为?)。sortKey是一个私有静态成员,我相信我的setter和getter中有一个错误

 bool Student::setSortKey(int userKey) {
   sortKey = SORT_BY_LAST;
   if(!validSortKey(userKey))
      return false;
   sortKey = userKey;
   return false;
}
static int getSortKey() { return sortKey; }
而错误

Undefined symbols for architecture x86_64:
  "Student::sortKey", referenced from:
      Student::setSortKey(int) in main.o
      Student::getSortKey() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我已经考虑了一段时间,但我不知道是什么错了。我是否需要在setter中使用Student::(这是类名)引用sortKey?类中的所有方法也定义为静态的。任何帮助都将不胜感激。

假设您有这样的声明

class Student {
    // ...
    static int sortKey;
};
.cpp
文件中为
Student::sortKey
提供定义:

int Student::sortKey = SORT_BY_LAST;
问更好的问题:!