七行代码一个错误大家看看
#include <iostream>
using namespace std;
int operator+(int i)
{
return -i;
}
void main()
{
int a=12;
cout < <a+32;
}
我想把加号重载为减号
[解决办法]
运算符的重载只能用在用户自已定义的类型上,
也就是类类型,
例如
A& A::operator+(const A&a)
是可以通过的
而不能用在系统的内置类型上,
也就是说不能通过重载运算符来改变内置运算.
所以楼主不要进行这样的尝试了,
是不能通过的
[解决办法]
operator+
这东西,貌似不好单独列出来成为独立的函数的.不过,我也不肯定了.至少我没见过.
通常,operator都是用在class里的.像上面例子那样.
用来重载+,-,*,/等等.
[解决办法]
通过类当然可以,但是想通过楼主的
void main()
{
int a=12;
cout < <a+32;
}
是不可能的,
a是一个类的对象的情况和这并不相同
[解决办法]
#include <iostream>
using namespace std;
class A{};
int operator+(int i, A b)
{
return -i;
}
int main()
{
int a=12;
A bb;
int c;
c = a+bb;
cout < <c < <endl;
system( "PAUSE ");
return EXIT_SUCCESS;
}
--------------------------------------
这样就可以了.同志哥.
[解决办法]
void main()
{
int a=12;
cout < <a+32;
}
楼主是要求这个的结果是-20,
这是不可能实现的啦...