using UML diagram to write a header

This is a discussion on using UML diagram to write a header within the C++ Programming forums, part of the General Programming Boards category; This is the diagram: Integer -number: int +Integer(): +Integer(num: int): +~Integer(): +getInt(): int +operator<(Integer): bool +operator++(): int +operator--(): int Here ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    using UML diagram to write a header

    This is the diagram:

    Integer
    -number: int
    +Integer():
    +Integer(num: int):
    +~Integer():
    +getInt(): int
    +operator<(Integer): bool
    +operator++(): int
    +operator--(): int

    Here is my code, prob. not quite right but was a little confused on a few lines and the book had about 2 pages on UML which did not help me much. Some of these lines I'm not sure are right but I tried what I could from reading the book. For example the operators. If I could just get some input or maybe some help on what I need to do. Thanks

    Code:
    #ifndef INTEGER_H
    #define INTEGER_H
    
    class Integer
    {
    private:
    	int number;
    
    public:
    	Integer();     // Constructor
    	Integer(int num)
    	~Integer();  // Destructor
    	int getInt();
    	bool operator < (int Integer &);
    	int operator ++ (const Integer &);
    	int operator -- (const Integer &);
    
    };
    #endif

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe the UML wants:
    Code:
    bool operator < (Integer& rhs);
    instead of:
    Code:
    bool operator < (int Integer &);

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    rhs? and what about the operators ++ and -- . I keep getting errors "error C2675: unary '--' : 'Integer' does not define this operator or a conversion to a type acceptable to the predefined operator" for each <, ++ , and -- the same error ?
    Last edited by dukebdx12; 06-18-2008 at 10:32 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Your overload of operator++ and operator-- should take no arguments (prefix version), and/or a dummy int argument (postfix version). From the UML diagram, the former is the case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    ok thanks. I fixed those errors. Now I am getting 4 errors and they are all dealing with the bool line. Not sure what is wrong because I wrote it how I thought the diagram showed. And cpjust line of code did not help.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Now I am getting 4 errors and they are all dealing with the bool line. Not sure what is wrong because I wrote it how I thought the diagram showed.
    Post your current code and the error messages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dukebdx12 View Post
    rhs? and what about the operators ++ and -- . I keep getting errors "error C2675: unary '--' : 'Integer' does not define this operator or a conversion to a type acceptable to the predefined operator" for each <, ++ , and -- the same error ?
    rhs is short for Right Hand Side (which is probably the most common name for the argument on the right side of a == or < or + ... operator.)

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    my code:

    Code:
    #ifndef INTEGER_H
    #define INTEGER_H
    
    class Integer
    {
    private:
    	int number;
    
    public:
    	Integer();   // Constructor
    	Integer(int num);
    	~Integer();  // Destructor
    	int getInt();
    	bool operator < (Integer);
    	int operator ++ ();
    	int operator -- ();
    
    };
    #endif
    and the errors messages:

    Code:
    1>c: : error C2784: 'bool std::operator >(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2244) : see declaration of 'std::operator >'
    1> : error C2784: 'bool std::operator >(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2057) : see declaration of 'std::operator >'
    1>: error C2784: 'bool std::operator >(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(93) : see declaration of 'std::operator >'
    1>: error C2676: binary '>' : 'Integer' does not define this operator or a conversion to a type acceptable to the predefined operator

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dukebdx12 View Post
    my code:

    Code:
    #ifndef INTEGER_H
    #define INTEGER_H
    
    class Integer
    {
    private:
    	int number;
    
    public:
    	Integer();   // Constructor
    	Integer(int num);
    	~Integer();  // Destructor
    	int getInt();
    	bool operator < (Integer);
    	int operator ++ ();
    	int operator -- ();
    
    };
    #endif
    and the errors messages:

    Code:
    1>c: : error C2784: 'bool std::operator >(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2244) : see declaration of 'std::operator >'
    1> : error C2784: 'bool std::operator >(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2057) : see declaration of 'std::operator >'
    1>: error C2784: 'bool std::operator >(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'Integer'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(93) : see declaration of 'std::operator >'
    1>: error C2676: binary '>' : 'Integer' does not define this operator or a conversion to a type acceptable to the predefined operator
    You try to use >, but you only define <.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Where do the errors come from? You have code that implements the class? And code that uses the class?

    By the way, note that getInt() and operator< should be const member functions since they, logically, do not modify the object's state. You can consider implementing operator< as a free function (a friend, if necessary).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21