-6

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!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
jBit
  • 41
  • 1
  • 9
  • 4
    What do you expect `cout << *this` to do? Why do you have output statements in an assignment operator in the first place? – interjay Jul 25 '18 at 23:36
  • He's most likely using those for debugging purposes... I think. – MPops Jul 25 '18 at 23:50
  • 2
    Semi-related: `CarCounter` meets the requirements for [the Rule of Zero](https://en.cppreference.com/w/cpp/language/rule_of_three). There is no need for an assignment operator. You can leave it out or declare it in the class as `CarCounter& operator=(const CarCounter& objToCopy) = default;` to make it clear that the class uses the default assignment logic. – user4581301 Jul 26 '18 at 00:02
  • How is the compiler supposed to know how to print a `CarCounter`? If you want to be able directly use the `<<` operator with a `CarCounter`, you're going to need to tell the compiler how you want the object to be printed by overloading the operator for your class. – eesiraed Jul 26 '18 at 00:03
  • Please give your question a more descriptive title. Almost all questions are about how to fix an issue, so that's useless. – Barmar Jul 26 '18 at 01:06
  • Sorry about the nondescript title; didn't quite know what to name it. Yes, I was trying to use cout for debugging (working on an assignment in a lousy web-ide and can't debug). – jBit Jul 26 '18 at 01:39
  • Possible duplicate of [How can I use cout << myclass](https://stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass) – eesiraed Jul 26 '18 at 03:31

2 Answers2

1

You can't pass your custom class to cout. The error is basically saying that your class CarCounter is incompatible to be piped to cout. It seems like that line is completely unneeded and can be removed.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Thank you for clarifying. I marked the other one as the answer, but this actually explained why. – jBit Jul 26 '18 at 01:40
1

Your copy assignment operator should look like this:

CarCounter& CarCounter::operator=(const CarCounter& objToCopy) {
   carCount = objToCopy.carCount;
   return *this;
}
lenik
  • 23,228
  • 4
  • 34
  • 43