我很难找到为什么编译器告诉我这个:
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 <memory>
我使用了好的 C++ 编译标志
CFLAGS = -std=c++11 -W -Wall -Wextra -Werror -pedantic
我已经尝试过 使用命名空间 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
编辑2。
这是一个给我同样错误的小代码:
#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 编译你应该有错误
原文由 Mocking Bird 发布,翻译遵循 CC BY-SA 4.0 许可协议
CFLAGS
适用于 C 编译器。您正在使用 C++ 和 C++ 编译器。在 Makefile 中使用CXXFLAGS
来设置 C++ 编译器的标志:由于您正在设置 C 标志,因此未启用 C++11,因为
-std=c++11
未传递给您的 C++ 编译器。如果您使用 C 编译器进行编译,则编译器(至少 GCC 会这样做)会警告 C 编译器上设置了 C++ 标志。您可以在这些编译器错误情况下使用make VERBOSE=1
进行调试。