No match for operator error.

This is a code I am trying to run from C++ Primer by Lippman. However, I am getting this error in line 12. No match for operator in "std::cin>>item1"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include <iostream>
#include "Sales_item.h"

using namespace std;

int main()
{


    Sales_item item1, item2;
    std::cin>>item1>>item2;
    std::cout<<item1+item2<<std::endl;

    return 0;
Last edited on
Does Sales_item.h have an overload in the Sales_item class for the >> operator?
Thanks! I do not exactly how to check it. This is all the content I have on the .h file. (it was automatically generated).

1
2
3
4
5
6
#ifndef SALES_ITEM_H_INCLUDED
#define SALES_ITEM_H_INCLUDED



#endif // SALES_ITEM_H_INCLUDED 


So the header file has no actual content? Where is the class Sales_item defined?


Edit:
There's a section on operator overloads in the tutorial here. If you have a user defined type like a Sales_item class - you need to define how an operator like << should work.

http://www.cplusplus.com/doc/tutorial/templates/
Last edited on
But if the header file has no actual content, line 11 could not compile:
 
    Sales_item item1, item2;

There must be a more complete version of "Sales_item.h" somewhere?
Wildblue and Chervil:

Thank you for your answers. The problem is that the book does not explicitly indicates how to do that previously.

For example I do not know if I need to create the header file and also the class.

Is there a book or tutorial where I can learn how to define these things?

Last edited on
I don't have the Lippman book, but I've got to think that a primer on C++ would include classes. What does the book say this particularly code snippet is trying to help illustrate?

You can get into the tutorial on this site through that link I posted above.
When I tried to compile that code, i got the following errors:
[Error] 'Sales_item' was not declared in this scope
[Error] expected ';' before 'item1'
[Error] 'item1' was not declared in this scope
[Error] 'item2' was not declared in this scope


That is to say, it fails before reaching line 12.
Does your compiler also give similar errors?
Yes. The second one. The problem is that I do not know what kind of code to write on the.h file. I will be searching on the web.
Well, it seems as though either the book hasn't explained things very clearly, or perhaps that code wasn't meant to be a complete runnable program, it was just to read through and then move on. Or maybe the book comes with a cd or web site where you can get the full cpp code.
Topic archived. No new replies allowed.