使用GCC编译类

Compiling a Class with GCC

本文关键字:编译 GCC 使用      更新时间:2023-10-16

嘿,我不确定我是否犯了语法错误,但我犯了一个类,当我试图编译它时得到了这个错误…

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

它与g++配合使用很好,但我必须使用gcc。。。

这是问题代码。。

#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

gcc默认将程序编译为C。由于它是C++程序,因此无法正常工作。使用-x c++标志,或者将文件重命名为具有.C(大小写很重要)或.cpp扩展名。

编辑:实际上,你可以使用一大堆文件扩展名来表示你的程序是C++。从这个链接:

.cc、.cp、.cxx、.cpp、.c++、.c

编辑2:你下面的评论让我觉得你试图把头文件放在命令行上。不要那样做。只需编译源文件,并根据需要包含标头。