首页 文章

与Visual Studio 2012 C主要使用的函数LNK2019错误

提问于
浏览
1

我收到以下错误:

1> main.obj:错误LNK2019:未解析的外部符号“void __cdecl setup(void)”(?setup @@ YAXXZ)在函数_main 1> main.obj中引用:错误LNK2019:未解析的外部符号“void __cdecl display(void )“(?display @@ YAXXZ)在函数_main 1> C:\ code \ Project1 \ Debug \ Project1.exe中引用:致命错误LNK1120:2个未解析的外部

我该如何解决这个问题?

这是使用函数的主类:

#include "interface.h"
using namespace std;

int main(){
    setup();
    display();
    system("pause");
    return 0;
}

这是interface.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;
        }
    }
};

这是interface.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

2 回答

  • 3

    您将这些声明为全局函数 .

    但是,您的实现在 class ui{ 中定义了它们 .

    您可以删除 class ui{ 和匹配的右括号,它应该可以正常工作 .

  • 0

    您的 setup() 函数位于 ui 命名空间内,但是当您尝试使用它时,您没有指定它 . 请尝试使用 ui::setup(); .

    display() 的解决方案相同 .

    除此之外,您还需要重新检查您的interface.h头文件 . 在这里,您将声明这些函数而不将声明括在其命名空间中 . 这就是为什么你能够编译main()函数但不能链接它 .

相关问题