调用复制方法
Invoking copy method
这是我用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
由于某些原因,在copy方法中它需要一个指针。为什么呢?这是我的头文件
#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;
}
可能是什么问题?
因为你传递的是一个指针:
Lecture::Lecture(Lecture& orig) {
copy(&orig); // The & here is taking the address of orig (so remove it!)
}
[旁注1:除非你有很好的理由,否则你的复制构造函数等应该更倾向于使用const
ref而不是非const
ref.]
编译器这样说:
<>之前在复制构造函数' Lecture::Lecture(Lecture&)'中:function function function function function function function function function function function function function function function function function function functionLecture.hpp:21:注:候选人为:void Lecture::copy(Lecture&)之前你从这个错误中得出结论,"copy方法…期待一个指示,但你错了。复制方法需要一个引用。我们知道这一点是因为给出的"候选人"名单。你试图用一个指针来调用它,编译器说没有匹配的copy
函数接受一个指针。所以不要给它传递一个指针。
相关文章:
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 复制几乎为空的数组的最快方法
- 是否可以将不可复制的成员用作使对象不可复制的替代方法?
- 使用 memcpy 将矢量数据复制到 wstring 的正确方法
- C++将所有元素从矢量复制到地图/unordered_map的最佳方法
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- 将 std:set<int32_t> 复制到 std::set <uint32_t>的好方法
- 使变量不可复制的紧凑方法
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- 有没有一种方法可以在不复制数据的情况下从string_view创建字符串流
- 运算符重载C++、(+、-、*、/ 等)有没有比复制、替换和粘贴更聪明的方法?
- 是否有任何常规方法可以通知 STL 移动和复制构造函数?
- 尝试使用 Qt 库中的 QPixmap 将图像拆分为多个块。关于他的复制方法的工作方式,我有什么不明白的吗?
- 我可以制作一个对象方法,如果单独调用,它将自行修改,但如果在复制初始化期间调用,则会返回一个新对象?
- 我正在考虑为"A"类创建复制构造函数,但我不确定采用哪种方法
- 复制构造函数并从方法中获取数据
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 如何通过派生类的方法复制来设置基类字段