关于赋值操作符的重载: 这个程序为什么能在g++上编译通过?
2006-06-21 11:17
513 查看
// 一般认为gcc是学习C/C++的最佳编译器。可是下面这个程序在g++3.4.4上竟然可以编译通过。
#include <vector>
#include <iostream>
using namespace std;
class val_box
{
private:
int val;
public:
int get()
{
return val;
}
void set(int new_val)
{
val = new_val;
}
val_box& operator= (val_box& right) // 实际上, const 是必需的
{ // 赋值操作符的重载
val = right.val;
return *this;
}
};
class query
{
private:
vector<val_box> val_box_collection;
public:
void Insert(int val)
{
val_box temp_val_box;
temp_val_box.set(val);
val_box_collection.push_back(temp_val_box);
}
friend ostream& operator<< (ostream& os, query& right)
{
for(vector<val_box>::iterator iter = right.val_box_collection.begin();
iter != right.val_box_collection.end(); ++iter)
{
os << (*iter).get() << "/t";
}
return os;
}
};
int main()
{
query test_class;
test_class.Insert(1);
test_class.Insert(2);
test_class.Insert(3);
cout << test_class << endl;
cin.get();
return 0;
}
/*
vc2005的错误描述:
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const val_box' (or there is no acceptable conversion)
Digital Mars 的错误描述:
C:/dm/bin/../stlport/stlport/stl/_algobase.h(327) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
C:/dm/bin/../stlport/stlport/stl/_algobase.h(139) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
C:/dm/bin/../stlport/stlport/stl/_algobase.h(335) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
main.cpp:
--- errorlevel 1
*/
#include <vector>
#include <iostream>
using namespace std;
class val_box
{
private:
int val;
public:
int get()
{
return val;
}
void set(int new_val)
{
val = new_val;
}
val_box& operator= (val_box& right) // 实际上, const 是必需的
{ // 赋值操作符的重载
val = right.val;
return *this;
}
};
class query
{
private:
vector<val_box> val_box_collection;
public:
void Insert(int val)
{
val_box temp_val_box;
temp_val_box.set(val);
val_box_collection.push_back(temp_val_box);
}
friend ostream& operator<< (ostream& os, query& right)
{
for(vector<val_box>::iterator iter = right.val_box_collection.begin();
iter != right.val_box_collection.end(); ++iter)
{
os << (*iter).get() << "/t";
}
return os;
}
};
int main()
{
query test_class;
test_class.Insert(1);
test_class.Insert(2);
test_class.Insert(3);
cout << test_class << endl;
cin.get();
return 0;
}
/*
vc2005的错误描述:
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const val_box' (or there is no acceptable conversion)
Digital Mars 的错误描述:
C:/dm/bin/../stlport/stlport/stl/_algobase.h(327) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
C:/dm/bin/../stlport/stlport/stl/_algobase.h(139) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
C:/dm/bin/../stlport/stlport/stl/_algobase.h(335) : Error: need explicit cast for function parameter 1 to get
from: const val_box*
to : val_box*
main.cpp:
--- errorlevel 1
*/
相关文章推荐
- 关于用g++编译后运行时出现的问题:无法定位程序输入点__gxx_personality_v0
- 在C++ 程序中调用被 C 编译器编译后的函数,为什么要加 extern
- redis是个单线程的程序,为什么会这么快呢?每秒10000?这个有点不解,具体是快在哪里呢?EPOLL?内存?
- Android 关于图片文件夹后缀错误,使应用在源码下编译通过却无法运行的错误
- 为什么TheBeerHouse的网站可以VS2005下正常编译通过?
- Makefile之关于程序的编译和链接
- 【闲聊】关于为什么会有这个博客
- c++程序调用被C编译器编译后的函数为什么要加extern "C"
- 通过例子来说明Java程序的编译执行过程
- 关于在eclipse中编译后的java小程序如何在命令行窗口运行的说明
- g++ 编译 c++ 程序时需要包括 include .h 文件,还是需要 .cpp 文件
- 关于Basic程序解释器及编译原理的简单化(1)---Basic器的语法分析及主要代码
- 编写多线程程序,模拟多个人通过一个山洞。这个山洞每次只能通过一个人,每个人通过山洞的时间为2秒(sleep)。随机生成10个人,都要通过此山洞,用随机值对应的字符串表示人名,打印输出
- 关于VC编译的程序在其他电脑上无法运行的解决办法
- PCLint 帮助中关于如何获得gcc/g++编译宏定义和头文件搜索目录的方法说明
- 关于keil编译cortex-m3纯汇编时为什么问题使用align地址问题
- 在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”?
- 关于自定义控件,可以编译通过,但是用时提示无法创建新实例。
- 关于程序的编译和链接
- 关于如何通过域名直接访问程序的项目