使用简单宏 C++ 的未声明标识符错误

edo*_*101 2 c++

看一下这个:

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

    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

pas*_*ugh 7

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

全部替换AREA_CIRC(radius) (PI * pow(radius, 2))

结果,您只会得到纯文本替换,包括radius哪个确实是未知标识符。你可能指的是一个类似函数的宏:

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

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