函数返回类型是c++中的vector

function return type is vector in c++

本文关键字:中的 vector c++ 返回类型 函数      更新时间:2023-10-16

我希望向量作为返回类型的函数在我的代码,如

class SocketTransportClient{
    void sendData(RMLInfoset *info){
        vector<unsigned long>::iterator blockIterator;
        vector<unsigned long> vectBlock=info->getRML(); // error :  error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>'
    }
}
class RMLInfoset  {
    vector<unsigned int> RMLInfoset::getRML(){
        return  vectDataBlock;
    }
}

但显示错误'无法从'std::vector<_Ty>'转换为'std::vector<_Ty>' '所以请大家帮帮我,谢谢。

嗯,部分问题是您从未在Myfun中实际构建vector v

std::vector<int> Myfun() {
  std::vector<int> v;
  return v;
}
编辑:

在问题编辑之后,仍然有一个小问题-您需要将向量声明为std::vector<int>,而不仅仅是vector<int>,如上所述。这是因为vector位于std命名空间中。

你的函数被声明为返回vector<unsigned int>,但你实际上试图将结果分配给vector<unsigned long>。它们是不同的类型,不兼容赋值。修改函数声明:

  vector<unsigned long> RMLInfoset::getRML(){

您还需要更改vectDataBlock的类型。基本上,决定你想要使用的向量类型和是一致的

你有三个错误

你没有使用#include <vector>

2)应该是std::vector<int> v;而不是vector<int> v;