2010-10-10 5 views
0

J'écris une classe de modèle "PersonalVec" - un vecteur avec d'autres qualités. En tant que modèle de classe, j'ai mis tout dans le .hpp, comme suit:erreurs de compilation bizarres - vecteur et modèles

#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_ */ 

Je ne comprends pas ce qui est si mal avec mon code que le compilateur conserve des erreurs en criant comme:

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 

J'apprécierais vraiment votre aide, puisque je deviens vraiment fou ici.

Répondre

4

Typo ici:

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

Il devrait être vecteur.

0

Je mets votre code par Clang et obtenu

main1.cpp:18:7: error: no template named 'vecotr' in namespace 'std'; did you mean 'vector'? 
std::vecotr<T> _myVec; 
~~~~~^~~~~~ 
     vector 
In file included from main1.cpp:4: 
In file included from /usr/include/c++/4.5.1/vector:64: 
/usr/include/c++/4.5.1/bits/stl_vector.h:170:11: note: 'vector' declared here 
    class vector : protected _Vector_base<_Tp, _Alloc> 
     ^
main1.cpp:43:13: error: functions that differ only in their return type cannot be overloaded 
const int& operator[](size_type index){ 
      ^
main1.cpp:38:7: note: previous declaration is here 
int& operator[](size_type index){ 
    ^
main1.cpp:32:19: error: called object type 'int' is not a function or function pointer 
    int rand = rand()%_myVec.size(); 
       ~~~~^ 
3 errors generated. 
Questions connexes