对class::method的未定义引用
Undefined reference to class::method
在这些日子里,我想通过将我的一个旧程序转换为可以在其他程序中使用的方法的对象来尝试一些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
}
否则这些方法将是自由函数,而不是定义的类方法。因此,你的类方法仍然未定义,当链接器试图链接这些方法时,它找不到它们,并显示未定义的方法引用错误。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用