multipy and add( main) not declared ?

Apr 23, 2015 at 9:46am
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 Apr 23, 2015 at 9:46am
Apr 23, 2015 at 9:59am
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 Apr 23, 2015 at 12:22pm
Apr 23, 2015 at 11:33am
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!!
Apr 23, 2015 at 11:38am
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 Apr 23, 2015 at 11:40am
Apr 23, 2015 at 11:53am
The example on the net has no non-function members.
Is that ok, or is that just short way of writting?

Apr 23, 2015 at 12:16pm
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 Apr 23, 2015 at 12:18pm
Apr 23, 2015 at 1:05pm
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.