什么是“未声明的标识符”错误,我如何解决它? [英] What is an 'undeclared identifier' error and how do I fix it?

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

问题描述

什么是未声明的标识符错误?

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

错误文本示例:


  • 对于Visual Studio编译器:错误C2065:'printf':undeclared标识符

  • 对于GCC编译器:`printf'undeclared(首次使用此函数)

  • For the Visual Studio compiler: error C2065: 'printf' : undeclared identifier
  • For the GCC compiler: `printf' undeclared (first use in this function)

推荐答案

它们通常来自忘记包含包含函数声明的头文件,例如,此程序将给出一个未声明的标识符错误:

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:

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.

要阅读更多内容,请参阅 http://msdn.microsoft.com/en-us/library/aa229215(v = vs60).aspx

To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

另一个常见的初级错误来源当你拼写一个变量:

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;
}



范围不正确



例如,此代码会给出错误,因为您需要使用 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 尚未在首次使用前声明。要修复它,请移动 f 之前的 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(); }

或添加 g 的声明 f

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

这个答案。

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆