C++ 什么是“未声明的标识符”错误,我该如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22197030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an 'undeclared identifier' error and how do I fix it?
提问by sashoalm
What are undeclared identifier errors? What are common causes and how do I fix them?
什么是未声明的标识符错误?什么是常见原因,我该如何解决?
Example error texts:
错误文本示例:
- For the Visual Studio compiler:
error C2065: 'cout' : undeclared identifier
- For the GCC compiler:
'cout' undeclared (first use in this function)
- 对于 Visual Studio 编译器:
error C2065: 'cout' : undeclared identifier
- 对于 GCC 编译器:
'cout' undeclared (first use in this function)
回答by sashoalm
They most often come from forgetting to include the header file that contains the function declaration, for example, this program will give an 'undeclared identifier' error:
它们通常来自忘记包含包含函数声明的头文件,例如,该程序将给出“未声明的标识符”错误:
Missing header
缺少标题
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
To fix it, we must include the header:
要修复它,我们必须包含标题:
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
If you wrote the header and included it correctly, the header may contain the wrong include guard.
如果您编写了标题并正确包含了它,则标题可能包含错误的包含保护。
To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.
要了解更多信息,请参阅http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx。
Misspelled variable
拼写错误的变量
Another common source of beginner's error occur when you misspelled a variable:
当你拼错一个变量时,初学者错误的另一个常见来源发生:
int main() {
int aComplicatedName;
AComplicatedName = 1; /* mind the uppercase A */
return 0;
}
Incorrect scope
范围不正确
For example, this code would give an error, because you need to use std::string
:
例如,这段代码会报错,因为你需要使用std::string
:
#include <string>
int main() {
std::string s1 = "Hello"; // Correct.
string s2 = "world"; // WRONG - would give error.
}
Use before declaration
声明前使用
void f() { g(); }
void g() { }
g
has not been declared before its first use. To fix it, either move the definition of g
before f
:
g
在第一次使用之前没有被声明。要修复它,请移动g
before的定义f
:
void g() { }
void f() { g(); }
Or add a declaration of g
before f
:
或者添加一个g
before声明f
:
void g(); // declaration
void f() { g(); }
void g() { } // definition
stdafx.h not on top (VS-specific)
stdafx.h 不在顶部(VS 特定)
This is Visual Studio-specific. In VS, you need to add #include "stdafx.h"
before any code. Code before it is ignored by the compiler, so if you have this:
这是特定于 Visual Studio 的。在VS中,需要#include "stdafx.h"
在任何代码之前添加。编译器忽略它之前的代码,所以如果你有这个:
#include <iostream>
#include "stdafx.h"
The #include <iostream>
would be ignored. You need to move it below:
在#include <iostream>
将被忽略。你需要把它移到下面:
#include "stdafx.h"
#include <iostream>
Feel free to edit this answer.
随意编辑这个答案。
回答by Joseph Mansfield
Consider a similar situation in conversation. Imagine your friend says to you, "Bob is coming over for dinner," and you have no idea who Bob is. You're going to be confused, right? Your friend should have said, "I have a work colleague called Bob. Bob is coming over for dinner." Now Bob has been declared and you know who your friend is talking about.
在谈话中考虑类似的情况。想象一下,您的朋友对您说,“鲍勃要过来吃饭了”,而您不知道鲍勃是谁。你会感到困惑,对吧?你的朋友应该说:“我有个同事叫鲍勃。鲍勃要过来吃饭。” 现在鲍勃已经被宣布,你知道你的朋友在谈论谁。
The compiler emits an 'undeclared identifier' error when you have attempted to use some identifier (what would be the name of a function, variable, class, etc.) and the compiler has not seen a declaration for it. That is, the compiler has no idea what you are referring to because it hasn't seen it before.
当您尝试使用某个标识符(函数、变量、类等的名称)并且编译器没有看到它的声明时,编译器会发出“未声明的标识符”错误。也就是说,编译器不知道您指的是什么,因为它以前没有见过。
If you get such an error in C or C++, it means that you haven't told the compiler about the thing you are trying to use. Declarations are often found in header files, so it likely means that you haven't included the appropriate header. Of course, it may be that you just haven't remembered to declare the entity at all.
如果您在 C 或 C++ 中遇到这样的错误,则意味着您还没有告诉编译器您正在尝试使用的东西。声明通常出现在头文件中,因此这可能意味着您没有包含适当的头文件。当然,也可能是您根本不记得声明实体。
Some compilers give more specific errors depending on the context. For example, attempting to compile X x;
where the type X
has not been declared with clang will tell you "unknown type name X
". This is much more useful because you know it's trying to interpret X
as a type. However, if you have int x = y;
, where y
is not yet declared, it will tell you "use of undeclared identifier y
" because there is some ambiguity about what exactly y
might represent.
一些编译器根据上下文给出更具体的错误。例如,尝试X x;
在X
未使用 clang 声明类型的情况下进行编译会告诉您“未知类型名称X
”。这更有用,因为您知道它试图解释X
为一种类型。但是,如果您有int x = y;
, wherey
尚未声明,它会告诉您“使用未声明的标识符y
”,因为对于确切y
可能代表的内容存在一些歧义。
回答by Dimitar Dimitrov
I had the same problem with a custom class, which was defined in a namespace. I tried to use the class without the namespace, causing the compiler error "identifier "MyClass" is undefined". Adding
我在命名空间中定义的自定义类也遇到了同样的问题。我尝试使用没有命名空间的类,导致编译器错误"identifier "MyClass" is undefined"。添加
using namespace <MyNamespace>
or using the class like
或使用类
MyNamespace::MyClass myClass;
solved the problem.
解决了这个问题。
回答by Some programmer dude
In C and C++ all names have to be declared before they are used. If you try to use the name of a variable or a function that hasn't been declared you will get an "undeclared identifier" error.
在 C 和 C++ 中,所有名称都必须在使用之前声明。如果您尝试使用尚未声明的变量或函数的名称,您将收到“未声明的标识符”错误。
However, functions are a special case in C (and in C only) in that you don't have to declare them first. The C compiler will the assume the function exists with the number and type of arguments as in the call. If the actual function definition does not match that you will get another error. This special case for functions does not exist in C++.
但是,函数在 C 中(并且仅在 C 中)是一种特殊情况,因为您不必先声明它们。C 编译器将假定函数存在,其参数的数量和类型与调用中的一样。如果实际函数定义不匹配,您将收到另一个错误。C++ 中不存在函数的这种特殊情况。
You fix these kind of errors by making sure that functions and variables are declared before they are used. In the case of printf
you need to include the header file <stdio.h>
(or <cstdio>
in C++).
您可以通过确保在使用函数和变量之前声明它们来修复这些类型的错误。如果printf
您需要包含头文件<stdio.h>
(或<cstdio>
在 C++ 中)。
For standard functions, I recommend you check e.g. this reference site, and search for the functions you want to use. The documentation for each function tells you what header file you need.
对于标准功能,我建议您检查例如这个参考站点,并搜索您想要使用的功能。每个函数的文档都会告诉您需要什么头文件。
回答by Vlad from Moscow
These error meassages
这些错误提示
1.For the Visual Studio compiler: error C2065: 'printf' : undeclared identifier
2.For the GCC compiler: `printf' undeclared (first use in this function)
mean that you use name printf
but the compiler does not see where the name was declared and accordingly does not know what it means.
意味着您使用名称,printf
但编译器看不到名称的声明位置,因此不知道它的含义。
Any name used in a program shall be declared before its using. The compiler has to know what the name denotes.
程序中使用的任何名称都应在使用前声明。编译器必须知道名称表示什么。
In this particular case the compiler does not see the declaration of name printf
. As we know (but not the compiler) it is the name of standard C function declared in header <stdio.h>
in C or in header <cstdio>
in C++ and placed in standard (std::
) and global (::
) (not necessarily) name spaces.
在这种特殊情况下,编译器看不到 name 的声明printf
。正如我们所知(但不是编译器),它是<stdio.h>
在 C 中的头文件或<cstdio>
C++中的头文件中声明的标准 C 函数的名称,并放置在标准 ( std::
) 和全局 ( ::
) (不一定)名称空间中。
So before using this function we have to provide its name declaration to the compiler by including corresponding headers.
所以在使用这个函数之前,我们必须通过包含相应的头文件来向编译器提供它的名称声明。
For example C:
例如 C:
#include <stdio.h>
int main( void )
{
printf( "Hello World\n" );
}
C++:
C++:
#include <cstdio>
int main()
{
std::printf( "Hello World\n" );
// or printf( "Hello World\n" );
// or ::printf( "Hello World\n" );
}
Sometimes the reason of such an error is a simple typo. For example let's assume that you defined function PrintHello
有时这种错误的原因是一个简单的错字。例如,假设您定义了函数PrintHello
void PrintHello()
{
std::printf( "Hello World\n" );
}
but in main you made a typo and instead of PrintHello
you typed printHello
with lower case letter 'p'.
但在主要输入有误,并代替PrintHello
您键入printHello
用小写字母“P”。
#include <cstdio>
void PrintHello()
{
std::printf( "Hello World\n" );
}
int main()
{
printHello();
}
In this case the compiler will issue such an error because it does not see the declaration of name printHello
. PrintHello
and printHello
are two different names one of which was declared and other was not declared but used in the body of main
在这种情况下,编译器会发出这样的错误,因为它没有看到 name 的声明printHello
。 PrintHello
并且printHello
是两个不同的名称,其中一个已声明,另一个未声明但在 main 的主体中使用
回答by phil
Another possible situation: accessing parent (a template class) member in a template class.
另一种可能的情况:访问模板类中的父(模板类)成员。
Fix method: using the parent class member by its full name (by prefixing this->
or parentClassName::
to the name of the member).
修复方法:使用父类成员的全名(通过前缀this->
或parentClassName::
成员名称)。
see: templates: parent class member variables not visible in inherited class
回答by Sanket
one more case where this issue can occur,
可能发生此问题的另一种情况,
if(a==b)
double c;
getValue(c);
here, the value is declared in a condition and then used outside it.
在这里,该值在条件中声明,然后在条件之外使用。
回答by Goosebumps
It happened to me when the auto formatter in a visual studio project sorted my includes after which the pre compiled header was not the first include anymore.
当 Visual Studio 项目中的自动格式化程序对我的包含进行排序后,预编译的标头不再是第一个包含时,这发生在我身上。
In other words. If you have any of these:
换句话说。如果您有以下任何一项:
#include "pch.h"
or
或者
#include <stdio.h>
or
或者
#include <iostream>
#include "stdafx.h"
Put it at the start of your file.
将它放在文件的开头。
If your clang formatteris sorting the files automatically, try putting an enter after the pre compiled header. If it is on IBS_Preserveit will sort each #include block separately.
如果您的clang 格式化程序自动对文件进行排序,请尝试在预编译的头文件之后输入一个 Enter。如果它在IBS_Preserve 上,它将分别对每个 #include 块进行排序。
#include "pch.h" // must be first
#include "bar.h" // next block
#include "baz.h"
#include "foo.h"
More info at Compiler Error C2065
编译器错误 C2065 中的更多信息
回答by Declan Nnadozie
Most of the time, if you are very sure you imported the library in question, Visual Studio will guide you with IntelliSense.
大多数情况下,如果您非常确定您导入了有问题的库,Visual Studio 将使用 IntelliSense 为您提供指导。
Here is what worked for me:
这是对我有用的:
Make sure that #include "stdafx.h"
is declared first, that is, at the top of all of your includes.
确保#include "stdafx.h"
首先声明,即在所有包含的顶部。
回答by Amit G.
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. In C++ all names have to be declared before they are used. If you try to use the name of a such that hasn't been declared you will get an "undeclared identifier" compile-error.
C++ 标识符是用于标识变量、函数、类、模块或任何其他用户定义项的名称。在 C++ 中,所有名称都必须在使用之前声明。如果您尝试使用尚未声明的此类名称,则会出现“未声明的标识符”编译错误。
According to the documentation, the declaration of printf() is in cstdio i.e. you have to include it, before using the function.
根据文档, printf() 的声明在 cstdio 中,即您必须在使用该函数之前将其包含在内。