是Visual Studio 2013还是什么????- "Identifier not found"

Is it Visual Studio 2013 or what???? - "Identifier not found"

本文关键字:not found Identifier 是什么 Visual Studio 2013      更新时间:2023-10-16

我正在阅读Bjarne的-使用c++的原理和实践(我发现它对初学者非常有用,因为我是一个),并且有一个示例计算器。我键入它在书中具有的函数,但使用不同的组织:头文件。我有一个特别的问题:

#ifndef Primary_h
#define Primary_h
#include "std_lib_facilities.h"
#include "Token.h"
#include "Expression.h"
//Deal with numbers and parentheses
//USES:expression() and get_token
double primary(){
    Token t = get_token();
    switch (t.kind){
    case '(':
    {
        double d = expression();
        t = get_token();
        if (t.kind != ')')
            cerr << "')' expectedn";
        return d;
    }
    case '8': return t.value;
    default: cerr << "primary expectedn";
    }
}
#endif

当我编译我得到:

error C3861: 'expression': identifier not found 

尽管我添加了expression头文件,它是:

#ifndef Expression_h
#define Expression_h
#include "std_lib_facilities.h"
#include "Token.h"
#include "Term.h"
//Deal with + and -
//USES: term() and get_token
double expression(){
    double left = term();                               //read and evaluate an expression
    Token t = get_token();                              //get the next token
    switch (t.kind){                                    //see which kind of token it is
    case '+': left += term(); t = get_token(); break;   //read and evaluate a Term, then do an addition
    case '-': left -= term(); t = get_token(); break;   //read and evaluate a Term, then do a subtraction
    default: return left; break;                        //return the value of the expression
    }
}
#endif

正如你在expression中看到的,我使用了另一个类似的头文件:Term.h,它工作得很好(在编译中)。错误行用粗体表示。你能帮帮我吗?

像往常一样,这种"神奇的"行为很可能是(读:肯定是)由头文件的循环包含引起的。

Include保护符打破了无限递归,但这样做的一个副作用是,某些头文件第一眼看起来"包含",实际上并没有包含(被保护符跳过)。

在你的案例中,包括守卫完成了他们的工作,阻止了Expression.h被适当地包含到Primary.h中。这就是为什么expressionPrimary.h中保持未声明的原因。

但是为什么你在头文件中写函数定义??我相信你不可能从比亚恩的书里得到这个想法。如果将函数定义移动到.cpp文件中,并在头文件中仅留下声明,则问题将自行解决(尽管这不是以循环方式包含头文件的借口)。

你有一个循环依赖,请检查你所有的头文件和头保护,如果他们是正确的或不