Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用独特的ptr制作一双鞋的正确方法是什么?_C++_Clang++ - Fatal编程技术网

C++ 使用独特的ptr制作一双鞋的正确方法是什么?

C++ 使用独特的ptr制作一双鞋的正确方法是什么?,c++,clang++,C++,Clang++,我正在尝试使用一个独特的\u ptr来创建\u pair,如下面的示例所示 代码是外观设计模式的C++实现 #include <unordered_map> #include <utility> #include <functional> using namespace std; // Step 1 : Design the Interface class IAccount { protected: int accountNo;

我正在尝试使用一个独特的\u ptr来创建\u pair,如下面的示例所示

代码是外观设计模式

的C++实现
#include <unordered_map>
#include <utility> 
#include <functional>

using namespace std;

// Step 1 : Design the Interface
class IAccount {
    protected: 
    int accountNo;
    int cash;

    public:
    virtual void deposit(float amount);
    virtual void withdraw(float amount);
    virtual void transfer(float amount);
    virtual int getAccountNumber() = 0;
};

// Step 2 : Implement the interface with one or more subtypes
class Current : public IAccount{
    public:
    Current(int amt) { cash = amt; }

    int getAccountNumber() {
        return accountNo;
    }
};

class Savings : public IAccount{
    public:
    Savings(int amt) { cash = amt; }

    int getAccountNumber() {
        return accountNo;
    }
};

// Step 3: Create the Facade class and wrap the classes that implement the Interface 
class BankService {
    unordered_map<int, unique_ptr<IAccount>> *bankAccounts;    
    int index;

    public: 
    BankService(): index(0){ 
        bankAccounts = new unordered_map<int, unique_ptr<IAccount>>();    
    }

    int createNewAccount(string name, int amount, string type, int accountNo) {
        unique_ptr<IAccount> newAccount;    
        if(type.compare("current")) {          
            newAccount.reset(new Current(amount));  
        }

        // Add to the unordered list --- Error here --- 
        pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, newAccount);
        bankAccounts->insert(toBeInserted); 

        return accountNo;
    }
};
代码显示出以下错误:

/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:266:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:639:
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:634:12: error: no matching constructor for initialization of 'pair<typename
      __make_pair_return<int &>::type, typename __make_pair_return<unique_ptr<IAccount, default_delete<IAccount> > &>::type>' (aka 'pair<int,
      std::__1::unique_ptr<IAccount, std::__1::default_delete<IAccount> > >')
    return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
           ^
如果我将其更改为:

// Add to the unordered list (note added const) 
pair<const int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, newAccount);
bankAccounts->insert(toBeInserted); 
欢迎我的是:

In file included from ./DesignPatterns/Structural/./../../Include/Common.h:1:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/vector:266:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:640:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1783:31: error: call to implicitly-deleted copy constructor of 'std::__1::pair<const
      int, std::__1::unique_ptr<IAccount, std::__1::default_delete<IAccount> > >'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
我试着用Clang++和G++进行编译++

叮当版本:叮当-900.0.39.2

G++版本:4.2.1

平台:OS x-x86_64-apple-darwin16.6.0

这是因为苹果提供的旧版本的G++吗

这是因为苹果提供的旧版本的G++吗

否。这是因为您试图通过将唯一指针传递给make_pair和insert来复制它

std::unique_ptr不可复制。事实上,这只是一种行动。改为:

pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, std::move(newAccount));
bankAccounts->insert(std::move(toBeInserted)); 
这是因为苹果提供的旧版本的G++吗

否。这是因为您试图通过将唯一指针传递给make_pair和insert来复制它

std::unique_ptr不可复制。事实上,这只是一种行动。改为:

pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, std::move(newAccount));
bankAccounts->insert(std::move(toBeInserted));