简体   繁体   English

简单的模板化函数可以转换std :: vectors-“非法使用此类型作为表达式”

[英]Simple templated function to convert std::vectors - “illegal use of this type as an expression”

I wrote a quick method to convert std::vectors from one type to another: 我写了一个快速方法将std :: vectors从一种类型转换为另一种类型:

template<class A, class B>
vector<B> ConvertSTDVector_AToB(vector<A> vector)
{
    vector<B> converted_vector;

    for(unsigned int i= 0; i< vector.size(); i++)
        converted_vector.push_back(vector[i]);

    return converted_vector;
}

But the compiler errors with "error C2275: 'B' : illegal use of this type as an expression" on the line after the opening bracket. 但是编译器在左括号后的行显示“错误C2275:'B':非法使用此类型作为表达式”错误。 At first I thought somehow 'B' was defined elsewhere, but changing both template type names results in the same error. 起初,我认为在其他地方以某种方式定义了“ B”,但是更改两个模板类型名称都会导致相同的错误。 Then I thought that something strange was going on with the types. 然后我认为这些类型正在发生一些奇怪的事情。 But even making both template parameters ints doesn't change anything. 但是即使将两个模板参数都设置为int都不会改变任何东西。

I can't for the life of me see what's wrong with this method. 我一辈子都看不到这种方法有什么问题。 (Although I feel like I might just be blind to something obvious, at this point). (尽管现在我觉得自己可能对明显的东西视而不见)。

Simply rename the parameter name. 只需重命名参数名称即可。 Its name hides the std::vector name. 它的名称隐藏了std :: vector名称。

Or write the erroneous line the following way 或通过以下方式写错行

class vector<B> converted_vector;

that is use the elaborated type name for std::vector that to distinguish it from object (parameter) vector. 即使用std :: vector的详细类型名称将其与对象(参数)矢量区分开。

The code of the function can be written in different ways. 该函数的代码可以用不同的方式编写。 For example 例如

template<class A, class B>
vector<B> ConvertSTDVector_AToB(vector<A> vector)
{
    class vector<B> converted_vector( vector.begin(), vector.end() );

    return converted_vector;
}

pr PR

template<class A, class B>
vector<B> ConvertSTDVector_AToB(vector<A> vector)
{
    class vector<B> converted_vector;

    converted_vector.assign( vector.begin(), vector.end() );
    return converted_vector;
}

ans so on. 等等。

You don't really have to do such a function, instead all you have to do is to use std::copy provided that the objects you want to convert are convertible to each other by providing an overloaded conversion operator. 您实际上并不需要做这样的功能,相反,您所要做的就是使用std::copy前提是您要转换的对象通过提供重载的转换运算符可以相互转换。 Just like the example below: 就像下面的例子:

#include <vector>
#include <iostream>
#include <algorithm>

class B;

class A {
  friend std::ostream& operator<<(std::ostream &out, A const &a);
  int id;
public:
  A() : id(0) { }
  A(int const _id) : id(_id) { }
  operator B() const; 
};

class B {
  friend std::ostream& operator<<(std::ostream &out, B const &b);
  int id;
public:
  B() : id(0) { }
  B(int const _id) : id(_id) { }
  operator A() const { return A(id); }
};

A::operator B() const { return B(id); }

std::ostream& operator<<(std::ostream &out, A const &a) { return (out << a.id); }
std::ostream& operator<<(std::ostream &out, B const &b) { return (out << b.id); }

int main() {
  std::vector<B> bv{B(1), B(2), B(3), B(4), B(5)};
  std::vector<A> av(bv.size());
  std::vector<B> bbv(bv.size());
  std::copy(bv.begin(), bv.end(), av.begin());
  for(auto i : av) std::cout << i << " ";
  std::cout << std::endl;
  std::copy(av.begin(), av.end(), bbv.begin());
  for(auto i : bbv) std::cout << i << " ";
  std::cout << std::endl;  
  return 0;
}

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM