![]() |
| | #1 |
| 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;
}
__________________ 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 | |
| | #2 |
| Skunkmeister 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 | |
| | #3 |
| Tha 1 Sick RAT Join Date: Dec 2003
Posts: 267
| 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 | |
| | #4 |
| Skunkmeister 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 | |
| | #5 |
| Skunkmeister 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 | |
| | #6 |
| Skunkmeister 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 | |
| | #7 |
| Registered User 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
|
| Codeplug is online now | |
| | #8 |
| 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
__________________ A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside |
| WDT is offline | |
| | #9 | |
| Registered User Join Date: Mar 2003
Posts: 3,902
| Quote:
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 | |
| | #10 |
| 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |