为何同一段c++代码在VC++6.0上面能运行,而在VC8.0上面就不能运行了?

同一段代码在VC++6.0上面能编译并运行,而到vc++8.0上面就只能编译通过,而不能运行了!这是为什么??
以下代码在VC++8.0中能编译通过,可是却不能运行,这是为什么??
# include <iostream>
using namespace std;
template<class type>
class Sample
{
protected:
type Tn;
public:
Sample(type T)
{
Tn=T;
}
friend bool operator==(Sample&,Sample&);
void dispaly()const
{
cout<<Tn<<endl;
}
};
template<class type>
bool operator==(Sample<type>& s1,Sample<type>& s2)
{
if(s1.Tn==s2.Tn)
return true;
else return false;
}
void main(void)
{
Sample<int>S1(10),S2(20),S3(10);
cout<<"S1==";
S1.dispaly();
cout<<"S2==";
S2.dispaly();
cout<<"S3==";
S3.dispaly();
if(S1==S2) cout<<"S1==S2"<<endl;
else cout<<"S1!=S2"<<endl;
if(S1==S3) cout<<"S1==S3"<<endl;
else cout<<"S1!=S3"<<endl;

Sample<char>S4('a'),S5('b'),S6('a');
cout<<"S4==";
S4.dispaly();
cout<<"S5==";
S5.dispaly();
cout<<"S6==";
S6.dispaly();
if(S4==S5) cout<<"S4==S5"<<endl;
else cout<<"S4!=S5"<<endl;
if(S4==S6) cout<<"S4==S6"<<endl;
else cout<<"S4!=S6"<<endl;
}

VC++6.0跟标准C++差别太大了,vc++8.0向标准C++更靠近了,不过差别应该不大,你按照提示信息稍微修改下应该就好了

你给那个程序我编译都没有通过。。。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-24
改成这样就行了:
# include <iostream>
using namespace std;
template<class type>
class Sample
{
protected:
type Tn;
public:
Sample(type T)
{
Tn=T;
}
friend bool operator==(Sample& s1,Sample& s2)
{
if(s1.Tn==s2.Tn)
return true;
else return false;
}
void dispaly()const
{
cout<<Tn<<endl;
}
};
void main(void)
{
Sample<int>S1(10),S2(20),S3(10);
cout<<"S1==";
S1.dispaly();
cout<<"S2==";
S2.dispaly();
cout<<"S3==";
S3.dispaly();
if(S1==S2) cout<<"S1==S2"<<endl;
else cout<<"S1!=S2"<<endl;
if(S1==S3) cout<<"S1==S3"<<endl;
else cout<<"S1!=S3"<<endl;

Sample<char>S4('a'),S5('b'),S6('a');
cout<<"S4==";
S4.dispaly();
cout<<"S5==";
S5.dispaly();
cout<<"S6==";
S6.dispaly();
if(S4==S5) cout<<"S4==S5"<<endl;
else cout<<"S4!=S5"<<endl;
if(S4==S6) cout<<"S4==S6"<<endl;
else cout<<"S4!=S6"<<endl;
}
直接将运算符重载的定义放在声明的地方。

参考资料:http://zhidao.baidu.com/question/19781807.html

第2个回答  2009-05-24
能编译通过就能运行的
如果不能运行的话,重点考虑一下环境的问题。
相似回答