I have a hard time finding why compiler tell me this:
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)
and this:
main.cpp:69:5: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr<Pizza> pizza = PizzaFactory::createPizza(t_pizzaType);
I have the include for unique_ptr
#include <memory>
and I use the good C++ compilation flags
CFLAGS = -std=c++11 -W -Wall -Wextra -Werror -pedantic
I have already try using namespace std ;
Here are the block of code where I use 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';
}
I really can find what's wrong with this code, please help
Thank you.
edit.
Here is the Makefile I use:
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.
Here a smal code that give me the same errors:
#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;
}
compile with the Makefile above you should have the error
CFLAGS
is for C compilers. You are using C++ and a C++ compiler. Use CXXFLAGS
in your Makefile to set C++ compiler's flags:
NAME = plazza
G++ = g++
CXXFLAGS = -W -Wall -Wextra -Werror -std=c++11
SRC = main.cpp
Since you are setting C flags, C++11 is not enabled because -std=c++11
is not passed to your C++ compiler. If you compiled with a C compiler, the compiler (at least GCC does it AFAIK) would warn about the C++ flag being set on the C compiler. You could use make VERBOSE=1
in these kind of compiler error situations for debugging.
Try adding
#include <memory>
To the top of your file.
If you are creating cmake project, you can add a line of code to CmakeLists.txt as shown below.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Ethereal has explained the details of the reason for the error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.