gpt4 book ai didi

c++ - 与 unique_ptr 配对的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 00:44:03 27 4
gpt4 key购买 nike

我正在尝试使用 unique_ptr make_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++ 编译

Clang 版本:clang-900.0.39.2

G++ 版本:4.2.1

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

这是因为 Apple 提供的旧版 G++ 吗?

最佳答案

Is this due to the old version of G++ shipped by Apple?

没有。这是因为您试图通过将唯一指针传递给 make_pairinsert 来复制它。

std::unique_ptr 不可复制。事实上它只是移动。更改为:

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

关于c++ - 与 unique_ptr 配对的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com