gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:04:57 35 4
gpt4 key购买 nike

看一下这个:

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

}

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

Error C2065 'radius': undeclared identifier Derektut



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

最佳答案

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

replace all AREA_CIRC with (radius) (PI * pow(radius, 2))



结果,您只会得到纯文本替换,包括 radius这确实是一个未知的标识符。您可能指的是一个类似函数的宏:
#define AREA_CIRC(radius) (PI * pow(radius, 2))
只需删除宏名称与其左括号之间的空格。

关于c++ - 使用简单宏 C++ 的未声明标识符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452579/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com