【问题标题】:Compiling a Class with GCC用 GCC 编译一个类
【发布时间】:2012-03-29 12:53:19
【问题描述】:

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

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

【问题讨论】:

  • 请显示您当前使用的命令行。

标签: c++ class gcc compilation cc


【解决方案1】:

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

编辑:实际上,您可以使用一大堆文件扩展名来表明您的程序是 C++。来自this link

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

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

【讨论】:

  • 你为什么要把你的头文件放在命令行上? gcc file.cpp 会很好。如果你想使用-x 标志:gcc -x c++ file.cpp
  • 哦,我想我明白了。所以我将用于 g++ 的 .h 在 gcc 中无法识别\
  • 不知道是什么意思,不过正常的做法是不要自己编译头文件。
  • 嗯,我不确定我是否在这里说清楚了,因为我仍然有问题......对此感到抱歉。我必须编译的文件是dining.cpp 和dining.h。我必须使用 CC 编译器。所以我一直在做这样的编译.. cc Dining.cpp Dining.h
  • OP的问题明确排除了它。
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
相关资源
最近更新 更多