运行长度编码矢量

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

我以前多次在论坛上阅读,但今天我无法弄清楚为什么我的代码出错,所以如果有人能指出我为什么会收到这些错误,我会感激不尽。我是模板的新手,不明白为什么vectr在这里不止一次声明。我试图声明全局vectr来修复错误,但我觉得这不是正确的修复。

main.cpp:8:31: error: invalid declarator before _v_class printContent (vector<T> v)
                           ^
main.cpp:8:31: error: expected _)_ before _v_
main.cpp: In function _int main()_:
main.cpp:70:23: error: conflicting declaration _printContent vectr_printContent(vectr);
                   ^
main.cpp:49:20: error: _vectr_ has a previous declaration as _std::vector<double> vectr_
 vector<double> vectr;

这是我的代码,目标是运行长度将向量的内容编码为新向量,以便新向量将具有旧向量中的项目数和项目对。在我设法在此函数内部创建它之后,我将交换新的向量。

#include <iostream>
#include "vector"

using namespace std;

template<class T>
class printContent (vector<T> v) //Error 31 here
{

vector<pair<int, T> > vp; //declare a new vector with type T


...(Here vectr would go through a loop and vp would fill up with pairs, for now I will also print out the content of the new vector here instead of swapping it out.) 

}

int main()
{

vector<double> vectr; //Error 49

/*... (I fill in vectr with various numbers here, could be char or int if I declare vectr to be    char or int instead)*/

printContent(vectr); //Error 70
}
c++ templates vector declaration std-pair
1个回答
0
投票

不确定您是否需要模板类或实际上是否正在寻找打印矢量的函数?这是您编译后重新编写的代码。

资源

#include <vector>
#include <utility>

using namespace std;

template< class T >
class PrintContent
{
 public:
  PrintContent( vector< T > value )
  {
     //make vector of pairs here.
  }
 private:
  vector< pair< int, T > > m_values;
};

int main( int, char** )
{
  vector< double > value;

  PrintContent< double > object( value );

  return 0;
}

建立

g ++ -o homework homework.cpp

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