1

This is my source file written in 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++;
}

From copy constructor and from overloaded = operator function I am trying to invoke copy() function. It gives me the following error:

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 

For some reasons at copy method it expects a pointer. Why is that? This is my header file:

#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 */

This is main method:

#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;
}

What might be the problem?

ucas
  • 417
  • 1
  • 11
  • 30
  • 1
    Please note that your assignment operator leaks memory. The assignment operator is always called on an object that has already allocated memory for itself, so the `log` pointer points to something. Your `copy` function overwrites that member without first freeing what it pointed to before. If assignment and copy construction were really the same thing, as your implementation indicates, then we wouldn't have needed both methods in the first place. – Rob Kennedy Nov 15 '11 at 17:16
  • Nothing to do with your question (which others have already answered, but you do realize that your `copy` function will result in an incoherent object if the dynamic allocation fails, and that it leaks memory like crazy. – James Kanze Nov 15 '11 at 17:18

2 Answers2

9

Because you are passing a pointer:

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

[Sidenote 1: Unless you have a very good reason, your copy constructor, etc. should prefer to take a const ref over a non-const ref.]

[Sidenote 2: See this question for the idiomatic way to implement copy-constructor and copy-assignment operators in C++.]

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

The compiler says this:

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&)

You conclude from that error that the "copy method ... expects a pointer," but you're wrong. The copy method expects a reference. We know that because of the list of "candidates" given. You're trying to call it with a pointer, and the compiler says there's no match for a copy function that takes a pointer. So don't pass it a pointer. Pass it the reference you already have.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467