jeudi 28 mai 2020

C2280 std::unique_ptr

I'm trying to make Ticket Reservation System. However, I have an error about C2280.

On TicketReservation, I'm adding TicketManagers with std::vector.

It works fine without TicketResetvation, but with it, error uccred.

I found when error occured, but I don't know how to fix it.

void TicketReservation::addManager(TicketManager tm)
{
    std::cout << tm.getName() << std::endl;   // works.
    managers.push_back(tm);   // error occured.
}

And the error:

C2280 std::unique_ptr<Ticket,std::default_delete<Ticket>>::unique_ptr(const std::unique_ptr<Ticket, std::default_delete<Ticket>> &)'

1>        with
1>        [
1>            _Alloc=std::allocator<std::unique_ptr<Ticket,std::default_delete<Ticket>>>,
1>            _Ty=std::unique_ptr<Ticket,std::default_delete<Ticket>>,
1>            _Objty=std::unique_ptr<Ticket,std::default_delete<Ticket>>
1>        ]

1>        with
1>        [
1>            _Alloc=std::allocator<std::unique_ptr<Ticket,std::default_delete<Ticket>>>,
1>            _Ty=std::unique_ptr<Ticket,std::default_delete<Ticket>>,
1>            _Objty=std::unique_ptr<Ticket,std::default_delete<Ticket>>
1>        ]

1>        with
1>        [
1>            _Ty=std::unique_ptr<Ticket,std::default_delete<Ticket>>
1>        ]


I tried with managers.emplace_back(tm.getName(), tm.getNumber()); but same error.

Also I tried create every default constructor, but same error.

I don't know what is wrong..

Thank you for your help.

Here's my code :

#include <iostream>
#include "TicketReservation.h"
#include "TicketManager.h"
int main()
{
    TicketReservation tickets;
    tickets.addManager(TicketManager("Test A", 10));
    tickets.addManager(TicketManager("Test B", 5));
    tickets.addManager(TicketManager("Test C", 10));
...
}
#pragma once
class Ticket
{
protected:
    int number;     
    static int fNumber; 
    double price;       
public:

    Ticket() = delete;
    Ticket(const double& price);

    virtual ~Ticket();

...
};

#pragma once
#include "Ticket.h"
#include <vector>
#include <string>
#include <memory>
class TicketManager
{
private:
    std::string m_name;         
    int totalNumber;            
    std::vector<std::unique_ptr<Ticket>> ticket;
public:
    TicketManager() = delete;
    ~TicketManager();
    TicketManager(const std::string& name, const int& number);
    TicketManager(const TicketManager& tm);
    TicketManager(TicketManager&& tm) noexcept;

...
};
std::ostream& operator<<(std::ostream& out, const TicketManager& manager);

#include "TicketManager.h"
#include <iostream>
#include "AdvancedTicket.h"
#include "GeneralTicket.h"

TicketManager::~TicketManager()
{
}

TicketManager::TicketManager(const std::string& name, const int& number)
    : m_name(name), totalNumber(number)
{
}

TicketManager::TicketManager(const TicketManager& tm)
    : TicketManager(tm.getName(), tm.getNumber())
{
    for (auto it = tm.ticket.begin(); it < tm.ticket.end(); it++) {
        this->ticket.push_back(*it);
    }
}

TicketManager::TicketManager(TicketManager&& tm) noexcept
    : m_name(tm.m_name), totalNumber(tm.totalNumber)
{
}
...

Aucun commentaire:

Enregistrer un commentaire