Thread: using UML diagram to write a header

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:46 AM