0

So I have an abstract class Product with fields name and price. There are a few classes that inherit from Product, and the reason Product is abstract is because these subclasses have to implement this function (defined in Product):

 virtual std::string getCategory()=0;

Category is not a field, it just depends on which subclass we have and in some cases on the price.

Now I want an output operator for the subclasses of Product, but since I only want to print the name and price, I did this in Product.h:

 friend std::ostream& operator<<(std::ostream& os, const Product& secondOperand);

And this in Product.cpp:

 ostream& operator<<(ostream& outputStream, Product& secondOperand){
     outputStream << "["<<secondOperand.getName()<<" "<<secondOperand.getPrice()<<"]"<<endl;
     return outputStream;
 }

Now I get this error in visual studio:

Error C2259: 'Product' : cannot instantiate abstract class

I don't want to implement this output for every subclass (cause then I have to literally copy everything which isn't ideal). Also, I started out with Product being not pure virtual, but then I had Linker errors for the getCategory() functions...

  • possible duplicate of [C++ Abstract class operator overloading and interface enforcement question](http://stackoverflow.com/questions/2059058/c-abstract-class-operator-overloading-and-interface-enforcement-question) – David Nov 22 '14 at 13:34
  • 1
    *Where* do you get the error? Is it the complete error output? – Some programmer dude Nov 22 '14 at 13:35
  • 2
    You're showing `const Product&` for the declaration but `Product&` for the implementation. That mismatch might not compile. But furthermore, it makes me suspect you have a mistake elsewhere such that you're passing a `Product` by value thus making the compiler think you want to instantiate one and thus causing the compile error in question. – TheUndeadFish Nov 22 '14 at 13:40
  • Not a duplicate, since he wants a different output for his subclasses. The error is my only compiler error. It's at the first line of the body of the implementation of operator<< (in product.cpp). – harrymuana Nov 22 '14 at 13:42
  • There's no problem with the approach...Just paste the complete code – ravi Nov 22 '14 at 13:44
  • You do not pass Product by reference but by value. That means the program would have to generate a temporary copy of Product. which is not possible since Product is pure vortual. – Oncaphillis Nov 22 '14 at 13:44
  • @Oncaphillis What did you see in the code... – ravi Nov 22 '14 at 13:45
  • Ok -- I'm awfully wrong... – Oncaphillis Nov 22 '14 at 13:48
  • @TheUndeadFish I removed the const in the declaration, and now the error has changed to "Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) – harrymuana Nov 22 '14 at 13:55
  • @ravi I'm not sure on what else I can paste, there's a lot of code and I'm not really allowed to share much of it :/ – harrymuana Nov 22 '14 at 13:56
  • @TheUndeadFish Fixing the new error seemed as simple as including . Thanks a lot! – harrymuana Nov 22 '14 at 14:53

1 Answers1

0

Nothing wrong with the approach, here's a sample which compiles and runs....

#include <string>
#include <iostream>

using namespace std;

class Product
{
public:
    friend std::ostream& operator<<(std::ostream& os, const Product& secondOperand);
    virtual ~Product() = 0;

    string getName() { return "Product Name"; }
    string getPrice() {return "£1.00"; }
    virtual std::string getCategory()=0;
};

ostream& operator<<(ostream& outputStream, Product& secondOperand){
     outputStream << "["<<secondOperand.getName()<<" "<<secondOperand.getPrice()<<"]"<<endl;
     return outputStream;
 }

Product::~Product() {}

class DerivedProduct : public Product
{
public:
    DerivedProduct() {}
    std::string getCategory() { return "Derived getCategory()"; }
};

int main(int argc, char *argv[])
{
    DerivedProduct d;
    cout << d.getCategory() << endl;
    cout << d << endl;
    return 0;
}
Mark Toller
  • 106
  • 4
  • Thanks, this helped me to understand I was just looking at the wrong part! My error seems to be solved by including , as dumb as that may sound... – harrymuana Nov 22 '14 at 14:52