【发布时间】:2014-06-20 02:32:36
【问题描述】:
添加.cpp
int add(int x, int y)
{
return x + y;
}
Main.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
当我尝试编译此程序时,我收到一条关于 main.cpp 第 6 行的错误消息,指出:“错误:'add' 未在此范围内声明”。
【问题讨论】:
-
您究竟是如何包含 add.cpp 的?
-
在
Add.cpp和Main.cpp中添加包含int add(int x, int y);和#include "Add.hpp"的第三个文件Add.hpp。 -
请注意,您可以包含 .cpp 文件,但您不应该这样做,因为它会在其包含的每个文件中插入重复的函数定义