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
|
|
|
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..
|