2011-11-15 62 views
1

這是我用C++編寫的源文件。調用複製方法

#include<string> 
#include<sstream> 
#include "Lecture.hpp" 
#include <iostream> 

using namespace std; 


Lecture::Lecture() { 
    capacity=5; 
    log = new int[capacity]; 
    used = 0; 
} 

/* 
* The following is copy constructor. 
*/ 
Lecture::Lecture(Lecture& orig) { 
    copy(&orig); 

} 

/* 
* This is an empty destructor 
*/ 
Lecture::~Lecture() { 
    // dereference dynamic memory 
} 

Lecture & Lecture:: operator=(Lecture & other){ 
    this->copy(&other); 
    return *this; 
} 
/* 
    * Copy method. 
    */ 
void Lecture::copy(Lecture &other){  
    if(&other != this){ 
     capacity = other.capacity; 
     log = new int[capacity]; 
     used = other.used; 
     for(int x = 0; x < used; x++){ 
      log[x]= other.log[x]; 
     } 
    } 
} 

string Lecture::getLogs() { 
    ostringstream ans; 
    ans << "["; 
    for (int i=0; i<used-1; ++i) { 
    ans << log[i] <<", "; 
    } 
    if (used>0) 
    ans << log[used-1] << "]"; 
    else 
    ans << "empty log]"; 
    return ans.str(); 
} 

void Lecture::addLogEntry(int b) { 
    if (used==capacity) { 
    capacity *= 2; 
    int* temp= new int[capacity]; 
    for (int i=0; i<used; ++i) { 
     temp[i]=log[i]; 
    } 
    delete[] log; 
    log=temp; 
    } 
    log[used]=b; 
    used++; 
} 

從複製構造函數和重載=運算符函數我試圖調用copy()函數。它給我以下錯誤:

Lecture.cpp: In copy constructor `Lecture::Lecture(Lecture&)': 
Lecture.cpp:26: error: no matching function for call to `Lecture::copy(Lecture*)' 
Lecture.hpp:21: note: candidates are: void Lecture::copy(Lecture&) 
Lecture.cpp: In member function `Lecture& Lecture::operator=(Lecture&)': 
Lecture.cpp:38: error: no matching function for call to `Lecture::copy(Lecture*)' 
Lecture.hpp:21: note: candidates are: void Lecture::copy(Lecture&) 
make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2' 
make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2' 
make[2]: *** [build/Debug/Cygwin-Windows/Lecture.o] Error 1 
make[1]: *** [.build-conf] Error 2 
make: *** [.build-impl] Error 2 

由於複製方法的某些原因,它期望一個指針。這是爲什麼? 這是我的頭文件:

#ifndef LECTURE_HPP 
#define LECTURE_HPP 

#include<string> 
using namespace std; 

class Lecture{ 

public : 
    Lecture();     // default constructor 
    Lecture(Lecture &other); // copy constructor 
    string getLogs(); 
    void addLogEntry(int); 
    void copy(Lecture &other); // copy method 
    ~Lecture();    // destructor 
    Lecture& operator=(Lecture& other); // overloading of '=' 

private: 
    int* log; 
    int used; 
    int capacity; 



}; 

#endif /* LECTURE_HPP */ 

這是主要的方法:

#include<iostream> 
#include<string> 
//#include "University.hpp" 
#include <cmath> 
#include <cstdlib> 



using namespace std; 

/* 
* 
*/ 
int main(int argc, char** argv) { 


    Lecture c1; 

    cout << "Information of c1: " <<c1.getLogs() << endl; 

    c1.addLogEntry(20); 

    cout << "Information of c1: " <<c1.getLogs() << endl; 

    Lecture c2=c1; 

    cout << "Information of c2: " <<c2.getLogs() << endl; 

    Lecture c3; 
    c3=c1; 

    cout << "Information of c3: " <<c3.getLogs() << endl; 

    c1.addLogEntry(-4); 
    c2.addLogEntry(10); 

    cout << "-----------------------------------------------"<< endl; 

    cout << "Information of c1: " <<c1.getLogs() << endl; 
    cout << "Information of c2: " <<c2.getLogs() << endl; 
    cout << "Information of c3: " <<c3.getLogs() << endl; 


    return 0; 
} 

可能是什麼問題?

+1

請注意您的賦值操作符會泄漏內存。賦值運算符總是在已經爲自己分配內存的對象上調用,所以'log'指針指向某個內容。你的'copy'函數會覆蓋那個成員,而不必先釋放它之前指向的內容。如果賦值和拷貝構造是真的一樣的東西,就像你的實現所表明的那樣,那麼我們就不會首先需要這兩種方法。 –

+0

無關,與你的問題(這人已經回答了,但你一定要明白,你的'copy'功能將導致不連貫的對象,如果動態分配失敗,而且它泄漏內存像瘋了似的。 –

回答

9

因爲你傳遞一個指針:

Lecture::Lecture(Lecture& orig) { 
    copy(&orig); // The & here is taking the address of orig (so remove it!) 
} 

[旁註1:除非你有一個很好的理由,你的拷貝構造函數等,應該更喜歡採取const裁判在非const參考。 ]

[旁註2:爲實現拷貝構造函數和複製C-賦值運算符++的慣用方式見this question]

+0

同樣與這個 - >在operator =中複製(&other) – Ben

0

編譯器這樣說:

 
Lecture.cpp: In copy constructor `Lecture::Lecture(Lecture&)': 
Lecture.cpp:26: error: no matching function for call to `Lecture::copy(Lecture*)' 
Lecture.hpp:21: note: candidates are: void Lecture::copy(Lecture&) 

您從錯誤中得出結論:「複製方法...期望指針」,但是你錯了。複製方法期望參考。我們知道這是因爲給出了「候選人」名單。你試圖用一個指針來調用它,編譯器說沒有匹配一個帶指針的copy函數。所以不要通過它指針。傳遞給你已有的參考。