我试图在C++的派生类中声明一个指向std::vector<int>的指针。该类再次成为其他类的基类。

我的尝试如下

protected:
   std::vector<CustomClass> *mypointer;

但是,当我编译它时,出现以下错误:
error: no match for ‘operator=’ (operand types are ‘std::vector<int>’ and ‘std::vector<int>*’)
error: no match for ‘operator*’ (operand type is ‘std::vector<int>’)

和其他一些操作数丢失。

我很无能为力,问题根本就在这里。我必须在当前类中实现所有这些功能吗?如果是这样,为什么我必须这样做?

对于需要更多上下文的任何人,我想在CbmStsSensor (found here)类中实现该指针,该类派生自CbmStsElement。

编辑:可以在CbmStsElement.henter link description here中找到一些相关的类。

Edit2:整个编译错误日志可以在here中找到。

最佳答案

错误消息足够清楚。例如,在代码中的某处,您尝试将一个指针指向std::vector<int> *类型的对象分配给std::vector<int>类型的对象。

这是一个演示程序,显示了如何生成此错误消息。

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v1;
    std::vector<int> v2;

    v1 = &v2;

    return 0;
}

编译错误是
prog.cpp: In function ‘int main()’:
prog.cpp:9:8: error: no match for ‘operator=’ (operand types are ‘std::vector<int>’ and ‘std::vector<int>*’)
  v1 = &v2;
        ^~

或(相对于第二条错误消息)类型operator *的对象没有一元std::vector<int>

这是另一个示范节目
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v;

    *v;

    return 0;
}

错误消息是
prog.cpp: In function ‘int main()’:
prog.cpp:8:2: error: no match for ‘operator*’ (operand type is ‘std::vector<int>’)
  *v;
  ^~

也许您应该将数据成员声明为std::vector<int>类型而不是指针类型。

关于c++ - 声明 vector 的 protected 指针的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60253413/

10-10 18:18