Search code examples
c++classgcccompilationcc

Compiling a Class with GCC


Hey all im not sure if ive made a syntactical error hear but ive made a class and get this error when i try to compile it....

dining.h:7:1: error: unknown type name ‘class’
dining.h:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

it works fine with g++ but i have to use gcc...

this is the problem code..

#ifndef DINING_H
#define DINING_H

class DiningSet {

    public:
        DiningSet();
        ~DiningSet();
        void getSize(); // accepts X and Y dimentions of the table (in metres)
        //void surfaceArea(int,int);    // Calculates Surface Area of table (in square metres)
        void numChairs();   // accepts number of chairs
        void cushionColour();   // accepts a cushion colour
        void woodType();    // accepts wood type
        void priceComp();   // computes a price of set
        void purchaseDet(); // displays details of purchase
        void purchasePrice();   // displays price of purchace

    private:
        int X, Y; // Dimentions of table
        float Surface;
        int chairs; // Number of chairs
        char colour; // colour of cushion (should be dynamic array)
        char wood;
        float totalPrice;   
};


#endif

Solution

  • gcc defaults to compiling your program as C. Since it's a C++ program, that's not going to work. Use the -x c++ flag, or rename your file to have a .C (case is important) or .cpp extension.

    Edit: actually, you can use a whole bunch of filename extensions to indicate that your program is C++. From this link:

    .cc, .cp, .cxx, .cpp, .c++, .C

    Edit 2: Your comment below makes me think you're trying to put the header file on the command line. Don't do that. Just compile source files and include the headers as necessary.