【问题标题】:Undeclared Identifier error using simple macro C++使用简单宏 C++ 的未声明标识符错误
【发布时间】:2020-08-10 15:16:01
【问题描述】:

看看这个:

#include <iostream>  //input outut like cout, cin
#include <string>  //strings
#include <cstdlib>  //includes random num generators and basic c++ functions 
#include <limits> //functions for min and max for data types 
#include <vector>
#include <numeric> //sequences of values
#include <cmath> //math functions
#include <sstream> //string stream
#include <ctime> //time
#include <algorithm> //includes sort
#include <fstream>  //alllows ofstream and ifstream for some reason? unicode vs ansi? 
#include "Shape.h"
#include "Circle.h"
#include <functional>  //allows you to use function poitner? <function>   

#define PI 3.14159 //anywhere you see PI, it will be replaced with the number to its right 
    #define AREA_CIRC (radius) (PI * pow(radius, 2))

    int main(){

    cout << "Circle Area " << AREA_CIRC(5) << endl;

    }

每当我运行这段代码时,它都会给我这个错误:

错误 C2065 'radius': 未声明的标识符 Derektut

为什么? 在宏定义中声明 int radius 没有区别

【问题讨论】:

  • 去掉AREA_CIRC定义中的“int”。
  • @wychmaster 这样做会产生同样的错误
  • 在发布未来的问题时,还包括标题。不知道您是否还包括了所需的
  • @skrrrt 我已经编辑它以包含我的头文件,感谢提示

标签: c++


【解决方案1】:

#define AREA_CIRC (radius) (PI * pow(radius, 2)) 表示

将所有AREA_CIRC 替换为(radius) (PI * pow(radius, 2))

因此,您只会得到包含radius 的纯文本替换,这确实是一个未知标识符。你的意思可能是一个类似函数的宏:

#define AREA_CIRC(radius) (PI * pow(radius, 2))

只需删除宏名称和它的左括号之间的空格。

【讨论】:

  • 是的,这是正确的答案。这也是为什么预处理器指令通常不是一个好主意的原因,尤其是在需要更多参数和多行功能时。
  • @passing_through 谢谢你成功了。第一次使用宏,我不知道间距意味着什么?为什么在这种情况下间距很重要?
  • @edo101 在某些情况下,您需要类似#define FOO (value)(将所有FOOs 替换为(value))以及当您需要#define FOO(value) value + 1(将所有FOO(bar)s 替换为@987654331)时@) 所以 C/C++ 允许这两种变体。
【解决方案2】:

您的宏扩展为

cout << "Circle Area " << (radius) (PI * pow(radius, 2))(5) << endl;

不是你预期的方式。 @passing_through 给了你解药。

【讨论】:

    【解决方案3】:

    以下内容大部分是在您进行编辑之前编写的,因此我建议删除AREA_CIRC(radius) 之间的空格,看看是否有帮助(它对我有用)。

    值得注意的是,其余的大部分仍然适用,除了可能建议摆脱类型声明的部分。


    宏不使用类型声明。它们是纯文本替换(这使得它们使用起来有些不安全)。因此,这里不需要int

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    #define PI 3.14159 //anywhere you see PI, it will be replaced with the number to its right 
    #define AREA_CIRC(radius) (PI * pow(radius, 2))
    
    int main(){
    
    cout << "Circle Area " << AREA_CIRC(5) << endl;
    
    }
    
    

    That should work,尽管特别是如果您使用宏,I don't recommend using namespace std 即使我在这里快速使用它来创建示例。

    最终,最好改用constexpr 函数:

    constexpr float AREA_CIRC (int radius) { // double will work here as well, if desired
        return (PI * pow(radius, 2));
    }
    

    另外,@passing_through 的好建议,您也可以将PI 更改为constexpr

    constexpr double PI = 3.14159;
    

    【讨论】:

    • 谢谢!有很多东西要学。为什么不推荐命名空间标准。它使打字更容易@Chipster
    • 连接的链接解释得比我想象的要好得多。它的要点是这可能导致难以找到和调试的命名冲突。
    • @edo101 编程语言的目标是让编写优质代码成为可能,而不是让打字更容易。
    • 您也可以建议将PI 更改为constexpr double pi
    • @passing_through 很棒的建议。我已经添加了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2017-07-11
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多