什么是“未声明的标识符"错误,我该如何解决?

2023-10-18C/C++开发问题
375

本文介绍了什么是“未声明的标识符"错误,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

错误文本示例:

  • 对于 Visual Studio 编译器:error C2065: 'cout' : undeclared identifier
  • 对于 GCC 编译器:'cout' 未声明(首次在此函数中使用)

解决方案

最常见的原因是忘记包含包含函数声明的头文件,例如,这个程序会给出'undeclared identifier'错误:

>

缺少标题

int main() {std::cout <<你好世界!"<<std::endl;返回0;}

要修复它,我们必须包含标题:

#include int main() {std::cout <<你好世界!"<<std::endl;返回0;}

如果您编写了标题并正确包含了它,则标题可能包含错误的include guard.

要了解更多信息,请参阅 http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

拼写错误的变量

初学者错误的另一个常见原因是变量拼写错误:

int main() {int aComplicatedName;AComplicatedName = 1;/* 注意大写 A */返回0;}

范围不正确

比如这段代码会报错,因为你需要使用std::string:

#include int main() {std::string s1 = "你好";//正确的.字符串 s2 = "世界";//错误 - 会出错.}

声明前使用

void f() { g();}无效 g() { }

g 在第一次使用之前没有被声明.要修复它,请在 f 之前移动 g 的定义:

void g() { }无效 f() { g();}

或者在f之前添加一个g的声明:

void g();//宣言无效 f() { g();}void g() { }//定义

stdafx.h 不在顶部(VS 特定)

这是特定于 Visual Studio 的.在VS中,您需要在任何代码之前添加#include "stdafx.h".编译器忽略之前的代码,所以如果你有这个:

#include #include "stdafx.h"

#include 将被忽略.你需要把它移到下面:

#include "stdafx.h"#include 

随意编辑这个答案.

What are undeclared identifier errors? What are common causes and how do I fix them?

Example error texts:

解决方案

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.

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:

#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:

void g() { }
void f() { g(); }

Or add a declaration of g before f:

void g(); // declaration
void f() { g(); }
void g() { } // definition

stdafx.h not on top (VS-specific)

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:

#include <iostream>
#include "stdafx.h"

The #include <iostream> would be ignored. You need to move it below:

#include "stdafx.h"
#include <iostream>

Feel free to edit this answer.

这篇关于什么是“未声明的标识符"错误,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数

无法访问 C++ std::set 中对象的非常量成员函数

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数

从 lambda 构造 std::function 参数

Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现

STL BigInt 类实现

STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠

使用 std::atomic 和 std::condition_variable 同步不可靠

Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾

在 STL 中将列表元素移动到末尾

Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?

Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6

热门文章

1LNK2038:检测到“RuntimeLibrary"不匹配:值“MT_StaticRelease" 2c++ 编译错误:ISO C++ 禁止指针和整数之间的比较 3CMake 找不到源文件 (add_executable) 4Cmake 链接库目标链接错误 5如何使用 VideoWriter 从 OpenCV 打开 GStreamer 管道 6CMakeLists.txt:30 (project) 中的 CMake 错误:找不到 CMAKE_C_COMPILER 7CMake 无法确定目标的链接器语言 8如何解决munmap_chunk():C++中的无效指针错误

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序