expected unqualified id before {

Trying out a project in codeblocks with a class, I have a header file with the declaration in, a cpp file with the definition in and another cpp with the main code in.

This is the portion of the class definition giving me trouble:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include "Sales_item.h"

using std::cout; using std::string;

Sales_item
{
    //class definition
}

The opening curly brace is where I get the error message, and if I put

class Sales_item instead then it gives me an error about multiple definitions!
You don't need the Sales_item {}
if I put class Sales_item instead then it gives me an error about multiple definitions!


Don't define a class more than once. Is it already defined in Sales_item.h?
I thought I declared it in the header file, which is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef SALESITEM_H_INCLUDED
#define SALESITEM_H_INCLUDED

#include <string>

class Sales_item
{
    public:
        double avg_price() const;
        bool same_isbn(const Sales_item&) const;
        void write_class(std::string&, unsigned&, double&);
        void read_class();

        Sales_item();

    private:
        std::string isbn;
        unsigned units_sold;
        double revenue;
};



#endif // SALES_ITEM_H_INCLUDED 


P.S also, the header file now has a little lock symbol next to it and I can't make changes to the code within it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Sales_item
{
    public:
        double avg_price() const;
        bool same_isbn(const Sales_item&) const;
        void write_class(std::string&, unsigned&, double&);
        void read_class();

        Sales_item();

    private:
        std::string isbn;
        unsigned units_sold;
        double revenue;
};


You can only have one of these per cpp file. You've got this one from the header. What's needed now is definition of each of those functions, like this:
1
2
3
4
Sales_item::read_class()
{
  // code here
}

and so on.

This
1
2
3
4
Sales_item
{
    //class definition
}

is just nonsensical and not anything the compiler will recognise. It thinks you're trying to create an object of type Sales_item, so it's expecting something like this:
Sales_item someObject;

This
1
2
3
4
class Sales_item
{
    //class definition
};

is seen an an attempt to create a new kind of object - a new kind of class, in fact, named Sales_item, and quite rightly it objects because you've already done that.
Last edited on
class implentation

1
2
3
4
double Sales_item::avg_price() const
{
 //your code
}

Do this in your cpp file for all members
Last edited on
Okay so I have to define each function individually (which seems laborious, but is the only way to go about it?). And what about the local variables? In my declaration I have
1
2
3
4
private: 
       string isbn; 
       unsigned units_sold;
       double revenue;


and none of them need to be initialised (apart from given default values by the default constructor) so is the declaration of them enough or do I have to write something in the definition as well?
Okay so I have to define each function individually (which seems laborious, but is the only way to go about it?).

I must have misunderstood this - how else would you do it? Some kind of mutant all-in-one function? If you like you can do it wrapped inside the class { ...}; in the header file, but you still need to write each function individually.

... none of them need to be initialised (apart from given default values by the default constructor) so is the declaration of them enough or do I have to write something in the definition as well?


No, that's it. They don't even exist (i.e. don't take up any memory) until you actually create an instance of the class. I think under C++11, you can specify default values, but you don't have to.
Ahhh!!! Now in the definition I'm writing them like this:

1
2
3
4
5
6
7
Sales_item::double avg_price() const
{
    if(units_sold)
        return revenue/units_sold;

    else return 0;
}


and getting even more unqualified id error messages! An error message for each function

N.B. Fixed, I was being stupid. Thanks for your help!
Last edited on
Topic archived. No new replies allowed.