Recommended Posts

Hey, heres a peice of code that wont compile the error i get is,

"'minus' was not declared in this scope"

// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }
int (*minus)(int,int) = subtraction;

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}

Minus should be global...shouldn't it?

If I change:

int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}

To:

int main ()
{
int m,n;
m = operation (7, 5, addition);
n = operation (20, m, subtraction);
cout <<n;
return 0;
}

obivouly it works but thats not the point >_< why isn't minus global?

btw i got the code from a tutorial located at

http://www.cplusplus.com/doc/tutorial/pointers.html

Link to post
Share on other sites

it looks like gcc is saying (code blocks and dev-c++ use minigw right which if I remember is gcc on windows)

that minus is already a funtion in the std namespace

this is the error I get

 also declared as `template<class _Tp> struct std::minus' here

so instead of using namespace std;

change it to using std::cout;

this way you won't pull in the entire namespace just to shortcut cout..

Link to post
Share on other sites
it looks like gcc is saying (code blocks and dev-c++ use minigw right which if I remember is gcc on windows)

that minus is already a funtion in the std namespace

Indeed. It's an STL (Standard Template Library) struct defining a functor (object that acts like a function).

so instead of using namespace std;

change it to using std::cout;

Or qualify the reference to minus in main as ::minus to indicate that you want the one in the global namespace.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...