读书人

新手C++有关问题求解释

发布时间: 2013-01-11 11:57:35 作者: rapoo

新手C++问题求解释?
#include<iostream>
using namespace std;
class Three{
public:
Three(int I1,int I2,int I3);
void print();
friend Three operator++(Three &);
friend Three operator++(Three &,int);
private:
int i1,i2,i3;
};
Three::Three(int I1,int I2,int I3){
i1=I1;
i2=I2;
i3=I3;
}
void Three::print(){
cout<<"i1: "<<i1<<"i2: "<<i2<<"i3: "<<i3<<endl;
}
Three operator++(Three &op,int){
op.i1++;
op.i2++;
op.i3++;
return op;
}
int main(){
Three obj1(4,5,6),obj2(14,15,16);
obj1.print();
++obj1;
obj1.print();
obj1++;
obj1.print();
cout<<endl;
obj2.print();
operator++(obj2);
obj2.print();
operator++(obj2,0);
obj2.print();
return 0;

}

编译:-Configuration: test - Win32 Debug--------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "class Three __cdecl operator++(class Three &)" (??E@YA?AVThree@@AAV0@@Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

test.exe - 2 error(s), 0 warning(s)

请问这是什么问题啊??求解释,谢谢。。 C++问题
[解决办法]
friend Three operator++(Three &); 这个函数没有函数实现。
在此处operator++(obj2);调用
[解决办法]
friend Three operator++(Three &);
应该你的这个函数没有实现
多说点,你的这两个函数都是返回的对象本身吧,你这样子的返回值是不能做左值的了。建议最好Three& 返回一个引用更好,还有有关i++和++i的区别的请看我的一篇博文:http://blog.csdn.net/zlhy_/article/details/8349300
对你的实现也许有帮助吧

读书人网 >C++

热点推荐