std::vector
中存储指针到另一个的实例。
我想创建std::shared_ptr
ClientRepository
以便将来将其传递给Manager类实例。
有一个名为“Repository”的模板类。我想用它来创建几种类型的Repositories
,例如:CarsRepository
,ItemsRepository
等。
不幸的是我在编译时遇到错误:
main.cpp:84:139:错误:模板参数1无效 std :: shared_ptr,std :: vector> > p = std :: make_shared; ^
Repository.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
ClientRepository.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
ClientRepository.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;
}
的main.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>;
}
这段代码有什么问题?我应该改变什么?
答案 0 :(得分:2)
你错过了括号:
std::shared_ptr<Repository<bool,
std::shared_ptr<Client>,
std::vector<std::shared_ptr<Client>>>> p
// ^
= std::make_shared<ClientRepository>();
// ^^