2016-11-29 94 views
1

我不斷收到我在這裏看到的很多錯誤代碼,但大多數答案似乎都對我沒有的頭文件問題發表評論(我不認爲?)。 sortKey是一個私有靜態成員,我在我的setters和getters中遇到一個錯誤,我相信。架構x86_64的未定義符號靜態類成員錯誤

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) 

我曾想過這一段時間,我無法弄清楚它是什麼,是錯誤的。我是否需要使用Student ::(這是類名)引用setter中的sortKey?該類中的所有方法都被定義爲靜態的。任何幫助將非常感激。

+2

提出更好的問題:MCVE]! –

回答

2

認爲你有一個像

class Student { 
    // ... 
    static int sortKey; 
}; 

聲明在.cpp文件提供Student::sortKey一個定義:

int Student::sortKey = SORT_BY_LAST; 
相關問題