使用头文件和源文件时出现问题 我试图学习如何在C++程序中使用头文件/源文件,但是我无法得到一个非常基本的程序来正确编译。当我尝试编译时,会出现诸如“预期的非限定id”或“使用未声明的标识符”之类的错误
这是我头文件(project5functions.h)中的全部内容:使用头文件和源文件时出现问题 我试图学习如何在C++程序中使用头文件/源文件,但是我无法得到一个非常基本的程序来正确编译。当我尝试编译时,会出现诸如“预期的非限定id”或“使用未声明的标识符”之类的错误,c++,C++,这是我头文件(project5functions.h)中的全部内容: \ifndef项目5功能 #定义项目5功能 #包括 #包括 std::int testing123(std::int a); #恩迪夫 关联的project5functions.cpp: #include <iostream> #include "project5functions.h" using namespace std; int testing123(int a) { retur
\ifndef项目5功能
#定义项目5功能
#包括
#包括
std::int testing123(std::int a);
#恩迪夫
关联的project5functions.cpp:
#include <iostream>
#include "project5functions.h"
using namespace std;
int testing123(int a) {
return a + 4;
}
#包括
#包括“project5functions.h”
使用名称空间std;
内部测试123(内部a){
返回a+4;
}
和我尝试编译的主要C++文件:
#include <iostream>
#include "project5functions.h"
using namespace std;
int main () {
cout << testing123(7) << endl;
return 0;
}
#包括
#包括“project5functions.h”
使用名称空间std;
int main(){
cout类型int不是std名称空间的一部分,不要使用std::int,只使用int
要回答第二个问题,使用名称空间std将使std名称空间内的方法充斥您的作用域。名称空间有助于减少与其他名称空间的冲突。您不需要为头文件中的整数指定std名称空间(std::
)
std::int testing123(std::int a);
到
应该是这个技巧。
不需要<代码>:ST::int /Cuth>在C++中是足够的。@ LeFLUO作为已验证的代码> STD::INT/CUT>甚至是错误的。在翻译单元<代码>使用名称空间STD; >可以被认为是好的,但是取决于。它可能是好的,但没有理由。它们是语言的一部分。谢谢,这使感觉
std::int testing123(std::int a);
int testing123(int a);