这是我用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构造函数和overloaded = operator函数我试图调用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;
}
可能是什么问题?
答案 0 :(得分:9)
因为你传递了一个指针:
Lecture::Lecture(Lecture& orig) {
copy(&orig); // The & here is taking the address of orig (so remove it!)
}
[旁注1:除非你有充分的理由,否则你的复制构造函数等应该优先考虑非const
参考的const
参考。
[旁注2:请参阅this question了解在C ++中实现复制构造函数和复制赋值运算符的惯用方法。]
答案 1 :(得分: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
函数。所以不要传递指针。传递你已经拥有的参考资料。