对class::method的未定义引用

Undefined reference to class::method

本文关键字:未定义 引用 method class      更新时间:2023-10-16

在这些日子里,我想通过将我的一个旧程序转换为可以在其他程序中使用的方法的对象来尝试一些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
对不起,如果它是在意大利,但我想用它作为一个学校的项目。

在源文件(Selection.cpp)中,您必须用类名限定方法名:

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

否则这些方法将是自由函数,而不是定义的类方法。因此,你的类方法仍然未定义,当链接器试图链接这些方法时,它找不到它们,并显示未定义的方法引用错误。