2017-04-17 20 views
0

我有一個很難找到的成員,爲什麼編譯器告訴我:麻煩用的unique_ptr:不是「性病」

main.cpp:51:17: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type 
static std::unique_ptr<Pizza> createPizza(PizzaType t_pizza) 

這:

main.cpp:69:5: error: ‘unique_ptr’ is not a member of ‘std’ 
std::unique_ptr<Pizza> pizza = PizzaFactory::createPizza(t_pizzaType); 

我有包括uniqu_ptr和我用的是好的編譯標誌liek說:

#include <memory> 

CFLAGS = -std=c++11 -W -Wall -Wextra -Werror -pedantic 

我已經使用空間std嘗試;

這裏是代碼塊,我使用的std :: uniqu_ptr

class PizzaFactory 
{ 
public: 
    enum PizzaType 
    { 
    Hawaiian, 
    Vegetarian, 
    Carnivoro 
    }; 

    static std::unique_ptr<Pizza> createPizza(PizzaType t_pizza) 
    { 
    switch (t_pizza) 
    { 
    case Hawaiian: 
    return std::unique_ptr<HawaiianPizza>(new HawaiianPizza()); 
    case Vegetarian: 
    return std::unique_ptr<VegetarianPizza>(new VegetarianPizza()); 
    case Carnivoro: 
    return std::unique_ptr<CarnivoroPizza>(new CarnivoroPizza()); 
    default: 
    throw "Invalid pizza type."; 
    } 
    } 
}; 

void pizza_information(PizzaFactory::PizzaType t_pizzaType) 
{ 
    std::unique_ptr<Pizza> pizza = PizzaFactory::createPizza(t_pizzaType); 
    std::cout << "Price of " << t_pizzaType << "is " << pizza->getPrice() << '\n'; 
} 

我真的能找到什麼不對的代碼,請大家幫忙

謝謝。

編輯。

這裏是我使用的Makefile:

NAME = plazza 

G++ = g++ 

CFLAGS = -W -Wall -Wextra -Werror -std=c++11 

SRC = main.cpp 

OBJ = $(SRC:.cpp=.o) 

RM = rm -rf 

all: $(NAME) 

$(NAME): $(OBJ) 
    $(G++) $(CFLAGS) $(OBJ) -o $(NAME) 

clean: 
    $(RM) $(OBj) 

fclean: clean 
    $(RM) $(NAME) 

re: fclean all 

EDIT2。

這裏一個SMaL公司的代碼,給我同樣的錯誤:

#include <memory> 
#include <iostream> 

class Hi 
{ 
    public: 
     void sayHi(const std::string &t_hi) 
     { 
      std::cout << t_hi << '\n'; 
     } 
}; 

int main() 
{ 
    auto hi = std::unique_ptr<Hi>(new Hi()); 

    hi->sayHi("Salut"); 
    return 0; 
} 

與Makefile文件編譯你上面應該有錯誤

+4

*你在哪裏*包括''?你能否請嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們展示? –

+0

看起來你的make文件中包含'#include'而不是源文件 – myaut

+0

詢問顯而易見的問題:你是否已經在源代碼中定義了「Pizza」,「HawaiianPizza」,「VegetarianPizza」和「CarnivoroPizza」類或者聲明瞭它們在包含的標題中? – Steve

回答

3

CFLAGS適用於C編譯器。您正在使用C++和C++編譯器。使用CXXFLAGS在Makefile中設置C++編譯器的標誌:

NAME = plazza 

G++ = g++ 

CXXFLAGS = -W -Wall -Wextra -Werror -std=c++11 

SRC = main.cpp 

既然你設置爲C標誌,不啓用C++ 11,因爲-std=c++11不傳遞到你的C++編譯器。如果使用C編譯器進行編譯,編譯器(至少GCC會執行AFAIK)會警告C編譯器上設置的C++標誌。在這些類型的編譯器錯誤情況下,您可以使用make VERBOSE=1進行調試。

+0

謝謝,先生,它的工作。我不知道Makefile中的變量名稱很重要。再次感謝 –

+1

@MockingBird不客氣。在StackExchange站點上,如果答案能夠解決您的問題,則應將其標記爲接受的答案,以便問題得到關閉(可在投票按鈕下方完成)。 – Etherealone

+0

@MockingBird我相信它看起來像一個複選標記。 – Yakk

1

的加入

#include <memory> 

要頂你的文件。