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

    [RESOLVED] at a loss with overloading opperators

    Code:
    friend ostream& operator<<(ostream& ostm, const Mixed& m);
    	friend istream& operator>>(istream& istm, Mixed& m);
    	friend bool operator<(const Mixed& lhs, const Mixed& rhs);
    	friend bool operator>(const Mixed& lhs, const Mixed& rhs);
    	friend bool operator<=(const Mixed& lhs, const Mixed& rhs);
    	friend bool operator>=(const Mixed& lhs, const Mixed& rhs);
    	friend bool operator==(const Mixed& lhs, const Mixed& rhs);
    	friend bool operator!=(const Mixed& lhs, const Mixed& rhs);
        friend Mixed operator+(const Mixed& lhs, const Mixed& rhs);
        friend Mixed operator-(const Mixed& lhs, const Mixed& rhs);
        friend Mixed operator*(const Mixed& lhs, const Mixed& rhs);	 
        friend Mixed operator/(const Mixed& lhs, const Mixed& rhs);	 
        friend Mixed operator++(Mixed& m, int);
        friend Mixed operator++(Mixed& m);
        friend Mixed operator--(Mixed& m, int);
        friend Mixed operator--(Mixed& m);
    i keep getting errors with all 6 with bool when i dont comment them out in the driver if you need my .h and 2 .cpp files let me know! thank you!
    Code:
     1>main.obj : error LNK2019: unresolved external symbol "bool __cdecl operator<(class Mixed const &,class Mixed const &)" (??M@YA_NABVMixed@@0@Z) referenced in function _main
    1>c:\users\austin\documents\visual studio 2010\Projects\finalff\Debug\finalff.exe : fatal error LNK1120: 1 unresolved externals

    more info
    Code:
    // demonstrate comparison overloads
      if (x < y)	cout << "(x < y) is TRUE\n";
      if (x > y)	cout << "(x > y) is TRUE\n";
      if (x <= y)	cout << "(x <= y) is TRUE\n";
      if (x >= y)	cout << "(x >= y) is TRUE\n";
      if (x == y)	cout << "(x == y) is TRUE\n";
      if (x != y)	cout << "(x != y) is TRUE\n";
    Last edited by austinm6; February 14th, 2012 at 01:42 AM.

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

    Re: at a loss with overloading opperators

    Perhaps you forgot to compile and link the source file containing their definitions, or perhaps their definitions don't match the declarations.
    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

  3. #3
    Join Date
    Nov 2011
    Posts
    27

    Re: at a loss with overloading opperators

    Quote Originally Posted by laserlight View Post
    Perhaps you forgot to compile and link the source file containing their definitions, or perhaps their definitions don't match the declarations.
    Everything is there and looks good to the eye, atleast for a novice like me.

    @laser - sent a message let me know! thanks!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: at a loss with overloading opperators

    Quote Originally Posted by austinm6 View Post
    Everything is there and looks good to the eye, atleast for a novice like me.
    Perhaps it looks good to your eye but not for linker...
    And we do NOT see the implementation of
    Code:
    friend bool operator<(const Mixed& lhs, const Mixed& rhs);
    either.
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: at a loss with overloading opperators

    Quote Originally Posted by austinm6 View Post
    Everything is there and looks good to the eye, atleast for a novice like me.
    The linker doesn't lie. You are missing that implementation that the linker is looking for.The key word is implementation. Just a declaration is not enough.

    As to "your eye" -- does everything look good in this example?
    Code:
    void foo();
    
    int main()
    {
       foo();
    }
    This compiles, but the linker will give you the exact same error. So what is missing here? It is the implementation of the foo() function.

    Even though you see a declaration of foo(), all that declaration does is make the compiler happy when you call the function. It is the linker that now needs to know where the "guts" of foo() exists. It isn't found, so the linker gives you an error.

    This in a nutshell is what the linker is telling you. Also since C++ allows function overloading, possibly you believe you've implemented your operator function, but you didn't really implement it.

    Example:
    Code:
    void foo();
    void foo(int);
    
    int main()
    {
       foo(6);
    }
    
    void foo()
    { }
    Again, this compiles, and it looks like it should work, but look which foo() you're calling -- you're calling the one that takes an int argument, and that version of foo(int) was not implemented. Only the no-argument of foo was implemented.

    So either you never implemented them, or you didn't compile the module(s) where the implementations exist (and give these modules to the linker via your project).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 14th, 2012 at 04:47 AM.

  6. #6
    Join Date
    Nov 2011
    Posts
    27

    Talking Re: at a loss with overloading opperators

    Quote Originally Posted by Paul McKenzie View Post
    The linker doesn't lie. You are missing that implementation that the linker is looking for.The key word is implementation. Just a declaration is not enough.

    As to "your eye" -- does everything look good in this example?
    Code:
    void foo();
    
    int main()
    {
       foo();
    }
    This compiles, but the linker will give you the exact same error. So what is missing here? It is the implementation of the foo() function.

    Even though you see a declaration of foo(), all that declaration does is make the compiler happy when you call the function. It is the linker that now needs to know where the "guts" of foo() exists. It isn't found, so the linker gives you an error.

    This in a nutshell is what the linker is telling you. Also since C++ allows function overloading, possibly you believe you've implemented your operator function, but you didn't really implement it.

    Example:
    Code:
    void foo();
    void foo(int);
    
    int main()
    {
       foo(6);
    }
    
    void foo()
    { }
    Again, this compiles, and it looks like it should work, but look which foo() you're calling -- you're calling the one that takes an int argument, and that version of foo(int) was not implemented. Only the no-argument of foo was implemented.

    So either you never implemented them, or you didn't compile the module(s) where the implementations exist (and give these modules to the linker via your project).

    Regards,

    Paul McKenzie
    Just finished thanks to you!!! thank you so much, your advice really helped! 6:35 A.M time for zZzZ

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