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
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.
Why can I not pass a unique_ptr into a function? You cannot do that because unique_ptr has a move constructor but not a copy constructor. According to the standard, when a move constructor is defined but a copy constructor is not defined, the copy constructor is deleted.
An explicit delete for a unique_ptr would be reset() . But do remember that unique_ptr are there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.
A unique_ptr does not share its pointer. It cannot be copied to another unique_ptr , passed by value to a function, or used in any C++ Standard Library algorithm that requires copies to be made. A unique_ptr can only be moved.
Try adding
#include <memory>
To the top of your file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With