声明向量的受保护指针的问题

问题描述 投票:-2回答:1

我正在尝试在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.h和这里enter link description here中找到。

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

c++ c++11 pointers vector compiler-errors
1个回答
0
投票

错误消息足够清晰。例如,在代码中的某处,您试图将指针指向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>,而不是指针类型。

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