Hey. I recently made a vector, and I thought everything was smooth sailing, however, I tried to construct an object of my vector class with typename std::string, and it didn't work. It gave me an assertion, and I can't seem to figure out why. Here is my code, up to my index operator:

#ifndef VECTOR_H

#define VECTOR_H

#include<iostream>

template <typename T>

class vector

{

public:

//Default Constructor

vector();

//Destructor

~vector();

//Copy Constructor

vector(const vector<T>&);

//Assignment Operator

vector<T>&operator=(const vector<T>&);

//Index Operator

T& operator[](unsigned int);

//Equivalence

bool operator==(const vector<T>&);

//Returns the actual number of elements

unsigned int size() const;

//Append a copy of an element to the end of vector<T>

void push_back(const T&);

//Remove last appended element

void pop_back();

//Clears all items

void clear();

//Reserves specified amount of memory for vector<T>

void reserve(unsigned int);

private:

unsigned int THE_SIZE;

unsigned int THE_ARRAY_CAPACITY;

T* theDataPtr;

};

const unsigned int INTIAL_Capacity = 10;

template <typename T>

vector<T>::vector()

{

theDataPtr = new T[INTIAL_Capacity];

THE_SIZE = 0;

THE_ARRAY_CAPACITY = INTIAL_Capacity;

}

template <typename T>

vector<T>::~vector()

{

delete theDataPtr;

}

template <typename T>

vector<T>::vector(const vector<T>& org)

{

T* TempPtr = new T[(org.THE_ARRAY_CAPACITY)];

for(unsigned int i=0; i<org.THE_SIZE; i++)

{

TempPtr[i] = org.theDataPtr[i];

}

(*this).THE_ARRAY_CAPACITY = org.THE_ARRAY_CAPACITY;

(*this).THE_SIZE = org.THE_SIZE;

(*this).theDataPtr = TempPtr;

}

template <typename T>

vector<T>&vector<T>::operator=(const vector<T>& rhs)

{

if((*this) == rhs)

{

return *this;

}

else

{

T* TempPtr = new T[(rhs.THE_ARRAY_CAPACITY)];

for(unsigned int i=0; i<rhs.THE_SIZE; i++)

{

TempPtr[i] = rhs.theDataPtr[i];

}

(*this).THE_SIZE = rhs.THE_SIZE;

(*this).THE_ARRAY_CAPACITY = rhs.THE_ARRAY_CAPACITY;

delete[](*this).theDataPtr;

(*this).theDataPtr = TempPtr;

return *this;

}

}

template <typename T>

T& vector<T>::operator[](unsigned int pos)

{

if(pos < THE_ARRAY_CAPACITY && pos < THE_SIZE)

{

return theDataPtr[pos];

}

else

{

try

{

if(pos >= THE_SIZE) throw "Out of Range";

}

catch ( char*str)

{

std::cerr << "Exception: " << str << std::endl;

}

return theDataPtr[(pos+THE_ARRAY_CAPACITY+THE_SIZE)];

}

}

Then this is my main.cpp:

#include "vector.h"

#include <iostream>

#include <string>

int main()

{

vector<string> yep;

return EXIT_SUCCESS;

}

However, this won't work. I require assistance, please.

#ifndef VECTOR_H

#define VECTOR_H

#include<iostream>

template <typename T>

class vector

{

public:

//Default Constructor

vector();

//Destructor

~vector();

//Copy Constructor

vector(const vector<T>&);

//Assignment Operator

vector<T>&operator=(const vector<T>&);

//Index Operator

T& operator[](unsigned int);

//Equivalence

bool operator==(const vector<T>&);

//Returns the actual number of elements

unsigned int size() const;

//Append a copy of an element to the end of vector<T>

void push_back(const T&);

//Remove last appended element

void pop_back();

//Clears all items

void clear();

//Reserves specified amount of memory for vector<T>

void reserve(unsigned int);

private:

unsigned int THE_SIZE;

unsigned int THE_ARRAY_CAPACITY;

T* theDataPtr;

};

const unsigned int INTIAL_Capacity = 10;

template <typename T>

vector<T>::vector()

{

theDataPtr = new T[INTIAL_Capacity];

THE_SIZE = 0;

THE_ARRAY_CAPACITY = INTIAL_Capacity;

}

template <typename T>

vector<T>::~vector()

{

delete theDataPtr;

}

template <typename T>

vector<T>::vector(const vector<T>& org)

{

T* TempPtr = new T[(org.THE_ARRAY_CAPACITY)

for(unsigned int i=0; i<org.THE_SIZE; i++)

{

TempPtr[i] = org.theDataPtr[i];

}

(*this).THE_ARRAY_CAPACITY

(*this).THE_SIZE = org.THE_SIZE;

(*this).theDataPtr = TempPtr;

}

template <typename T>

vector<T>&vector<T>::opera

{

if((*this) == rhs)

{

return *this;

}

else

{

T* TempPtr = new T[(rhs.THE_ARRAY_CAPACITY)

for(unsigned int i=0; i<rhs.THE_SIZE; i++)

{

TempPtr[i] = rhs.theDataPtr[i];

}

(*this).THE_SIZE = rhs.THE_SIZE;

(*this).THE_ARRAY_CAPACITY

delete[](*this).theDataPtr

(*this).theDataPtr = TempPtr;

return *this;

}

}

template <typename T>

T& vector<T>::operator[](unsi

{

if(pos < THE_ARRAY_CAPACITY && pos < THE_SIZE)

{

return theDataPtr[pos];

}

else

{

try

{

if(pos >= THE_SIZE) throw "Out of Range";

}

catch ( char*str)

{

std::cerr << "Exception: " << str << std::endl;

}

return theDataPtr[(pos+THE_ARRAY_

}

}

Then this is my main.cpp:

#include "vector.h"

#include <iostream>

#include <string>

int main()

{

vector<string> yep;

return EXIT_SUCCESS;

}

However, this won't work. I require assistance, please.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

On some compilers it might work for POD types, but not for complex types.

std::string is a complex type.

A POD type is a type that does not need a constructor or destructor, and that has only POD type members.