类C+中的函数问题+;(LNK2019和LNK1120错误) 我一直在为我的大学班级做一个项目,它使用C++类,不幸的是,每当我试图调用一个函数,它在我的类中传递参数时,程序就不能编译,因为下面的两个错误:
错误LNK2019未解析的外部符号“int\u cdecl binsearch(类课程**const,int,char*const)”(?binsearch@@YAHQAPAVCourse@@HQAD@Z)在函数\u main Project1 C:\Users\cvos\source\repos\Project1\Project1\courses\u main.obj 1中引用 及 错误LNK1120 1未解析的外部项目1 C:\Users\cvos\source\repos\Project1\Debug\Project1.exe 1类C+中的函数问题+;(LNK2019和LNK1120错误) 我一直在为我的大学班级做一个项目,它使用C++类,不幸的是,每当我试图调用一个函数,它在我的类中传递参数时,程序就不能编译,因为下面的两个错误:,c++,class,linker-errors,member-functions,C++,Class,Linker Errors,Member Functions,错误LNK2019未解析的外部符号“int\u cdecl binsearch(类课程**const,int,char*const)”(?binsearch@@YAHQAPAVCourse@@HQAD@Z)在函数\u main Project1 C:\Users\cvos\source\repos\Project1\Project1\courses\u main.obj 1中引用 及 错误LNK1120 1未解析的外部项目1 C:\Users\cvos\source\repos\Project1
我查过LNK问题,大多数结果表明它与C++中的符号有关(修复不起作用),或者在VisualStudio中链接文件的问题(该修复也不起作用),最后它与需要在控制台子系统上(它已经是)有关。 奇怪的是,如果我注释掉对“Course”类中传递参数的所有函数的调用,程序运行良好。只有当我试图使用在“课程”类中创建的函数时,程序才无法运行,这使我强烈怀疑我在如何将变量传递给成员函数方面做错了什么
我将发布代码的相关部分: 在我的头文件“courses.h”中,我声明了我的函数:int binsearch(Course* co_ptr[], int size, char search[]);
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;
}
}
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);
}
在我的第二个源文件“courses\u functions.cpp”中,我定义了函数:
int binsearch(Course* co_ptr[], int size, char search[]);
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;
}
}
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);
}
int-Course::bin搜索(Course*co_ptr[],int-size,char搜索[])
{
int low=0,high=size-1,first_index=-1,last_index=-1,num_of_条目=0;
while(低名称==搜索)
{
bool-found=false;
int i=中间;
while(!found)//检查mid以下的值
{
if(co_ptr[i]->name==搜索)
{
first\u index=i;//first\u index现在保存了我们名字的可能首次出现
如果(i==0)
{
发现=真;
}
}
其他的
{
发现=真;
}
i--;//减小i并再次检查。
}
i=mid;//重置i
found=false;//找到重置
while(!found)//检查mid以上的值
{
if(co_ptr[i]->name==搜索)
{
last_index=i;//last_index现在保存我们名字的最后一次出现
如果(i==大小-1)
{
发现=真;
}
}
其他的
{
发现=真;
}
i++;//递增i并再次检查。
}
break;//使我们脱离主while循环
}
else if(co_ptr[mid]->名称<搜索)
{
低=中+1;
}
其他的
{
高=中-1;
}
}
如果((第一个索引!=-1)和&(最后一个索引!=-1))
{
对于(int i=第一个指数;i而言,原因几乎可以肯定是以下之一:
您没有编译实现文件(只是在别处使用头)
您正在编译实现,但在将对象链接到可执行文件时不使用已编译对象
您在命名方面存在一些小的不匹配,例如,在类上下文中不使用bin搜索()
,或者以某种方式使用稍微不同的签名(不太可能给出您告诉我们的内容)
需要查看“courses.h”文件的声明。您可能已在课程
类声明之外声明了binsearch。在这种情况下,您将收到前面提到的链接器错误。
根据您在main中的使用情况。此函数的实现不需要在课程
类中,它可以是课程类之外的独立函数。一旦您将函数定义移到课程
类之外,您的链接器应该消失,前提是您在同一pro中有courses\u functions.cpp和courses\u main.cpp文件在MSVC IDE中创建一个对象。尝试检查是否有您声明但未实现的函数。binsearch
不是一个自由函数,它是一个类方法,因此您需要声明它static
,将它移到Course
类之外,或者从类似Course.binsearch的实例调用它(课程、大小、标题);
@CoryKramer当我把它移到我的课程之外时,它就可以工作了(我以前从大学课程之前的另一个程序中移植了它)。在与以前相同的问题中添加静态结果(LNK2019和LNK1120错误),您最后提出的建议我得到了这些错误:错误(活动)不允许使用E0254类型名Project1和错误C2275'Course':将此类型非法用作表达式Project1,尽管我完全不了解您最后的建议is@0xBlackMirror不幸的是,没有任何函数被声明但没有定义。我该如何解决案例2?我问这个问题是因为其他函数只要我不向类内部传递参数,类内部就可以工作。这方面的一个例子是我的void read()函数,我使用它来收集用户的输入,它工作正常,并且与我的binsearch函数在相同的位置声明和定义。这需要了解MS Visual Studio,但我没有,抱歉。