CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2011
    Posts
    10

    Question 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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Problem with composition

    Quote Originally Posted by depecheSoul View Post
    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

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem with composition

    What D_Drmmr said

    Quote Originally Posted by D_Drmmr View Post
    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.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Problem with composition

    Quote Originally Posted by monarch_dodra View Post
    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

  6. #6
    Join Date
    Dec 2011
    Posts
    10

    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
  •  





Click Here to Expand Forum to Full Width

Featured