c++ - 奇怪的编译错误 - vector 和模板

标签 c++ templates vector

<分区>

我正在编写一个“PersonalVec”模板类——一个具有其他特性的 vector 。 作为模板类,我已经在.hpp 中实现了所有内容,如下所示:

#ifndef PERSONALVEC_HPP_
#define PERSONALVEC_HPP_

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cassert>


using namespace std;

template <class T, class PrnT>
class PersonalVec{ 

 std::vecotr<T> _myVec;

public:

 typedef typename vector<T>::size_type size_type;

 PersonalVec():_myVec(){
  srand(time(NULL));
 }

 void push_back(T element){
  _myVec.push_back(element);
  if(_myVec.size()!= 0){//TODO: change to 1?!!?
   //pick a random number between 0..n-1
   int rand = rand()%_myVec.size();
   //swap
   std::swap(_myVec.at(rand), _myVec.at(_myVec.size()-1));
  }
 }

 int& operator[](size_type index){
  assert(index>=0 && index<_myVec.size());
  return _myVec.at(index);
 }

 const int& operator[](size_type index){
   assert(index>=0 && index<_myVec.size());

   const T element= _myVec.at(index);
   return element;
 }

 void erase(size_type index){
  assert(index>=0 && index<_myVec.size());
  if(index < _myVec.size()-1){
   std::swap(_myVec.at(index) , _myVec.at(_myVec.size()-1));
  }
  _myVec.pop_back();
 }

 void print(){
  for(size_type i=0; i<_myVec.size();++i){
   cout << PrnT()(_myVec.at(i))<<" ";
  }
  cout << endl;
 }

};



#endif /* PERSONALVEC_HPP_ */

我不明白我的代码有什么问题导致编译器不断报错,例如:

PersonalVec.hpp:18: error: ISO C++ forbids declaration of ‘vecotr’ with no type
PersonalVec.hpp:18: error: invalid use of ‘::’
PersonalVec.hpp:18: error: expected ‘;’ before ‘<’ token
PersonalVec.hpp:43: error: ‘const int& PersonalVec<T, PrnT>::operator[](typename std::vector<T, std::allocator<_Tp1> >::size_type)’ cannot be overloaded
PersonalVec.hpp:38: error: with ‘int& PersonalVec<T, PrnT>::operator[](typename std::vector<T, std::allocator<_Tp1> >::size_type)’
PersonalVec.hpp: In member function ‘void PersonalVec<T, PrnT>::push_back(T)’:
PersonalVec.hpp:29: error: ‘_myVec’ was not declared in this scope
PersonalVec.hpp:32: error: ‘rand’ cannot be used as a function
PersonalVec.hpp: In member function ‘int& PersonalVec<T, PrnT>::operator[](typename std::vector<T, std::allocator<_Tp1> >::size_type)’:
PersonalVec.hpp:39: error: ‘_myVec’ was not declared in this scope
PersonalVec.hpp: In member function ‘const int& PersonalVec<T, PrnT>::operator[](typename std::vector<T, std::allocator<_Tp1> >::size_type)’:
PersonalVec.hpp:44: error: ‘_myVec’ was not declared in this scope
PersonalVec.hpp: In member function ‘void PersonalVec<T, PrnT>::erase(typename std::vector<T, std::allocator<_Tp1> >::size_type)’:
PersonalVec.hpp:51: error: ‘_myVec’ was not declared in this scope
PersonalVec.hpp: In member function ‘void PersonalVec<T, PrnT>::print()’:
PersonalVec.hpp:59: error: ‘_myVec’ was not declared in this scope

我真的很感谢你的帮助,因为我真的要疯了。

最佳答案

此处输入错误:

std::vecotr<T> _myVec;
     ^^^^^^

它应该是 vector 。

关于c++ - 奇怪的编译错误 - vector 和模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3900640/

相关文章:

c++ - int a=int(); C++98 中会发生什么?

c++ - 缩短作为模板参数传递到 CRTP ("pass me"中的长模板派生类)

c++ - C++ 中字符串 vector 的 reserve() 函数

c++ - 复制构造函数在相同情况下失败并成功

c++ - 无法编辑 TADOTable 中的数据

C++模板函数错误: template-id does not match any template declaration

c# - 如何在 ms word 模板中放置标签并用数据库中的数据填充它们

r - 如何向量化从列索引向量扩展压缩稀疏矩阵?

c++ - 将字符串添加到 vector<const char *> 会导致 strtok() 出现奇怪的行为

c++ - 向同一进程中的另一个线程发出信号