I'm trying to do a custom dynamic array class using templates, and currently the .h
file looks like this:
//
// Created by juanfra on 10/10/22.
//
#ifndef IMAGENES_VDINAMICO_H
#define IMAGENES_VDINAMICO_H
#include <cmath>
#include <climits>
using namespace std;
template <typename T>
class VDinamico {
private:
int tamL, tamF;
T *contenedor;
public:
// CONSTRUCTORES
VDinamico<T>();
VDinamico<T>(int tamLog);
VDinamico<T>(const VDinamico<T> &origen);
VDinamico<T>(const VDinamico<T> &origen, unsigned int posicionInicial, unsigned int numElementos);
// OPERADORES
VDinamico &operator=(const VDinamico<T> &vector);
T operator[](int i);
// FUNCIONES
void insertar(const T &dato, unsigned int pos = UINT_MAX);
T borrar (unsigned int pos = UINT_MAX);
bool isGreater(const T& i, const T& j);
void ordenar();
void ordenarRev();
// GETTERS Y SETTERS
int getTamL() const;
void setTamL(int tamL);
int getTamF() const;
void setTamF(int tamF);
T *getContenedor() const;
void setContenedor(T *contenedor);
};
#endif //IMAGENES_VDINAMICO_H
While the .cpp
file looks like this:
//
// Created by juanfra on 10/10/22.
//
#include "VDinamico.h"
template<class T>
VDinamico<T>::VDinamico() {
tamF = tamL = 0;
contenedor = new T[tamF];
}
template<class T>
VDinamico<T>::VDinamico(int tamLog) {
tamL = tamLog;
tamF = pow(tamL, 2.0);
contenedor = new T[tamF];
}
template<class T>
VDinamico<T>::VDinamico(const VDinamico<T> &origen) {
tamL = origen.tamL;
tamF = origen.tamF;
contenedor = new T[tamF];
for (int i = 0; i < tamL; i++) {
contenedor[i] = origen.contenedor[i];
}
}
template<class T>
VDinamico<T>::VDinamico(const VDinamico<T> &origen, unsigned int posicionInicial, unsigned int numElementos) {
tamL = numElementos;
tamF = pow(tamL, 2.0);
contenedor = new T[tamF];
for (int i = 0; i < numElementos; i++) {
contenedor[i] = origen.contenedor[i + posicionInicial];
}
}
// OPERADORES
template<class T>
VDinamico<T> &VDinamico<T>::operator=(const VDinamico<T> &vector) {
tamL = vector.tamL;
tamF = vector.tamF;
contenedor = new T[tamF];
T *contenedorAux = vector.contenedor;
for (int i = 0; i < tamL; i++) {
contenedor[i] = contenedorAux[i];
}
}
template<class T>
T VDinamico<T>::operator[](int i) {
return contenedor[i];
}
// FUNCIONES
template<class T>
void VDinamico<T>::insertar(const T &dato, unsigned int pos) {
tamF = this->getTamF();
tamL = this->getTamL();
if (tamF == tamL) { //< Si los tamanyos son iguales, duplico tamF e inserto
T *vAux = new T[tamF * 2];
for (int i = 0; i < tamL; i++) {
vAux[i] = contenedor[i];
}
delete[]contenedor;
contenedor = vAux;
}
if (pos == UINT_MAX) { //< Si la posición no se indica, insertamos por el final
contenedor[tamL++] = dato;
} else { //< Si la posición se indica, abrimos un hueco en la posición indicada copiando el resto del vector
for (int i = tamL - 1; i >= pos; i--) {
contenedor[i + 1] = contenedor[i];
}
}
this->setTamL(tamL + 1);
}
template<class T>
T VDinamico<T>::borrar(unsigned int pos) {
tamF = this->getTamF();
tamL = this->getTamL();
if (tamL*3 < tamF){ //< Si tamL es 3 veces menor que tamF, hago un nuevo vector de tamF/2
T *vAux = new T[tamF/2];
for (int i = 0; i < tamL; i++){
vAux[i] = contenedor[i];
}
delete[]contenedor;
contenedor = vAux;
}
if (pos == UINT_MAX){
return contenedor[tamL--];
} else {
for (int i = pos; i < tamL; i++){
contenedor[i] = contenedor[i+1];
}
}
this->setTamL(tamL--);
return contenedor;
}
template<class T>
bool VDinamico<T>::isGreater(const T& i, const T& j) {
return (i.getId() > j.getId());
}
template<class T>
void VDinamico<T>::ordenar() {
sort(contenedor, contenedor + tamL);
}
template<class T>
void VDinamico<T>::ordenarRev() {
sort(contenedor, contenedor + tamL, isGreater());
}
// GETTERS Y SETTERS
template<class T>
int VDinamico<T>::getTamL() const {
return tamL;
}
template<class T>
void VDinamico<T>::setTamL(int tamL) {
VDinamico::tamL = tamL;
}
template<class T>
int VDinamico<T>::getTamF() const {
return tamF;
}
template<class T>
void VDinamico<T>::setTamF(int tamF) {
VDinamico::tamF = tamF;
}
template<class T>
T *VDinamico<T>::getContenedor() const {
return contenedor;
}
template<class T>
void VDinamico<T>::setContenedor(T *contenedor) {
VDinamico::contenedor = contenedor;
}
My problem is that, in the .h
, everytime I use <T>
, it throws me an error like:
error: expected unqualified-id before ‘)’ token in line 20
error: expected unqualified-id before ‘int’ in line 22
error: expected unqualified-id before ‘const’ in line 24
and so on...
What am I doing wrong?