#include <iostream>
#include<string>
using namespace std;

class Book
{

	private:

		string title;

		double price;

	public:

		Book();

		void setTitle(string s);

		void setPrice(double p);

		string getTitle() const;  
		{return title;}

error code C2059(syntax error: '{' ) and C2334(unexpected token preceding '{' skipping apparent function body occur

double getPrice() const;  
		{return price;}

error code C2059(syntax error: '{' ) and C2334(unexpected token preceding '{' skipping apparent function body occur

bool hasKeyword(string key);


	
};

Book::Book()
{
	title="None";
	price=0;
}

void Book :: setTitle(string s)
{
	title=s;

}

void Book::setPrice(double p)
{
	if(p>0)
	{
		price=p;
	
	}

	else
	{
		price=0;
	}

}

what is wrong with my code?

Recommended Answers

All 6 Replies

When you have the "return" statements, ie.

{return title;}
//and
{return price;}

You shouldn't have braces. It should be:

return title;
return price;
double getPrice() const;    //<----- naughty semi-colon use
		{return price;}

Lose the semi-colon after the const. You do not place a semi-colon after the header line of a function's definition. That turns it into a function prototype and then you're left with a dangling statement block that's not recognized as the function's body.

You have done this in a couple of places.

When you have the "return" statements, ie.

{return title;}
//and
{return price;}

You shouldn't have braces. It should be:

return title;
return price;

Not true. A function definition always requires braces. The only places they're optional is a loop statement or immediately after a conditional. Each of those return statements is intended to be associated with a different function. Observe:

string getTitle() const;  
		{return title;}

double getPrice() const;  
		{return price;}

This is where the error is, but the error is the semi-colons, not the braces.

Thank you.

Sorry I was looking at it the wrong way. You see I'm confused about the thing with classes where you have to do something funny and...oh I dunno... :(

>>...You see I'm confused ... ...oh I dunno... :(
:confused::confused::confused:
LOL! That makes 2 of us... ;)

Make a thread, maybe someone can "unconfuse" you... :P

Nah it's cool :)
It's 'cos I didn't spend much time learning classes before I went to Python, and now I've come back (sort of), I've forgotten it all :'(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.