【发布时间】:2018-01-23 11:50:18
【问题描述】:
我有三节课。一个是抽象的,第二个是基于抽象的,它在std::vector 中存储指向另一个实例的指针。
我想创建ClientRepository 中的std::shared_ptr,以便将来将其传递给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>;
}
这段代码有什么问题?我应该改变什么?
【问题讨论】:
-
std::bool到底是什么??bool是关键字:P -
@Rakete 可能改编自 C,其中头文件的名称
stdbool.h可能会产生误导。 -
typeBOOL?为什么会这样?
-
@Rakete1111 我已经改变了。现在它“只有”一个错误。
-
@manni66 它是类型的名称。在这个例子中对我来说更容易。
标签: c++ templates inheritance c++14