注册 登录
编程论坛 C++教室

请教一个拷贝构造函数

qong 发布于 2011-08-19 08:54, 743 次点击
程序代码:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Remo {
    public:
        Remo(int xx,int yy) {
            x=xx;
            y=yy;
        }
        Remo(const Remo& temp);
        int GetX() {
            return x;
        }
        int GetY() {
            return y;
        }
    private:
        int x;
        int y;
};

int main() {
    int x,y;
    Remo frist(1,2);
    Remo second=frist;
    x=second.GetX();
    y=second.GetY();
    cout << x << endl;
    cout << y << endl;
    return 0;
}                    /*不知道为什么会出错,编译的时候通过了,链接的时候怎么都通不过*/
        
9 回复
#2
lucky5635912011-08-19 09:52
应该是类中的const引用出了问题
#3
naruto012011-08-19 10:17
Remo(const Remo& temp);
没有定义么。~
改成
Remo(const Remo& temp) {
            this->x = temp.x;
            this->y = temp.y;
}

测试Ok~

ps:补充一个默认的构造函数Remo()更好吧。。

[ 本帖最后由 naruto01 于 2011-8-19 10:21 编辑 ]
#4
xinshou19912011-08-21 19:37
对!就像3楼说的,拷贝构造函数只声明了,没有定义!定义下就ok啦,还可以这样:
Remo(const Remo& temp) {
            x = temp.x;
            y = temp.y;
}
那个this指针还是不用的好(用不用都一样,this指针是“隐形”的,写出来只不过让它现形而已)。
#5
开了口的记忆2011-08-24 16:31
拷贝构造函数只声明了,没有定义定义下应该就可以吧。。。
#6
ljw9702432011-08-27 09:49
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Remo {
    public:
        Remo(int xx,int yy) {
            x=xx;
            y=yy;
        }
        Remo(const Remo& temp);
        int GetX() {
            return x;
        }
        int GetY() {
            return y;
        }
        //赋值运算符
        Remo &operator =(const Remo& temp);//要自己实现
    private:
        int x;
        int y;
};

int main() {
    int x,y;
    Remo frist(1,2);
    Remo second=frist;//这里用的是赋值运算符
    //Remo second(frist);//你定义的拷贝构造函数是这么用的
    x=second.GetX();
    y=second.GetY();
    cout << x << endl;
    cout << y << endl;
    return 0;
}

[ 本帖最后由 ljw970243 于 2011-8-27 09:50 编辑 ]
#7
jcw081201102011-08-27 11:29
程序代码:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Remo {
    public:
        Remo(int xx,int yy) {
            x=xx;
            y=yy;
        }
        Remo(const Remo& temp){};// 这里 你没有加{} 来实现这个函数 你只是定义了!
        int GetX() {
            return x;
        }
        int GetY() {
            return y;
        }
    private:
        int x;
        int y;
};

int main() {
    int x,y;
    Remo frist(1,2);
    Remo second=frist;
    x=second.GetX();
    y=second.GetY();
    cout << x << endl;
    cout << y << endl;
    return 0;
}
#8
开了口的记忆2011-09-02 10:58
定义就可以了
#9
lqsh2011-09-02 17:08
#include <iostream>
using namespace std;//统一命名空间

class Remo {
public:
    Remo(int xx=0,int yy=0):x(xx),y(yy){
    }//带可选参数构造函数,方便定义Remo对象数组
    Remo(const Remo& temp)
    {
        x=temp.x;
        y=temp.y;
    }//拷贝构造函数,数据成员没有涉及指针变量,不显式定义也可实现你的功能,c++中称此为浅拷贝。
    int GetX() {
        return x;
    }
    int GetY() {
        return y;
    }
private:
    int x;
    int y;
};

int main() {
    int x,y;
    Remo frist(1,2);
    Remo second=frist;
    x=second.GetX();
    y=second.GetY();
    cout << x << endl;
    cout << y << endl;
    return 0;
}                  
#10
wyane2011-09-04 12:29
正像ljw970243所说的,还应重载=号操作符
1