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

来源:百度知道 编辑:UC知道 时间:2024/06/07 02:07:09
同一段代码在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<

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

你给那个程序我编译都没有通过。。。

改成这样就行了:
# 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&q