multipy and add( main) not declared ?

Hello!

Please, can someone help me see th error?
Class is retyped from the net. It has no members that are NOT functions. Is that ok?

I wrote main, but both functions say "not delared in this scope" ! Please, does someone see the error faster than me? Many thanks, I am in a hurry!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class calc
{
  public:
    int multiply(int x, int y);
    int add(int x, int y);
 };
int calc::multiply(int x, int y)
{
  return x*y;
}
int calc::add(int x, int y)
{
  return x+y;
}

int main(){
multiply(3,4);
add(1,2);
retun 0;
}
Last edited on
You declared the member functions at class named calc scope.

and you have to declare the object to invoke its member functions.

Or you can declare them with static:

public:
static int multiply(int x,int y);

And invoking with calc::

calc::multiply(3,4);

Read the books for Function chap and Member function (Probably at class chap)
Last edited on
This program is NOT WORKING, compiler answers that add and multiply ARE NOT DECLARED.

I do not see on eyes that they are NOT DECLARED.
Anyway, becaue of them being not declared, the program is not working and so I am confused.

Did someone try on its ovn compiler and which one?

Many thanks!!
The add and multipy functions are members of the calc class (why?) so to call these functions you first have to create a calc object.

1
2
3
calc obj;
obj.multiply(3,4);
obj.add(1,2);
Last edited on
The example on the net has no non-function members.
Is that ok, or is that just short way of writting?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int multiply(int x, int y)
{
  return x*y;
}
int add(int x, int y)
{
  return x+y;
}

int main()
{
	multiply(3,4);
	add(1,2);
	return 0;
}

So this is what you want , huh?

And you actually didn't declare at global scope , you ACTUALLY DECLARED AN MEMBER FUNCTION at CLASS SCOPE
Last edited on
I "added" object declarations in main, then it worked.
Is that the answer?

How would look the same code if I declared the function at the global scope, so that it can be reached in main?

Is THIS a solution,or NOT, and why?
many thanks!!!

http://codepad.org/ve39CoPf
Topic archived. No new replies allowed.