在我的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 Build Console输出:
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。
答案 0 :(得分:2)
试试这个:
NOT 在“main.cpp”中“包含”Fruit.cpp源文件。 分别编译Fruit.cpp。这就是你的链接器的用途:)
Eclipse>项目偏好>设置> C / C ++编译器>其他>其他标志> < = add“-std = c ++ 11”
确保您的makefile也有“-std = c ++ 11”