我正尝试使用下例中的unique_ptr make_pair
(该代码是Facade Design模式的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 ++进行编译
Clang版本:clang-900.0.39.2
G ++版本:4.2.1
平台:OS x-x86_64-apple-darwin16.6.0
这是由于Apple发行的旧版G ++吗?
答案 0 :(得分:5)
这是由于Apple发行的旧版G ++吗?
不。这是因为您试图通过将唯一指针传递给make_pair
和insert
来复制唯一指针。
std::unique_ptr
是不可复制的。实际上,它仅是移动的。更改为:
pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, std::move(newAccount));
bankAccounts->insert(std::move(toBeInserted));