c++;11在头文件中无法识别 在我的C++程序中,C++ 11的特性像非静态数据成员初始化器和作用域枚举在我的Me.CPP文件中没有警告。当我尝试在头文件中使用这些c++11功能时,我得到的编译器警告仅适用于-std=c++11或-std=gnu++11[默认启用]

c++;11在头文件中无法识别 在我的C++程序中,C++ 11的特性像非静态数据成员初始化器和作用域枚举在我的Me.CPP文件中没有警告。当我尝试在头文件中使用这些c++11功能时,我得到的编译器警告仅适用于-std=c++11或-std=gnu++11[默认启用],c++,c++11,C++,C++11,main.cpp文件: #include "Fruit.h" #include "Fruit.cpp" class Vegetable { enum class VegetableType { Potato, Spinach, Broccoli, Carrot, Tomato, Pea, Cabbage }; Vegetable(const Vege

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;
}
#include "Fruit.h"

Fruit::Fruit(const FruitType& fruitType, const int& x, const int& y)
{
    currentFruitType = fruitType;
    this->x = x;
    this->y = y;
}
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"
#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;
}
#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++生成>设置>工具设置>杂项下我的“其他标志”是:
-C-std=C++11-fmessage length=0

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

尝试以下方法:

  • 不要在“main.cpp”中“包括”Fruit.cpp源文件。 分别编译Fruit.cpp。这就是你的链接器的用途:)

  • Eclipse>项目首选项>设置>C/C++编译器>杂项>其他标志>
    对于
    Fruit.cpp
    编译,没有
    --std=c++11
    。看起来您可能只对main.cpp文件启用了c++11支持。您应该改为设置项目范围的设置(即首先在工作区资源管理器中选择项目节点,然后转到属性)。@VTT我在“我的程序的属性”中设置了
    -std=c++11
    标志。这不应该影响整个项目吗?适当地修改您的
    Makefile
    不要包含
    您的
    .cpp
    文件。单独编译并链接。@PhilippHoehnkingphilippiii不为所有cpp文件启用此选项是危险的,您确实希望模块中的所有源文件都具有相同的选项。“仅适用于c++11”错误消失,但我在main.cpp的第26行获得了对“vtable for vegeture”的
    未定义引用,以及对“vtable for Fruit”的
    未定义引用,“Fruit::Fruit”的多个定义(Fruit::fructype const&,int const&,int const&)“
    在Fruit.cpp1的第3行)确保“Fruit”和“vegeture”中的所有方法都在各自的.cpp源文件中实现。2) 确保您的Makefile中包含水果.cpp和蔬菜.cpp,并且两者都链接到可执行文件中。另外,请记住C++成员默认为私有。您可能希望将“public”添加到类定义中!