Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with unique_ptr : not a member of 'std'

Tags:

c++

c++11

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

like image 534
Mocking Bird Avatar asked Apr 17 '17 11:04

Mocking Bird


People also ask

What happens when unique_ptr goes out of scope?

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.

Can I pass unique_ptr to a function?

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.

Can you delete a unique_ptr?

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.

Can you copy unique_ptr?

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.


1 Answers

Try adding

#include <memory>

To the top of your file.

like image 73
doron Avatar answered Oct 09 '22 04:10

doron