Dalam program C++ saya, fitur c++11 seperti inisialisasi anggota data non-statis dan enum tercakup berfungsi tanpa peringatan di file main.cpp saya. Ketika saya mencoba menggunakan fitur c++11 ini di file header saya, saya mendapatkan peringatan kompiler only available with -std=c++11 or -std=gnu++11 [enabled by default]

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

File buah.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

File buah.cpp:

#include "Fruit.h"

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

Keluaran Konsol Bangun 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)

Mengapa c++11 bekerja di main.cpp tetapi tidak di Fruit.h? Bagaimana cara mengaktifkan c++11 di file Fruit.h saya? 'Tanda lain' saya di bawah Properti > C/C++ Build > Setelan > Setelan Alat > Lain-lain adalah: -c -std=c++11 -fmessage-length=0

Saya menggunakan Eclipse Luna Service Release 2 (4.4.2) sebagai IDE saya.

1
Philipp Hoehn kingphilippiii 16 Desember 2017, 21:44

1 menjawab

Jawaban Terbaik

Coba ini:

  1. Jangan TIDAK "sertakan" file sumber Fruit.cpp di "main.cpp" Anda. Kompilasi Fruit.cpp secara terpisah. Itulah gunanya tautan Anda :)

  2. Eclipse > preferensi proyek > Pengaturan > C/C++ Compiler > Miscellaneous > Other Flags > <= tambahkan "-std=c++11"

  3. Pastikan makefile Anda juga memiliki "-std=c++11"

2
paulsm4 16 Desember 2017, 19:06