由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - question overloading ++ error
相关主题
operator() overloading 一问为啥gcc找不到类的构造函数?
C++ operator = overloading用copy & swap有啥优点一个c++小问题
why use static function here?问个c++的template的问题
问个overloading new operator的问题string operator +
[C++] 入门级问题 increment and decrement operatorsmember and friend
请问关于overloading <<An object of A automatically converted to an object of B.
why copy assignment operator returns non-const type?ostream& operator << (ostream& s, int cnt) error
为什么在overloading中,friend <<不能读取private值呢?一个inheritance 的问题
相关话题的讨论汇总
话题: upint话题: const话题: error话题: operator话题: gcc
进入Programming版参与讨论
1 (共1页)
j***i
发帖数: 1278
1
I define the class UPint and overload ++ operator.
In the main function c=a++ will have a error but c=++a is fine , anybody
know the reason?
int main()
{ UPint a(1);
UPint c;
c=a++// this will produce a error
// error: no matching function for call
// to ‘UPint::UPint(const UPint)’
c=++a// fine
return 0
}
//////////////////
class UPint
{
public:
UPint(int x=0): value(x){};
UPint(UPint &rhs):value(rhs.value){};
UPint& operator=(const UPint &rhs);
UPint& op
o****b
发帖数: 31
2
works fine in my machine
t****t
发帖数: 6806
3
i think the error message says all: you need a constructor with signature
UPint::UPint(const UPint&)

【在 j***i 的大作中提到】
: I define the class UPint and overload ++ operator.
: In the main function c=a++ will have a error but c=++a is fine , anybody
: know the reason?
: int main()
: { UPint a(1);
: UPint c;
: c=a++// this will produce a error
: // error: no matching function for call
: // to ‘UPint::UPint(const UPint)’
: c=++a// fine

j***i
发帖数: 1278
4
这样是的,但我不知道为什么,
我想可能是 ++ return by value 要用copy constructor

是我还是奇怪
c=a++ 是错的,但要是拆开
a++
c=a 没有错,我想是不是和alignment =有关

【在 t****t 的大作中提到】
: i think the error message says all: you need a constructor with signature
: UPint::UPint(const UPint&)

j***i
发帖数: 1278
5
怪事
我在vc2008 重编了,没有问题..
昨天linux是在gcc下编译的,

【在 o****b 的大作中提到】
: works fine in my machine
t****t
发帖数: 6806
6
because a++ returns const UPint, but (a) is UPint&
your copy constructor only accepts non-const version, which is obviously
wrong. in summary, you didn't add const wherever you can, so you can not use
your class as convenient as built-in types.
VC? forget it, that's not c++.

【在 j***i 的大作中提到】
: 这样是的,但我不知道为什么,
: 我想可能是 ++ return by value 要用copy constructor
: 但
: 是我还是奇怪
: c=a++ 是错的,但要是拆开
: a++
: c=a 没有错,我想是不是和alignment =有关

j***i
发帖数: 1278
7
谢谢,

为什么 a++ 单独写没有这个问题

use

【在 t****t 的大作中提到】
: because a++ returns const UPint, but (a) is UPint&
: your copy constructor only accepts non-const version, which is obviously
: wrong. in summary, you didn't add const wherever you can, so you can not use
: your class as convenient as built-in types.
: VC? forget it, that's not c++.

t****t
发帖数: 6806
8
i think originally you wrote
UPint c=a++;
this tries to call UPint::UPint(const UPint&) because a++ returns a const
UPint. but you only have UPint::UPint(UPint&). so this generates an error.
c=a++ itself should be fine since you do have UPint::operator=(const UPint&).

【在 j***i 的大作中提到】
: 谢谢,
: 那
: 为什么 a++ 单独写没有这个问题
:
: use

o****b
发帖数: 31
9
大牛说的对

use

【在 t****t 的大作中提到】
: because a++ returns const UPint, but (a) is UPint&
: your copy constructor only accepts non-const version, which is obviously
: wrong. in summary, you didn't add const wherever you can, so you can not use
: your class as convenient as built-in types.
: VC? forget it, that's not c++.

h*****0
发帖数: 4889
10
什么乱七八糟的……
你重载运算符不按规矩出牌。然后使用时还按“感觉”写。这不出错才怪了。
两个办法:
1. 不要重载运算符,因为你的习惯不合适。
2. 使用运算符时在旁边的注释里把运算符显式写成函数看对不对,如:
c = a.operator++()
c = a.operator++(int)
然后对比一下返回值就什么都清楚了。

【在 j***i 的大作中提到】
: I define the class UPint and overload ++ operator.
: In the main function c=a++ will have a error but c=++a is fine , anybody
: know the reason?
: int main()
: { UPint a(1);
: UPint c;
: c=a++// this will produce a error
: // error: no matching function for call
: // to ‘UPint::UPint(const UPint)’
: c=++a// fine

相关主题
请问关于overloading <<为啥gcc找不到类的构造函数?
why copy assignment operator returns non-const type?一个c++小问题
为什么在overloading中,friend <<不能读取private值呢?问个c++的template的问题
进入Programming版参与讨论
j***i
发帖数: 1278
11
Thanks for your explaination , it is reasonable.
however
I checked again,
I did not write UPint c a++;
actually, I define c first
it is like
UPint c;
c=a++;
it is still wrong
I using gcc in ubuntu

const
error.
UPint&).

【在 t****t 的大作中提到】
: i think originally you wrote
: UPint c=a++;
: this tries to call UPint::UPint(const UPint&) because a++ returns a const
: UPint. but you only have UPint::UPint(UPint&). so this generates an error.
: c=a++ itself should be fine since you do have UPint::operator=(const UPint&).

p***o
发帖数: 1252
12
Seems a bug of gcc ...
The following code is OK with gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3
but not gcc (GCC) 3.4.5 (mingw-vista special r3). So update your
gcc first.
class UPint
{
public:
UPint();
UPint(UPint &rhs);
UPint& operator=(const UPint &rhs);
UPint operator++(int);
};
int main()
{
UPint a, c;
c=a++;
return 0;
}

【在 j***i 的大作中提到】
: Thanks for your explaination , it is reasonable.
: however
: I checked again,
: I did not write UPint c a++;
: actually, I define c first
: it is like
: UPint c;
: c=a++;
: it is still wrong
: I using gcc in ubuntu

j***i
发帖数: 1278
13
My is gcc 4:4.2.3 lubuntu6 , it should works..
x*******u
发帖数: 2074
14
UPint c; 这里调的是default constructor
c=a++; 这里先调你overload的++operator,然后再调assignment operator, which tak
e 前面++ operator的返回值作为参数
你的问题跟++ operator无关, 真正原因前面thrust以及compiler的error msg讲得很清
楚了, 是你没有一个take const UPint的assignment operator(or constructor)
某些compiler通得过我觉得是因为它们提供了const UPint的constructor

【在 j***i 的大作中提到】
: Thanks for your explaination , it is reasonable.
: however
: I checked again,
: I did not write UPint c a++;
: actually, I define c first
: it is like
: UPint c;
: c=a++;
: it is still wrong
: I using gcc in ubuntu

p***o
发帖数: 1252
15
GCC 4.2 won't work. update to 4.3 and try.

【在 j***i 的大作中提到】
: My is gcc 4:4.2.3 lubuntu6 , it should works..
1 (共1页)
进入Programming版参与讨论
相关主题
一个inheritance 的问题[C++] 入门级问题 increment and decrement operators
one question about overloading operator delete请问关于overloading <<
这段code有啥问题?why copy assignment operator returns non-const type?
How to overload global new operator?为什么在overloading中,friend <<不能读取private值呢?
operator() overloading 一问为啥gcc找不到类的构造函数?
C++ operator = overloading用copy & swap有啥优点一个c++小问题
why use static function here?问个c++的template的问题
问个overloading new operator的问题string operator +
相关话题的讨论汇总
话题: upint话题: const话题: error话题: operator话题: gcc