LNK2019错误,在Visual Studio 2012 C++的main中使用的函数
LNK2019 error with functions used in main with Visual Studio 2012 C++
我收到以下错误:
1> main.obj:错误LNK2019:未解析的外部符号"void __cdeclsetup(void)"(?setup@@YAXXZ)在函数_main 1>main.obj中引用:错误LNK2019:未解析的外部符号"void __cdecl display(void)"函数_main中引用的(?display@@YAXXZ)1> C:\code\Project1\Debug\Project1.exe:致命错误LNK1120:2未解析外部
我该如何解决此问题?
这是主要的类,其中使用了函数:
#include "interface.h"
using namespace std;
int main(){
setup();
display();
system("pause");
return 0;
}
这是接口.cpp
#include <iostream>
#include <string>
#include "interface.h"
using namespace std;
class ui{
void setup(){
options[0][0]="Hello";
options[1][0]="Hello";
options[2][0]="Hello";
options[3][0]="Hello";
options[4][0]="Hello";
options[5][0]="Hello";
options[6][0]="Hello";
options[7][0]="Hello";
options[8][0]="Hello";
options[9][0]="Hello";
}
void changeOption(int whatOption, string whatText,
string prop1, string prop2, string prop3, string prop4, string prop5){
options[whatOption][0]=whatText;
options[whatOption][1]=prop1;
options[whatOption][2]=prop2;
options[whatOption][3]=prop3;
options[whatOption][4]=prop4;
options[whatOption][5]=prop5;
}
void display(){
for(int x=0;x<9;x++){
cout<<options[x][0]<<endl;
}
}
};
这是接口.h
#include <string>
//#include <iostream>
using namespace std;
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
void setup();
extern string options[10][6];
void changeOption(int whatOption, string whatText, string prop1, string prop2, string prop3, string prop4, string prop5);
void display();
#endif INTERFACE_H
您将这些函数声明为全局函数。
但是,您的实现在class ui{
中定义了它们。
您可以删除class ui{
和匹配的右括号,它应该可以正常工作。
您的setup()
函数位于ui
命名空间内,但当您尝试使用它时,您没有指定它。请改用ui::setup();
。
display()
的解决方案相同。
除此之外,您还需要重新检查您的interface.h头文件。在这里,您声明这些函数,而不将声明封装在它们的命名空间中。这就是为什么您可以编译main()函数,但无法链接它。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 尝试更改 main 函数内的全局变量值时出现编译错误 C++.
- 我应该在 main 函数中写什么来测试我的问题?
- 如何在 main 函数中输出两个新字符串C++?
- 有没有办法通过 main 函数访问受保护的矢量大小而无需将其转换为公共?
- 我们可以在main函数中声明嵌套类对象吗
- 为什么Main函数上的Sleep()会停止所有线程
- C++ main 函数中初始化静态变量
- 为什么VC++/MFC没有main函数?
- 如何在 main 函数的一个对象中初始化两个类的值?C++
- 在 C++ 中解析 main 函数的大量输入的正确方法是什么
- 如何编写一个 void 函数,该函数将在数组中搜索值,并通过引用将该值的位置传递回 main 函数
- 如何将矩阵参数发送到从C 中的MAIN函数
- 如何从子例程函数获取到 main 函数的返回值
- 为什么我不能在 main 函数中更改类的公共变量
- C++编译错误:多个定义"main",但项目中只有一个 main 函数
- 如何使用 x86 程序集在 c 中访问 main 函数中的变量