【发布时间】:2011-03-27 04:11:33
【问题描述】:
我试图了解在 C++ 中包含多个文件的工作原理。我做了很多搜索,最后我写了一个测试代码来总结我的问题。我有两个头文件和两个 cpp 文件,如下所示:
test1.h:
#ifndef _TEST_1_H
#define _TEST_1_H
int val = 10;
void func1();
#endif
test2.h:
#ifndef _TEST_2_H
#define _TEST_2_H
#include "test1.h"
void func2();
#endif
test1.cpp:
#include <iostream>
#include "test1.h"
void func1()
{
std::cout<<val<<std::endl;
}
test2.cpp:
#include <iostream>
#include "test2.h"
void func2()
{
func1();
}
我的主文件如下所示:
test.cpp:
#include <iostream>
#include "test2.h"
#include "test1.h"
int main()
{
func1();
func2();
getchar();
return 0;
}
我使用的是 VS10,我只添加了“test.cpp”作为源文件。当我编译这段代码时,我得到以下错误:
**1>test.obj : error LNK2019: unresolved external symbol "void __cdecl func2(void)" (?func2@@YAXXZ) referenced in function _main **
**1>test.obj : error LNK2019: unresolved external symbol "void __cdecl func1(void)" (?func1@@YAXXZ) referenced in function _main **
即使包含两个头文件,我也不太明白为什么我会得到这个?我错过了什么?
任何帮助将不胜感激!
谢谢 新手
【问题讨论】:
标签: c++ visual-c++