C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-02-2004, 10:09 AM   #1
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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??
__________________
A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Last edited by WDT; 01-02-2004 at 10:15 AM.
WDT is offline   Reply With Quote
Old 01-02-2004, 10:15 AM   #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
Stoned_Coder is offline   Reply With Quote
Old 01-02-2004, 10:42 AM   #3
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Old 01-02-2004, 10:58 AM   #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
Stoned_Coder is offline   Reply With Quote
Old 01-02-2004, 11:01 AM   #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
Stoned_Coder is offline   Reply With Quote
Old 01-02-2004, 11:07 AM   #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);
}
__________________
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

Last edited by Stoned_Coder; 01-02-2004 at 11:12 AM.
Stoned_Coder is offline   Reply With Quote
Old 01-02-2004, 12:42 PM   #7
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,902
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
Codeplug is online now   Reply With Quote
Old 01-04-2004, 09:09 AM   #8
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Old 01-04-2004, 11:53 AM   #9
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,902
Quote:
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
Codeplug is online now   Reply With Quote
Old 01-04-2004, 01:07 PM   #10
WDT
Tha 1 Sick RAT
 
Join Date: Dec 2003
Posts: 267
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
WDT is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:03 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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