-
December 20th, 2011, 11:40 AM
#1
Problem with composition
Hello. I have made this program for practice reasons but I dont know what i have done wrong can you please tell me.
I always get the same mistake: dinamicka_alokacija_memorije.cxx:25:12: error: request for member ‘rezultat’ in ‘add’, which is of non-class type ‘addition(numbers)’
This is the program:
#include <iostream>
using namespace std;
class numbers
{
private:
int x,y;
public:
int getx() {return x;}
int gety() {return y;}
};
class addition
{
private:
numbers num;
public:
addition(numbers obj):num(obj) {}
int rezultat() {return (num.getx()+num.gety());}
};
int main()
{
addition add(numbers);
cout<<add.rezultat()<<endl;
}
Thank you very much
-
December 20th, 2011, 11:48 AM
#2
Re: Problem with composition
Originally Posted by depecheSoul
int main()
{
addition add(numbers);
cout<<add.rezultat()<<endl;
}
The bold line declares a function called 'add' that takes a numbers object as argument and returns an addition object. If you want to create a variable called add of type addition that is instantiated with a default constructed numbers object, I think you can do this:
Code:
int main()
{
addition add(numbers());
}
That said, I don't see the point of having a class that can only contain undefined values.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 20th, 2011, 11:50 AM
#3
Re: Problem with composition
The problem is that this declares a function named add that has a parameter of type numbers and returns an addition object:
Code:
addition add(numbers);
You should create a numbers object to pass to the constructor of the addition object named add. Furthermore, note that numbers does not have an appropriate constructor, for now.
-
December 20th, 2011, 11:53 AM
#4
Re: Problem with composition
What D_Drmmr said
Originally Posted by D_Drmmr
I think you can do this:
Code:
int main()
{
addition add(numbers());
}
Actually, that doesn't work either. You'd have to add an extra set of parenthesis:
Code:
int main()
{
addition add((numbers()));
cout<<add.rezultat()<<endl;
}
That said, I think you'd be better off with either a default parameter, or a named dummy:
Code:
int main()
{
numbers aNumber;
addition add(aNumber);
cout<<add.rezultat()<<endl;
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
December 20th, 2011, 12:26 PM
#5
Re: Problem with composition
Originally Posted by monarch_dodra
What D_Drmmr said
Actually, that doesn't work either. You'd have to add an extra set of parenthesis:
Yeah, that's what I was afraid of. Thanks for clearing it up.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 20th, 2011, 01:25 PM
#6
Re: Problem with composition
Thank you very much it worked with double ()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|