3

Check this out:

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

    }

Whenever I run this code, it gives me this error:

Error C2065 'radius': undeclared identifier Derektut

Why? declaring int radius in the macro definition makes no difference

edo101
  • 629
  • 6
  • 17

3 Answers3

8

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

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

As a result, you just get plain text substitution including radius which really is an unknown identifier. What you probably meant was a function-like macro:

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

Just remove the space between the macro name and its opening bracket.

passing_through
  • 1,778
  • 12
  • 24
  • Yes this is the correct answer. This is also a reason why preprocessor directives are usually a bad idea, especially when more parameters and multi line functionality is needed. – Omid CompSCI Apr 27 '20 at 06:12
  • @passing_through thanks that did the trick. First time using macros, I didn't know spacing meant that much? Why would spacing matter in this situation? – edo101 Apr 27 '20 at 06:13
  • @edo101 there are cases when you want something like `#define FOO (value)` (replacing all `FOO`s with `(value)`) and when you want `#define FOO(value) value + 1` (replacing all `FOO(bar)`s with `bar + 1`) so C/C++ allow both variants. – passing_through Apr 27 '20 at 06:40
2

Your macro expands as

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

not the way you expected. @passing_through gave you the cure.

2

The below was mostly written before you made your edit, so I will suggest removing the space between AREA_CIRC and (radius) and see if that helps (it worked for me).

Worth noting that the rest still mostly applies, except maybe the part which suggested getting rid of the type declaration.


Macros don't use type declarations. They are pure text substitutions (which makes them somewhat unsafe to use). Thus, int here is unnecessary:

#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, though especially if you're using macros, I don't recommend using namespace std even though I've used it here quickly to create an example.

Ultimately, it'd be better to use a constexpr function instead:

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

Also, excellent suggestion by @passing_through, you can also change PI to a constexpr as well:

constexpr double PI = 3.14159;
  • Thank you! have a lot to learn. why don't you recommend namespace std. It makes typing eaiser @Chipster – edo101 Apr 27 '20 at 06:21
  • The link connected explained that far better than I could. The gist of it is this could lead to naming conflicts that are hard to find and debug. –  Apr 27 '20 at 06:33
  • @edo101 It the goal of programming languages to make it possible to write good quaility code, not to make typing easier. – john Apr 27 '20 at 06:37
  • You can also advise to change `PI` to a `constexpr double pi`. – passing_through Apr 27 '20 at 07:00
  • @passing_through great suggestion. I've added it. –  Apr 27 '20 at 07:03