LNK2019错误,在Visual Studio 2012 C++的main中使用的函数

LNK2019 error with functions used in main with Visual Studio 2012 C++

本文关键字:main 函数 C++ 2012 错误 Visual Studio LNK2019      更新时间:2023-10-16

我收到以下错误:

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()函数,但无法链接它。