2014-05-11 59 views
-1

在這些日子裏,我想我會嘗試將我的舊程序一個一個對象,我可以在其他程序中使用的方法在一些C++面向對象的編程工作。 該方案是能夠得到一個字符串數組並打印他們做出選擇畫面是這樣的:未定義參考類::方法

「>東西」
「Stuff2」
「Stuff3」

然後光標可能是用鍵盤箭頭移動,可以通過按Enter鍵選擇一個條目。
不幸的是,在刪除了大多數錯誤之後,唯一剩下的就是我寫的每個方法的「未定義的class :: method()的引用」。
代碼如下:

Selection.cpp

#include "selection.h" 
#include <iostream> 
#include <conio.h> 
#include <stdio.h> 
#include <cstring> 
#include <string> 
#include <vector> 
std::vector<entry> selectedArray; 
int posizione = 1, tastoPremuto;  
void setCurrentArray(std::string selectionEntries[]) 
{ 
    //Copies a string array to a vector of struct entry 
} 

void resetSelection() 
{ 
    //Blanks out every entry.segno 
} 

void selectionUpdate(int stato) 
{ 
    //Moves the cursor 
} 

int getPosition() 
{ 
    return posizione; //Returns the position of the cursor 
} 

void setPosition(int nuovaPosizione) //Sposta il puntatore alla nuovaPosizione 
{ 
    posizione = nuovaPosizione; //Sets the posiion of the cursor 
} 

entry getCurrentEntry(int posizioneCorrente) //Returna l'entry della posizione attuale 
{ 
    //Gets the name of the entry at which the cursor is 
} 

void printEntries() //Stampa tutte le entries con la selezione 
{ 
    //Prints all the entries with the cursor before them 
} 

int getSelection(bool needConfirm) 
{ 
    //gets the selection from keyboard 
} 

Selection.h

#ifndef SELECTION_H 
#define SELECTION_H 
#include <string> 
#include <vector> 

struct entry 
{ 
    std::string entryName; 
    char sign; 
}; 

class selection 
{ 
    public: 
    selection(); 
    virtual ~selection(); 
    void setCurrentArray(std::string selectionEntries[]); 
    void resetSelection(); 
    int getPosition(); 
    void setPosition(int nuovaPosizione); 
    entry getCurrentEntry(int posizioneCorrente); 
    void printEntries(int argumentsNumber); 
    int getSelection(bool needConfirm); 
protected: 
private: 
    int tastoPremuto; 
    int posizione; 
    void selectionUpdate(bool stato); 
    std::vector<entry> selectedArray; 
}; 

#endif // SELECTION_H 

的main.cpp

#include <stdio.h> 
#include <iostream> 
#include <conio.h> 
#include <selection.h> 
#include <vector> 
#include <array> 

std::string selezioneTestArray[3] = {"Prima selezione", "Seconda selezione", "Terza seleazione"}; 

int main() 
{ 
    selection sel; 
    sel.setCurrentArray(selezioneTestArray); 
    sel.setPosition(0); 
    sel.resetSelection(); 
    sel.printEntries(3); 
    sel.getSelection(true); 
    return 0; 
} 

如果您需要整個代碼,這是在我的Github上:https://github.com/LiceoFederici2H/Vita-Da-Lavoratore
對不起,如果它是意大利語,但我也打算用它作爲一個學校項目。
在此先感謝。

回答

1

在源文件(Selection.cpp),你有資格與類名的方法名:

void Selection::setCurrentArray(std::string selectionEntries[]) 
{ 
    //Copies a string array to a vector of struct entry 
} 

否則,這些方法將是免費的功能,而不是定義的類方法。其結果是,你的類方法仍然不確定,當連接嘗試與方法聯繫起來,它無法找到他們,並顯示出該方法錯誤的未定義的引用。

+0

謝謝,現在它工作。不過,它在啓動時崩潰了。任何想法爲什麼它這樣做? – user3624242

+1

@ user3624242:這是一個單獨的問題,或者是需要添加到您的主要問題的東西,我想。 – Rook

+0

@ user3624242,我推薦閱讀[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)以更好地瞭解如何調試。 – chris