【问题标题】:STD error: wrong number of template arguments (1, should be 3)STD 错误:模板参数的数量错误(1,应该是 3)
【发布时间】:2018-01-23 11:50:18
【问题描述】:

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

我想创建ClientRepository 中的std::shared_ptr,以便将来将其传递给Manager 类实例。

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

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

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


【解决方案1】:

你错过了括号:

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

【讨论】:

  • 现在我得到了`错误:模板参数 1 无效 std::shared_ptr, std::vector<:shared_ptr> >> p = std::make_shared(); ^`
  • 你不能用auto吗?
  • @KKMKK 数一下你的尖括号——''。
  • @Jarod42 我不能。它必须是 Repository 类型。
  • 修复括号后我又遇到了一个错误:main.cpp:84:124: error: conversion from ‘&lt;unresolved overloaded function type&gt;’ to non-scalar type ‘std::shared_ptr&lt;Repository&lt;bool, std::__cxx11::basic_string&lt;char&gt;, std::shared_ptr&lt;Client&gt;, std::vector&lt;std::shared_ptr&lt;Client&gt; &gt; &gt; &gt;’ requested std::shared_ptr&lt;Repository&lt;bool, std::string, std::shared_ptr&lt;Client&gt;, std::vector&lt;std::shared_ptr&lt;Client&gt;&gt;&gt;&gt; p = std::make_shared&lt;ClientRep ^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
相关资源
最近更新 更多