gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:26:54 26 4
gpt4 key购买 nike

在我的 C++ 程序中,非静态数据成员初始值设定项和作用域枚举等 C++11 功能在我的 main.cpp 文件中工作时没有警告。当我尝试在我的头文件中使用这些 c++11 功能时,我收到编译器警告仅适用于 -std=c++11 或 -std=gnu++11 [默认启用]

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;
}

水果.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

水果.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?我在 Properties > C/C++ Build > Settings > Tool Settings > Miscellaneous 下的“其他标志”是:-c -std=c++11 -fmessage-length=0

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

最佳答案

试试这个:

  1. 不要在您的“main.cpp”中“包含”Fruit.cpp 源文件。单独编译Fruit.cpp。这就是您的链接器的用途:)

  2. Eclipse > 项目首选项 > 设置 > C/C++ 编译器 > 杂项 > 其他标志 ><= 添加“-std=c++11”

  3. 确保您的 makefile 也有“-std=c++11”

关于c++11 在头文件中无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47848891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com