gpt4 book ai didi

c++ - STD 错误 : wrong number of template arguments (1, 应该是 3)

转载 作者:行者123 更新时间:2023-11-28 01:38:05 26 4
gpt4 key购买 nike

我有三个类(class)。一个是抽象的,第二个是基于抽象的,它在 std::vector 中存储指向另一个实例的指针。

我想创建 ClientRepositorystd::shared_ptr 以便将来传递给 Manager 类实例。

有一个名为“Repository”的模板类。我想用它来创建几种类型的 Repositories,例如:CarsRepositoryItemsRepository 等。

不幸的是,我在编译时遇到错误:

main.cpp:84:139: 错误:模板参数 1 无效 std::shared_ptr, std::vector>> p = std::make_shared; ^

知识库.hpp

#ifndef REPOSITORY_HPP
#define REPOSITORY_HPP

#include <string>

template<typename typeBOOL, typename typeShared_ptr, typename VectorOfSmarPtrs > class Repository
{
protected:
VectorOfSmarPtrs nameOfVector;

public:
virtual typeBOOL create(const typeShared_ptr&) = 0;
};

#endif

客户端库.hpp

#ifndef CLIENTREPOSITORY_HPP
#define CLIENTREPOSITORY_HPP

#include <memory>
#include <string>

#include "Client.hpp"
#include "Repository.hpp"

class ClientRepository : public Repository<bool, std::shared_ptr<Client>, std::vector<std::shared_ptr<Client> > >{
public:
bool create(const std::shared_ptr<Client> & newClient) override;
};

#endif

客户端库.cpp

include "ClientRepository.hpp"

bool ClientRepository::create(const std::shared_ptr<Client> & newClient) {
if(newClient != NULL){
for(int i = 0; i < this->nameOfVector.size(); i++) {
if(this->nameOfVector.at(i)->GetPersonalID() == newClient->GetPersonalID()) {
return 0;
}
}
this->nameOfVector.push_back(newClient);
return 1;
}
else return 0;
}

主要.cpp

#include <iostream>
#include <memory>

#include "Client.hpp"
#include "ClientRepository.hpp"
#include "Repository.hpp"

int main(){
ClientRepository x;


std::shared_ptr<Repository< bool, std::shared_ptr<Client>, std::vector<std::shared_ptr<Client>> > p = std::make_shared<ClientRepository>;
}

这段代码有什么问题?我应该改变什么?

最佳答案

你错过了括号:

std::shared_ptr<Repository<bool,
std::shared_ptr<Client>,
std::vector<std::shared_ptr<Client>>>> p
// ^
= std::make_shared<ClientRepository>();
// ^^

关于c++ - STD 错误 : wrong number of template arguments (1, 应该是 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48401039/

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