头文件中无法识别c ++ 11

问题描述 投票:1回答:1

在我的C ++程序中,c ++ 11的功能,如非静态数据成员初始化程序和作用域枚举在我的main.cpp文件中没有警告。当我尝试在头文件中使用这些c ++ 11功能时,我收到编译器警告only available with -std=c++11 or -std=gnu++11 [enabled by default]

main.cpp文件:

#include "Fruit.h"
#include "Fruit.cpp"

class Vegetable
{
    enum class VegetableType
    {
        Potato,
        Spinach,
        Broccoli,
        Carrot,
        Tomato,
        Pea,
        Cabbage
    };

    Vegetable(const VegetableType& vegetableType, const int& x, const int& y = 0);
    virtual ~Vegetable();

private:
    VegetableType currentVegetableType = VegetableType::Pea;
    int x = 0, y = 0;
    bool isTastey = false;
};

Vegetable::Vegetable(const VegetableType& vegetableType, const int& x, const int& y)
{
    currentVegetableType = vegetableType;
    this->x = x;
    this->y = y;
}

int main(void)
{
    return 0;
}

Fruit.h文件:

#ifndef FRUIT_H_
#define FRUIT_H_

class Fruit
{
    enum class FruitType
    {
        Berry,
        Pear,
        Samara,
        Drupe,
        Nucule,
        Pome,
        Pineapple
    };

    Fruit(const FruitType& fruitType, const int& x, const int& y = 0);
    virtual ~Fruit();

private:
    FruitType currentFruitType = FruitType::Pear;
    int x = 0, y = 0;
    bool isTastey = false;
};

#endif // FRUIT_H

Fruit.cpp文件:

#include "Fruit.h"

Fruit::Fruit(const FruitType& fruitType, const int& x, const int& y)
{
    currentFruitType = fruitType;
    this->x = x;
    this->y = y;
}

CDT构建控制台输出:

12:19:26 **** Incremental Build of configuration Debug for project EclipseBug ****
make all 
Building file: ../Fruit.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Fruit.d" -MT"Fruit.d" -o "Fruit.o" "../Fruit.cpp"
In file included from ../Fruit.cpp:1:0:
../Fruit.h:6:2: warning: scoped enums only available with -std=c++11 or -std=gnu++11 [enabled by default]
  enum class FruitType
  ^
../Fruit.h:21:42: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
  FruitType currentFruitType = FruitType::Pear;
                                          ^
../Fruit.h:22:10: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
  int x = 0, y = 0;
subdir.mk:21: recipe for target 'Fruit.o' failed
          ^
../Fruit.h:22:17: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
  int x = 0, y = 0;
                 ^
../Fruit.h:23:18: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
  bool isTastey = false;
                  ^
../Fruit.h:21:31: error: ‘FruitType’ is not a class or namespace
  FruitType currentFruitType = FruitType::Pear;
                               ^
make: *** [Fruit.o] Error 1

12:19:26 Build Finished (took 63ms)

为什么c ++ 11在main.cpp中工作但在Fruit.h中不起作用?如何在我的Fruit.h文件中启用c ++ 11?属性> C / C ++ Build>设置>工具设置>杂项下的“其他标志”为:-c -std=c++11 -fmessage-length=0

我使用Eclipse Luna Service Release 2(4.4.2)作为我的IDE。

c++ c++11
1个回答
2
投票

试试这个:

  1. 不要在“main.cpp”中“包含”Fruit.cpp源文件。分别编译Fruit.cpp。这就是你的链接器:)
  2. Eclipse>项目首选项>设置> C / C ++编译器>其他>其他标志> <= add“-std = c ++ 11”
  3. 确保你的makefile也有“-std = c ++ 11”
© www.soinside.com 2019 - 2024. All rights reserved.