MSVC++ vs GCC compiler issue

This is a discussion on MSVC++ vs GCC compiler issue within the C++ Programming forums, part of the General Programming Boards category; Code: #inlcude <iostream> #include <cstring> using namepsace std; class tst{ char name[15]; int acode, pNum; public: tst(char *nm, int areacode, ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    MSVC++ vs GCC compiler issue

    Code:
    #inlcude <iostream>
    #include <cstring>
    using namepsace std;
    
    class tst{
        char name[15];
        int acode, pNum;
    public:
        tst(char *nm, int areacode, int num)
       {name = nm; acode=areacode; pNum=num;}
    friend ostream &operator<<(ostream &stream, tst obj);
    }
    
    ostream &operator<<(ostream &stream, tst obj);
    {
    stream<< "NAME: "<< obj.name <<".\nNUMBER: ";
    stream<< obj.acode <<'-'<<obj.pNum<<".\n\n";
    
    return stream;
    }
    
    int main()
    {
      tst a("Lan", 44208, 6946360);
      cout<<a<<endl;  //<< also MSVC finds this line ambiguos
    
    return 0;
    }
    Right that's the code written to experiment with custom stream inserters; the problem I have with the above code is that MSVC++ 6.0 on WinXP gives me errors whereas gcc doesn't on Linux redhat 8. The errors regard accessing the private members in the class from the definnition of the inserter. Any takers on why this is so??
    Last edited by WDT; 01-02-2004 at 09:15 AM.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    compiler bug!

    Seem to remember cure was to write func impl in the class dec itself as if writing inline member func.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Unhappy

    Is that my ONLY solution??!! I'm tempted to go back to *nix programming. Anyone know a good e-book or online site for GUI programmming in Unix/Linux for starters?
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well few people ever said that msvc6 was a good compiler. The problem disappears if you upgrade to msvc.net or .net2003
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    oh yeah btw your constructor has a nasty little bug in it
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    heres another fix for you that doesnt require you to upgrade compiler and really is how code should have been written in first place....
    Code:
    class A
    {
       public:
          virtual ostream& print(ostream& os) const
           { // do printing; return os;}
    };
    
    ostream& operator << (ostream& os, const A& a)
    {
       return a.print(os);
    }
    Last edited by Stoned_Coder; 01-02-2004 at 10:12 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    MSVC has plenty of bugs but that's not one of em (atleast in the form you've posted).
    You have at least 5 syntax errors along with the constructor problem that Stoned pointed out. You should post the actual code that won't compile next time.

    MSVC 6.0 compiles this just fine:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class tst
    {
        string m_name;
        int m_areacode, m_phonenum;
    
    public:
        tst(const char *name, int areacode, int phonenum)
            : m_name(name), m_areacode(areacode), m_phonenum(phonenum) {}
    
        friend ostream& operator<<(ostream &stream, const tst &obj);
    };//tst
    
    ostream& operator<<(ostream &stream, const tst &obj)
    {
        stream << "NAME:   " << obj.m_name.c_str() << endl
               << "NUMBER: " << '(' << obj.m_areacode << ')' 
                             << obj.m_phonenum << endl << endl;
        return stream;
    }//ostream << tst
    
    int main()
    {
        tst a("Lan", 555, 6785309);
        
        cout << a;
    
        return 0;
    }//main
    gg

  8. #8
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thanx you two.
    Did I mention g++ compiled it? Anyway I can't see any syntax errors with the constructors or the bug in it? Also I'm still learning C++ (the Standard way) so I'm following book examples Why pass the object and string as const & why can't I use C-string? (I'm still using c-string because I haven't got round to C++ string class)
    Also forgive my Noobness but what the hell does this line mean??
    Code:
    virtual ostream& print(ostream& os) const
    I don't understand the significance of the const keyword after the parentheses.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Originally posted by Codeplug
    MSVC has plenty of bugs but that's not one of em...
    I stand corrected.
    However that bug was fixed way back in SP3.
    You can download the latest service pack here, SP5.

    >> Anyway I can't see any syntax errors with the constructors or the bug in it?
    Syntax errors are everywhere else, the bug is "m_name(name)".

    >> Why pass the object ... as const &
    The reference prevents unnecessary calls to copy constructors, the "const" says: "even though this is a reference parameter, I'm not going to modify it".

    >> why can't I use C-string?
    You can. You're currently mis-using them though (see bug above).

    >> what the hell does this line mean??
    If you haven't gotten to the "virtual" keyword in your C++ studies, revisit this code when you have.

    >> the significance of the const keyword after the parentheses
    That means that class method will not modify any members of this when it is called.
    So if you have a pointer or reference to a const object, you can still call that object's const methods.

    gg

  10. #10
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thank you cp. You're quite helpful . The only problem I see with the constructor -now that you've pointed it out- is that I didn't strcpy() the value of *nm into name; 'part from that, that's it. I make this error quite a lot, hardly ever did when I was C-ing though . Also thanx. I have SP5 on disk just forgot to install it.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. online compiler for gcc
    By kris.c in forum Tech Board
    Replies: 2
    Last Post: 07-14-2006, 12:02 PM
  3. GCC (cygwin) much faster than MSVC, Borland?
    By Sargnagel in forum C Programming
    Replies: 15
    Last Post: 08-05-2003, 03:15 AM
  4. compiler issue
    By deebs1000 in forum C Programming
    Replies: 6
    Last Post: 10-30-2002, 07:12 PM
  5. Compiler Issue
    By spd_dmn in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2002, 12:29 PM

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