我有一個很難找到的成員,爲什麼編譯器告訴我:麻煩用的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文件編譯你上面應該有錯誤
*你在哪裏*包括''?你能否請嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們展示? –
看起來你的make文件中包含'#include'而不是源文件 – myaut
詢問顯而易見的問題:你是否已經在源代碼中定義了「Pizza」,「HawaiianPizza」,「VegetarianPizza」和「CarnivoroPizza」類或者聲明瞭它們在包含的標題中? – Steve