作者在 2008-07-10 17:23:24 发布以下内容
VC++6.0的BUG
今天继续看书中,在练习的时候发现了以下的问题:
#include<iostream>
using namespace std;
using namespace std;
enum sign{plus,minus};
class Currency{
friend ostream & operator<<(ostream &,const Currency &);
public:
Currency(sign s=plus,unsigned long d=0,unsigned int c=0);
~Currency(){}
friend ostream & operator<<(ostream &,const Currency &);
public:
Currency(sign s=plus,unsigned long d=0,unsigned int c=0);
~Currency(){}
bool Set(sign s,unsigned long d,unsigned int c);
bool Set(float a);
bool Set(float a);
sign Sign() const
{
if(amount<0) return minus;
else return plus;
}
unsigned long Dollars() const
{
if(amount<0) return (-amount)/100;
else return amount/100;
}
unsigned int Cents() const
{
if(amount<0)
return -amount-Dollars()*100;
else
return amount-Dollars()*100;
}
{
if(amount<0) return minus;
else return plus;
}
unsigned long Dollars() const
{
if(amount<0) return (-amount)/100;
else return amount/100;
}
unsigned int Cents() const
{
if(amount<0)
return -amount-Dollars()*100;
else
return amount-Dollars()*100;
}
Currency operator+(const Currency &x) const;
Currency& operator+=(const Currency &x)
{
amount+=x.amount;
return *this;
}
Currency& operator+=(const Currency &x)
{
amount+=x.amount;
return *this;
}
private:
long amount;
};
long amount;
};
Currency::Currency(sign s,unsigned long d,unsigned int c)
{
if(c>99)
{
//cerr<<"Cents should be<100"<<endl;
exit(1);
}
{
if(c>99)
{
//cerr<<"Cents should be<100"<<endl;
exit(1);
}
amount=d*100+c;
if(s==minus) amount=-amount;
}
if(s==minus) amount=-amount;
}
bool Currency::Set(sign s,unsigned long d,unsigned int c)
{
if(c>99) return false;
{
if(c>99) return false;
amount=d*100+c;
if(s==minus) amount=-amount;
return true;
}
if(s==minus) amount=-amount;
return true;
}
bool Currency::Set(float a)
{
sign sgn;
if(a<0) {sgn=minus;a=-a;}
else sgn=plus;
{
sign sgn;
if(a<0) {sgn=minus;a=-a;}
else sgn=plus;
amount=(a+0.001)*100;
if(sgn==minus) amount=-amount;
return true;
}
}
Currency Currency::operator+(const Currency &x) const
{
Currency y;
y.amount=amount+x.amount;
return y;
}
{
Currency y;
y.amount=amount+x.amount;
return y;
}
ostream & operator<<(ostream & out,const Currency &x)
{
long a=x.amount;
if(a<0) {out<<'-';a=-a;}
long d=a/100;
out<<'$'<<d<<'.';
int c=a-d*100;
if(c<10) out<<'0';
out<<c;
return out;
}
int main()
{
Currency g,h(plus,3,50),i,j;
{
Currency g,h(plus,3,50),i,j;
g.Set(minus,2,25);
i.Set(-6.45f);
i.Set(-6.45f);
j=h+g;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
i+=h;
cout<<i<<endl;
//print(i);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
j=i+g+h;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
j=(i+=g)+h;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
return 0;
}
}
--------------------Configuration: 005 - Win32 Debug--------------------
Compiling...
005.cpp
C:\Documents and Settings\Administrator\桌面\005\005.cpp(88) : error C2248: 'amount' : cannot access private member declared in class 'Currency'
C:\Documents and Settings\Administrator\桌面\005\005.cpp(41) : see declaration of 'amount'
C:\Documents and Settings\Administrator\桌面\005\005.cpp(108) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(112) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(116) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(120) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(122) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
Compiling...
005.cpp
C:\Documents and Settings\Administrator\桌面\005\005.cpp(88) : error C2248: 'amount' : cannot access private member declared in class 'Currency'
C:\Documents and Settings\Administrator\桌面\005\005.cpp(41) : see declaration of 'amount'
C:\Documents and Settings\Administrator\桌面\005\005.cpp(108) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(112) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(116) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(120) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\桌面\005\005.cpp(122) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
005.exe - 6 error(s), 0 warning(s)
这个问题很棘手,看了半天也没有明白到底是哪里出了问题。在网上搜索了一下,发现了类似的情况。这个问题是命名空间和友元的冲突,据说是VC++6.0编译器的BUG所导致的。
按照下面的方式修改的话,就能够通过编译了:
#include<iostream.h>
//using namespace std;
//using namespace std;
enum sign{plus,minus};
class Currency{
friend ostream & operator<<(ostream &,const Currency &);
public:
Currency(sign s=plus,unsigned long d=0,unsigned int c=0);
~Currency(){}
friend ostream & operator<<(ostream &,const Currency &);
public:
Currency(sign s=plus,unsigned long d=0,unsigned int c=0);
~Currency(){}
bool Set(sign s,unsigned long d,unsigned int c);
bool Set(float a);
bool Set(float a);
sign Sign() const
{
if(amount<0) return minus;
else return plus;
}
unsigned long Dollars() const
{
if(amount<0) return (-amount)/100;
else return amount/100;
}
unsigned int Cents() const
{
if(amount<0)
return -amount-Dollars()*100;
else
return amount-Dollars()*100;
}
{
if(amount<0) return minus;
else return plus;
}
unsigned long Dollars() const
{
if(amount<0) return (-amount)/100;
else return amount/100;
}
unsigned int Cents() const
{
if(amount<0)
return -amount-Dollars()*100;
else
return amount-Dollars()*100;
}
Currency operator+(const Currency &x) const;
Currency& operator+=(const Currency &x)
{
amount+=x.amount;
return *this;
}
Currency& operator+=(const Currency &x)
{
amount+=x.amount;
return *this;
}
private:
long amount;
};
long amount;
};
Currency::Currency(sign s,unsigned long d,unsigned int c)
{
if(c>99)
{
//cerr<<"Cents should be<100"<<endl;
//exit(1);
}
{
if(c>99)
{
//cerr<<"Cents should be<100"<<endl;
//exit(1);
}
amount=d*100+c;
if(s==minus) amount=-amount;
}
if(s==minus) amount=-amount;
}
bool Currency::Set(sign s,unsigned long d,unsigned int c)
{
if(c>99) return false;
{
if(c>99) return false;
amount=d*100+c;
if(s==minus) amount=-amount;
return true;
}
if(s==minus) amount=-amount;
return true;
}
bool Currency::Set(float a)
{
sign sgn;
if(a<0) {sgn=minus;a=-a;}
else sgn=plus;
{
sign sgn;
if(a<0) {sgn=minus;a=-a;}
else sgn=plus;
amount=(a+0.001)*100;
if(sgn==minus) amount=-amount;
return true;
}
}
Currency Currency::operator+(const Currency &x) const
{
Currency y;
y.amount=amount+x.amount;
return y;
}
{
Currency y;
y.amount=amount+x.amount;
return y;
}
ostream & operator<<(ostream & out,const Currency &x)
{
long a=x.amount;
if(a<0) {out<<'-';a=-a;}
long d=a/100;
out<<'$'<<d<<'.';
int c=a-d*100;
if(c<10) out<<'0';
out<<c;
return out;
}
int main()
{
Currency g,h(plus,3,50),i,j;
{
Currency g,h(plus,3,50),i,j;
g.Set(minus,2,25);
i.Set(-6.45f);
i.Set(-6.45f);
j=h+g;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
i+=h;
cout<<i<<endl;
//print(i);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
j=i+g+h;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
j=(i+=g)+h;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
cout<<j<<endl;
//print(j);cout<<endl;
cout<<i<<endl;
//print(i);cout<<endl;
return 0;
}
}