[英]Trouble with unique_ptr : not a member of 'std'
我很難找到為什么編譯器告訴我這個:
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);
我有unique_ptr的include
#include <memory>
我使用了很好的C ++編譯標志
CFLAGS = -std=c++11 -W -Wall -Wextra -Werror -pedantic
我已經嘗試使用namespace std ;
這是我使用std :: unique_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。
這里有一個小代碼,給我相同的錯誤:
#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編譯你應該有錯誤
CFLAGS
適用於C編譯器。 您正在使用C ++和C ++編譯器。 在Makefile中使用CXXFLAGS
來設置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
進行調試。
嘗試添加
#include <memory>
到您文件的頂部。
如果要創建cmake項目,可以向CmakeLists.txt添加一行代碼,如下所示。
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Ethereal已經解釋了錯誤原因的細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.