C++ 什么是';未声明的标识符';错误以及如何修复它?

C++ 什么是';未声明的标识符';错误以及如何修复它?,c++,compiler-errors,declaration,undeclared-identifier,C++,Compiler Errors,Declaration,Undeclared Identifier,什么是未声明的标识符错误?常见的原因是什么?如何解决 错误文本示例: 对于Visual Studio编译器:错误C2065:'cout':未声明的标识符 对于GCC编译器:'cout'未声明(首次在此函数中使用) 它们最常见的原因是忘记包含函数声明的头文件,例如,此程序将给出一个“未声明的标识符”错误: 缺少标题 范围不正确 例如,此代码将给出一个错误,因为您需要使用std::string: #include <string> int main() { std::stri

什么是未声明的标识符错误?常见的原因是什么?如何解决

错误文本示例:

  • 对于Visual Studio编译器:
    错误C2065:'cout':未声明的标识符
  • 对于GCC编译器:
    'cout'未声明(首次在此函数中使用)

它们最常见的原因是忘记包含函数声明的头文件,例如,此程序将给出一个“未声明的标识符”错误:

缺少标题 范围不正确 例如,此代码将给出一个错误,因为您需要使用
std::string

#include <string>

int main() {
    std::string s1 = "Hello"; // Correct.
    string s2 = "world"; // WRONG - would give error.
}
g
在首次使用前尚未声明。要修复它,请将
g
的定义移动到
f
之前:

void g() { }
void f() { g(); }
void g(); // declaration
void f() { g(); }
void g() { } // definition
或者在
f
之前添加
g
声明:

void g() { }
void f() { g(); }
void g(); // declaration
void f() { g(); }
void g() { } // definition
stdafx.h不在顶部(与特定值相比) 这是特定于VisualStudio的。在VS中,您需要在任何代码之前添加
#包括“stdafx.h”
。编译器将忽略它之前的代码,因此如果您有以下情况:

#include <iostream>
#include "stdafx.h"
#包括
#包括“stdafx.h”
#include
将被忽略。您需要将其移动到以下位置:

#include "stdafx.h"
#include <iostream>
#包括“stdafx.h”
#包括

<>请编辑这个答案。在C和C++中,

< P> >所有名字都必须在使用前声明。如果尝试使用尚未声明的变量或函数的名称,则会出现“未声明的标识符”错误

然而,函数在C中是一个特例(并且仅在C中),因为您不必首先声明它们。C编译器将假定函数的数量和类型与调用中的参数相同。如果实际函数定义不匹配,您将得到另一个错误。C++中不存在函数的特殊情况。

通过确保在使用函数和变量之前声明它们,可以修复此类错误。如果是
printf
,则需要包含头文件
(或
在C++中)


对于标准功能,我建议您检查并搜索您想要使用的功能。每个函数的文档都会告诉您需要什么头文件。

这就像使用函数而不声明它一样。头文件将包含 函数printf()。将头文件包括在程序中这是解决方案。 某些用户定义函数在使用前未声明时也可能会出错。如果
它在全球范围内使用,没有问题

c错误中所有未声明的变量都会出现,因为编译器无法在项目中找到它。可以包括定义变量的库的外部(头)文件。因此,在您的问题中,您需要
,这是一个标准的输入输出文件,它描述printf()的功能


根据,fprintf()的声明在中,即在使用函数之前必须包含它。

在对话中考虑类似的情况。假设你的朋友对你说,“鲍勃要过来吃晚饭”,而你却不知道鲍勃是谁。你会感到困惑的,对吗?你的朋友应该说:“我有一个同事叫鲍勃。鲍勃要过来吃饭。”现在鲍勃已经被宣布了,你知道你朋友在说谁了

当您试图使用某个标识符(函数、变量、类等的名称)而编译器未看到该标识符的声明时,编译器会发出“未声明的标识符”错误。也就是说,编译器不知道您指的是什么,因为它以前从未见过它

如果你在C或C++中得到这样的错误,那就意味着你没有告诉编译器你要使用的东西。声明通常存在于头文件中,因此这可能意味着您没有包含适当的头文件。当然,可能是您根本没有记住声明实体

一些编译器根据上下文给出更具体的错误。例如,试图编译
X
如果类型
X
没有用clang声明,则会告诉您“未知类型名称
X
”。这更有用,因为您知道它试图将
X
解释为一种类型。然而,如果你有
intx=y,其中未声明
y
,它将告诉您“使用未声明的标识符
y
”,因为对于
y
可能表示的确切内容存在一些歧义。

这些错误信息

1.For the Visual Studio compiler: error C2065: 'printf' : undeclared identifier
2.For the GCC compiler: `printf' undeclared (first use in this function)
这意味着您使用name
printf
,但编译器看不到名称的声明位置,因此不知道它的含义

程序中使用的任何名称应在使用前声明。编译器必须知道名称表示什么

在这种特殊情况下,编译器看不到名称声明
printf
。正如我们所知(但不是编译器),它是C++中的标题<代码> <代码>中的标准C函数的名称,在C中或在头中<代码> <代码>中,并置于标准中(<代码> STD::/COD>)和Global(<代码>::/COD>)(不一定)名称空间。< /P> 因此,在使用此函数之前,我们必须通过包含相应的头向编译器提供其名称声明

比如说 C:

#include <stdio.h>

int main( void )
{
   printf( "Hello World\n" );
}
#include <cstdio>

int main()
{
   std::printf( "Hello World\n" );
   // or printf( "Hello World\n" );
   // or ::printf( "Hello World\n" );
}
但大体上,您输入了一个打字错误,而不是
PrintHello
,而是用小写字母“p”输入
PrintHello

#include <cstdio>

void PrintHello()
{
    std::printf( "Hello World\n" );
}

int main()
{
   printHello();
}
#包括
void PrintHello()
{
std::printf(“Hello World\n”);
}
int main()
{
printHello();
}

在这种情况下,编译器将发出这样一个错误,因为它没有看到名称声明
printHello
PrintHello
PrintHello
是两个不同的名称,其中一个已声明,另一个未声明,但在main的主体中使用
#include <cstdio>

int main()
{
   std::printf( "Hello World\n" );
   // or printf( "Hello World\n" );
   // or ::printf( "Hello World\n" );
}
void PrintHello()
{
    std::printf( "Hello World\n" );
}
#include <cstdio>

void PrintHello()
{
    std::printf( "Hello World\n" );
}

int main()
{
   printHello();
}
using namespace <MyNamespace>
MyNamespace::MyClass myClass;
if(a==b)
double c;
getValue(c);
#include "pch.h"
#include <stdio.h>
#include <iostream>
#include "stdafx.h"
#include "pch.h"  // must be first

#include "bar.h"  // next block
#include "baz.h"
#include "foo.h"