errors reported using friend

This is a discussion on errors reported using friend within the C++ Programming forums, part of the General Programming Boards category; Neither of the following programs compile in MSVC++ v 6.0 but both compile, run, and give expected results in Dev-C++. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    errors reported using friend

    Neither of the following programs compile in MSVC++ v 6.0 but both compile, run, and give expected results in Dev-C++. Any explanation for the descrepency? Any suggested corrections to get it to compile with MSVC++?
    Code:
    #include <iostream> 
    using namespace std; 
     
    class ClassType 
    { 
      public: 
        int f; 
        int s; 
        friend ostream & operator<<(ostream &, ClassType &); 
    }; 
     
    ostream & operator<<(ostream & os, ClassType & ct) 
    { 
      os << ct.f << ' ' << ct.s << endl; 
      return os;  
    } 
     
    int main() 
    { 
       ClassType classType; 
       classType.f = 5; 
       classType.s = 89; 
       cout << classType << endl; 
    
       char ch;
       cin >> ch;
       return 0; 
    }
    error message:
    C:\Program Files\Microsoft Visual Studio\MyProjects\mySamples\mySamples.cpp(78) : error C2593: 'operator <<' is ambiguous
    Error executing cl.exe.

    and it points to this line in main():
    cout << classType << endl;

    Code:
    #include <iostream> 
    using namespace std; 
     
    class ClassType 
    { 
      public: 
        int f; 
        int s; 
        friend  ClassType operator+(ClassType &, ClassType &); 
        const ClassType & operator=(const ClassType & rhs); 
    }; 
     
    const ClassType & ClassType::operator =(const ClassType & rhs) 
    { 
      f = rhs.f; 
      s = rhs.s; 
    } 
     
     
    ClassType operator+(ClassType & lhs, ClassType & rhs) 
    { 
      ClassType temp; 
      temp.f = lhs.f + rhs.f; 
      temp.s = lhs.s + rhs.s; 
      return temp; 
    } 
     
    int main(int argc, char* argv[]) 
    { 
      ClassType ct1; 
      ct1.f = 5; 
      ct1.s = 89; 
      
      ClassType ct2; 
      ct2.f = 6; 
      ct2.s = 11; 
     
      ClassType ct3; 
      ct3 = ct1 + ct2; 
      cout << ct3.f << ' ' << ct3.s << endl; 
     
      char ch;
      cin >> ch;
      return 0; 
    }
    error message is:
    C:\Program Files\Microsoft Visual Studio\MyProjects\mySamples\mySamples.cpp(20) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1786)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

    and it relates to line:

    friend ClassType operator+(const ClassType &, const ClassType &);
    Last edited by elad; 09-23-2003 at 08:14 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Both example compile fine in dev-c++ except this line:
    Code:
    const ClassType & operator=(ClassType & rhs);
    needs to be const ClassType & for the parameter...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thanks for the confirmation that they run in Dev-C++. I've made the correction in the post. Thankyou.

    Now if I can figure out why they won't compile with my copy of MSVC++ v 6.0!

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The first compiles fine in VC++ 7.1, and the second needs a return value for operator=, but otherwise compiles fine.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I tested it with MSVC++ 6 and I get the same errors...seems to have a problem with having operator << declared in the class and having operator << defined for ClassType, but not being part of the class...The only way i got it to work was to remove your definition of operator << and put it inside the class:
    Code:
    class ClassType 
    { 
      public: 
        int f; 
        int s; 
        friend ostream & operator<<(ostream & os, ClassType & ct)
    {
    os << ct.f << ' ' << ct.s << endl; 
     return os;  
    } 
     /*
    ostream & operator<<(ostream & os, ClassType & ct) 
    { 
      os << ct.f << ' ' << ct.s << endl; 
      return os;  
    } 
    */
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thanks to you both!

    With the inline version I get the overloaded << to work, too. If that isn't one of the wierdest things I've come up against. Sheeeeeesh!

    Still get a fatal error with the overloaded + operator as a friend in the second program, even if I try to inline it like the "solution" to the first program and put in the return value for the = operator. I can make that operator a member function, but it would be nice to know what the compiler is up to so I can work around it somehow!

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Does it work if you forward declare the function? E.g. does this work:

    Code:
    #include <iostream> 
    using namespace std; 
    
    class ClassType;
    ostream & operator<<(ostream & os, ClassType & ct);
     
    class ClassType 
    { 
      public: 
        int f; 
        int s; 
        friend ostream & operator<<(ostream &, ClassType &); 
    }; 
     
    ostream & operator<<(ostream & os, ClassType & ct) 
    { 
      os << ct.f << ' ' << ct.s << endl; 
      return os;  
    } 
     
    int main() 
    { 
       ClassType classType; 
       classType.f = 5; 
       classType.s = 89; 
       cout << classType << endl; 
    
       char ch;
       cin >> ch;
       return 0; 
    }
    There is no reason that this should present a problem, it is a compiler error in VC++ 6. Prior to 7.1, VC++ was not a very decent compiler.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Seems to work Too bad...MSVC++ 6 is the best I can get for free (legally) Dev-Cpp is pretty darn good compiler though
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    my copy of Dev-C++ frequently throws an out of bounds error message if I don't do things exacly right, like type in the name of the index in a subscript operator, etc. Therefore I gave it up and started using VC++. Now I don't get the out of bounds error boxes that were so annoying, but I'm running into other problems instead. MSVC++ v 7.1 is the 2003 version of Visual Studio .NET is it not? I've been reluctant to go that route because of the added layer of complexity and proprietary(ness) that .NET brings, but maybe I'll have to bite the bullet and go for it.

    Anybody with VC 6 able to come up with a work around/explanation for the second program (the + operator overaloaded as a friend function)?

  10. #10
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Instead of taking VC++ 7.1, if you don't need .NET support, you should get ICL 7.1 which is more standard compliant and produces the fastest code on x86. The only problem is that I don't know if programs shall run under a processor other than Intel's ones but I guess they will if you compile in generic mode.
    At least, try it for the 30 days free trial period offered.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by elad
    my copy of Dev-C++ frequently throws an out of bounds error message if I don't do things exacly right, like type in the name of the index in a subscript operator, etc. Therefore I gave it up and started using VC++. Now I don't get the out of bounds error boxes that were so annoying, but I'm running into other problems instead. MSVC++ v 7.1 is the 2003 version of Visual Studio .NET is it not? I've been reluctant to go that route because of the added layer of complexity and proprietary(ness) that .NET brings, but maybe I'll have to bite the bullet and go for it.

    Anybody with VC 6 able to come up with a work around/explanation for the second program (the + operator overaloaded as a friend function)?
    Nothing says you need to do .NET programming in VC++ .NET. It lets you do it if you want, it never forces you to.

    The .NET versions do not take AWAY any features/language support, they only add more. If you don't like the new stuff, don't use it.

    And VC++7.1 is, according to some statistics I've seen, the most standards-compliant compiler out there.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Maybe are you right, I don't actually have MSVC 7.1 and I've not seen benchmarks since the v7.0.

    [EDIT]May I ask a question? Is MSVC 7.1 really more standard-compliant than GCC 3.2? Because as far as I know (I don't know much on compilers that I don't use however), the only feature that is not supported by GCC is the export keyword.
    Last edited by lyx; 09-24-2003 at 09:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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