Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include <iostream>
#include <string>
using namespace std;

class Sales_item
{
private:
    string m_strName;
public:
    Sales_item(string name):m_strName(name){};
    void Display(ostream& os) {os << m_strName}; /*This line is wrong.*/
};



When I try to compile the code above, it says "error C2143: syntax error : missing ';' before '}'". I need an explanation why it won't compile. Thanks in advance.
Posted
Comments
[no name] 9-Feb-14 8:08am    
Rather than tell you the answer I will tell you that there is an unterminated statement on that line.
Vedat Ozan Oner 9-Feb-14 8:11am    
:) torturing. divide that line into more lines to see the exact place.
Kornfeld Eliyahu Peter 9-Feb-14 8:17am    
One more hint: on the line with the error there is only one '}'!!!
Vedat Ozan Oner 9-Feb-14 8:35am    
I give 5 for your hint :)

Your question has percolated for enough time I think, so I'll jump in and make a small contribution. You don't have your semicolons in the right place.

Your code:
C#
Sales_item(string name):m_strName(name){};
void Display(ostream& os) {os << m_strName};

should be:
C#
Sales_item(string name):m_strName(name){}  // (no semicolon)
void Display(ostream& os) {os << m_strName;} // (semicolon before brace not after)

It is not syntactically incorrect to have semicolons after the closing braces, but they are superfluous in a class definition...
 
Share this answer
 
The semicolon is required with C/C++ as end of command indicator. Your Display() function contains a command that must be terminated. So it must be:
void Display(ostream& os) {os << m_strName;}

The semicolon at the end is not required here, because the Display() function is defined (containing the implementation) and not declared.
 
Share this answer
 
Comments
H.Brydon 10-Feb-14 0:19am    
Not sure why you were downvoted; +5 from me to compensate.

This doesn't look like homework to me (and if it is, OP is not asking to do the work for him) so I don't see the reason for the downvote...
Jochen Arndt 10-Feb-14 3:25am    
Thank you. Maybe it was downvoted because I answered after the first comments. But I started writing the answer when there was no comment.
thatraja 10-Feb-14 3:13am    
Counter 5!
Jochen Arndt 10-Feb-14 3:25am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900