I am trying to write a C++ Program, but am greatly struggling with the copy assignment section. Here's my code:
#include <iostream>
using namespace std;
class CarCounter {
public:
CarCounter();
CarCounter& operator=(const CarCounter& objToCopy);
void SetCarCount(const int setVal) {
carCount = setVal;
}
int GetCarCount() const {
return carCount;
}
private:
int carCount;
};
CarCounter::CarCounter() {
carCount = 0;
}
// FIXME write copy assignment operator
/* Your solution goes here */
CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
CarCounter nobj;
cout << objToCopy.carCount << endl;
nobj.carCount = objToCopy.carCount;
cout << nobj.carCount << endl;
cout << *this << endl;
return *this;
}
int main() {
CarCounter frontParkingLot;
CarCounter backParkingLot;
frontParkingLot.SetCarCount(12);
backParkingLot = frontParkingLot;
cout << "Cars counted: " << backParkingLot.GetCarCount();
return 0;
}
And the output is:
main.cpp: In member function ‘CarCounter& CarCounter::operator=(const CarCounter&)’: main.cpp:30:9: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘CarCounter’) cout << *this << endl; ~~~~~^~~~~~~~
I know it's probably an obvious rookie mistake, but I'm new to C++. I appreciate all the help I can get. Thanks!
Aucun commentaire:
Enregistrer un commentaire