C ++模板类:没有匹配的成员函数可调用'push_back'

问题描述 投票:0回答:1

我正在尝试构建一个模板类,该模板类包含一个指向向量的指针,该向量本身也包含指针。

template <typename S, typename T>
struct MyClass
{
    std::shared_ptr<aNode<S,T>> head{nullptr};
    std::shared_ptr<std::vector<aNode<S,T>>> positionList; // <<- This guy

    void add(S const & k, T const & v)
    {
        std::shared_ptr<aNode<S,T>> newNode = std::make_shared<aNode<S,T>>();
        newNode->set_data(k, v);
        if (head == nullptr) {
            head = newNode;
        } else {
            auto current = head;
            while (current->next != nullptr) {
                current = current->next;
            }
            current->next = newNode;
        }
        positionList->push_back(newNode); // <<- Error here
    } 
    [...]

在第20行,编译器抛出错误No matching member function for call to 'push_back'

现在->运算符应该允许我访问vector,并且vector当然具有push_back方法。我唯一能想到的是向量没有初始化。将第4行更改为std::shared_ptr<std::vector<aNode<S,T>>()> positionList;会引发错误

Member reference base type 'std::__1::shared_ptr<std::__1::vector<aNode<std::__1::basic_string<char>, std::__1::basic_string<char> >, std::__1::allocator<aNode<std::__1::basic_string<char>, std::__1::basic_string<char> > > > ()>::element_type' (aka 'std::__1::vector<aNode<std::__1::basic_string<char>, std::__1::basic_string<char> >, std::__1::allocator<aNode<std::__1::basic_string<char>, std::__1::basic_string<char> > > > ()') is not a structure or union

(仍在第20行上。

我在哪里错了?

c++ pointers templates shared
1个回答
0
投票

根据您的描述,positionList应为:

© www.soinside.com 2019 - 2024. All rights reserved.