2012-09-04 35 views
1

我收到以下错误:LNK2019错误与功能主要用于与Visual Studio 2012 C++

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl setup(void)" ([email protected]@YAXXZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "void __cdecl display(void)" ([email protected]@YAXXZ) referenced in function _main 1>C:\code\Project1\Debug\Project1.exe : fatal error LNK1120: 2 unresolved externals

我如何解决这个问题?

这里是主类,其中功能用于:

#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 
+0

在目前的情况下,它并不会帮到你很多,但也许在长期的编程中:在'inteface.h'中,不要把任何东西放在包围卫士之外 – ch0kee

+0

好问的问题! –

回答

3

您声明这些为全局函数。

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

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

+0

现在它说:interface.obj:错误LNK2001:无法解析的外部符号“class std :: basic_string ,class std :: allocator >(* options)[6]”(?options @@ 3PAY05V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ A) –

+1

define string options [10] [6];在'interface.cpp'这样的地方:'string options [10] [6];'。你在'interface.h'中用'extern'声明了它,并承诺你将它定义在其他地方。 – ch0kee

+0

谢谢,修复它 –

0

setup()函数位于ui命名空间中,但是当您尝试使用它时,您并未指定临时文件吨。改为尝试ui::setup();

相同的解决方案display()

除此之外,您需要重新检查您的interface.h头文件。在这里,你声明了这些函数,而不用在他们的名字空间中包含声明。这就是为什么你能够编译main()函数但不能链接它。

+0

这实际上并不足以做到这一点,因为它们是在“ui”类中实现的,但在标题中没有这种方式... –

+0

好的,谢谢。我会更新我的答案。 – mah