类C++中的函数问题(LNK2019和LNK1120错误)
Problems with functions in classes C++ (LNK2019 & LNK1120 errors)
我一直在为我的大学班做一个项目,该项目使用c++中的类,不幸的是,每当我试图调用类中传递参数的函数时,程序都无法编译,并出现以下两个错误:
错误LNK2019未解析的外部符号"int __cdecl binsearch(class Course**const,int,char*const("(?binsearch@@YAHQAPAVCourse@@HQAD@Z)在函数_main Project1 C:\Users\cvos\source\repos\Project1\Project1\courses_main.obj 1 中引用
和
错误LNK1120 1未解析的外部项目1 C:\Users\cvos\source\repos\Project1\Debug\Project1.exe 1
我查找了LNK问题,大多数结果表明这与c++与c中的符号有关(该修复程序不起作用(,或者链接visual studio中的文件有问题(该修复也不起作用,(,最后是因为它需要在控制台子系统上(它已经是这样了(。
奇怪的是,如果我注释掉我在"Course"类中对所有传递参数的函数的调用,程序就会运行良好。只有当我试图使用在"Course"类中创建的函数时,程序才无法运行,这让我强烈怀疑我在向成员函数传递变量的方式上做错了什么。
我将发布我的代码的相关部分:
在我的头文件"courses.h"中,我声明我的函数:
int binsearch(Course* co_ptr[], int size, char search[]);
在我的第二个源文件"courses_functions.cpp"中,我定义了函数:
int Course::binsearch(Course* co_ptr[], int size, char search[])
{
int low = 0, high = size - 1, first_index = -1, last_index = -1, num_of_entries = 0;
while (low <= high)
{
int mid = (low + high) / 2;
if (co_ptr[mid]->name == search)
{
bool found = false;
int i = mid;
while (!found) //Check values below mid
{
if (co_ptr[i]->name == search)
{
first_index = i; //first_index now holds a potential first occurence of our name
if (i == 0)
{
found = true;
}
}
else
{
found = true;
}
i--; //decrement i and check again.
}
i = mid; //Reset i
found = false; //Reset found
while (!found) //Check values above mid
{
if (co_ptr[i]->name == search)
{
last_index = i; //last_index now holds a potential last occurence of our name
if (i == size - 1)
{
found = true;
}
}
else
{
found = true;
}
i++; //increment i and check again.
}
break; //Breaks us out of the main while loop
}
else if (co_ptr[mid]->name < search)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if ((first_index != -1) && (last_index != -1))
{
for (int i = first_index; i <= last_index; i++)
{
std::cout << "nEntry found: "
<< std::endl << co_ptr[i]->name << ' ' << co_ptr[i]->units << " units, grade:" << co_ptr[i]->grade;
num_of_entries++;
}
return num_of_entries;
}
else
{
std::cout << "nEntry not found.";
return num_of_entries;
}
}
最后,在我的主源文件"courses_main.cpp"中,我调用函数:
else if (selection == 3) //Display a course
{
char title[50] = "";
int results = 0;
std::cout << "nEnter a class to search for: ";
std::cin.getline(title, 50, 'n');
std::cin.ignore();
results = binsearch(courses, size, title);
}
由于这是一门大学课程,我不想使用任何替代方法,我主要想弄清楚为什么我使用的方法会返回我上面分享的错误,但如果有必要,我会很乐意发布更多的代码片段。
谢谢!
原因几乎可以肯定是以下原因之一:
- 您没有编译实现文件(只是在其他地方使用头(
- 您正在编译实现,但在将对象链接到可执行文件中时没有使用已编译的对象
- 您在命名方面有一些小的不匹配,例如,在类上下文中不使用
binsearch()
,或者以某种方式使用稍微不同的签名(不太可能给出您告诉我们的内容(
需要查看"courses.h"文件的声明。您可能已经在Course
类声明之外声明了binsearch。在这种情况下,您将得到一个链接器错误,如前所述。主要根据您的使用情况。。这个函数的实现不需要在Course
类中,它可以是Course类之外的独立函数。一旦您将函数定义移动到Course
类之外,只要您在MSVC IDE中的同一项目中有courses_functions.cpp和courses_main.cpp文件,您的链接器就应该消失。
- 类C++中的函数问题(LNK2019和LNK1120错误)
- LNK2019和LNK1120错误,未解析的外部和未解析的符号
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- "error LNK2019: unresolved external fatal"和"error LNK1120: 1 unresolved externals"
- QT错误:LNK1120:1未解决的外部Main.OBJ:-1:错误:LNK2019运行QMAKE
- C++使用 Visual Studio 编译错误(LNK1120 和 LNK2019)
- SDL 错误LNK1120 & LNK2019
- 错误 LNK2019:未解析的外部和LNK1120:1 个未解析的外部
- 操作员过载未解决的外部错误LNK1120、LNK2019
- 致命错误LNK1120:1 个未解析的外部 & 错误 LNK2019:函数___tmainCRTStartup中引用_main未解析的外部符号
- LNK2019:函数"函数"中'symbol'引用未解析的外部符号;致命错误 LNK1120:1 未解析的外部
- 程序应该运行…致命错误LNK1120和错误LNK2019
- 即使在一个简单的代码上也能获得LNK2019和LNK1120
- 链接器错误LNK2019和LNK1120
- 链接错误:LNK2019 & LNK1120
- visual studio 2010-C++错误LNK2019和错误LNK1120
- 错误LNK2019:函数__mainCRTStartup中引用的未解析外部符号_main和错误2错误LNK1120:1个
- 错误 LNK2019 和 致命错误 LNK1120:2 个未解析的外部
- Visual C++ Error: LNK2019, LNK2028 and LNK1120
- LNK1120: 1个未解析的外部符号和LNK2019:未解析的外部符号