I have two errors that I am not sure how to fix, please let me know what I need to fix. Here are the errors:
Error 1 error C2244: 'myVector<T>::at' : unable to match function definition to an existing declaration
Error 2 error C2244: 'myVector<T>::operator []' : unable to match function definition to an existing declaration
I know that it is in the implementation for both the 'at' and 'operator[] overload'.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
//Declaring constant
constint VECTOR_CAP = 2;
template <class T>
class myVector
{
private:
//Setting data members
T* vectorData;
int cap;
int numElements;
public:
//Default constructor
//Purpose: Creates a vector
//Parameters: None
//Returns: None
myVector();
//Parameterized constructor
//Purpose: Creates a vector capacity of n
//Parameters: None
//Returns: None
myVector(const T&);
//Copy Constructor
//Purpose: Copy data into vector
//Parameters: myVector object
//Returns: None
myVector(const myVector& copy)
{
numElements = copy.numElements;
vectorData = new T [numElements];
for (int i = 0; i < numElements; i++)
{
this->vectorData[i] = copy.vectorData[i];
}
}
//Destructor
//Purpose:Deletes any dynamically allocated storage
//Parameters: None
//Returns: None
~myVector();
//Size function
//Purpose: returns the size of your vector
//Parameters: None
//Returns: The size of your vector as an integer
int size() const;
//Capacity function
//Purpose: Returns the capacity of the vector
//Parameters: None
//Returns: Maximum value that your vector can hold
int capacity() const;
//Clear function
//Purpose: Deletes all of the elements from the vector and resets its size to zero and its capacity to two; thus becomming empty
//Parameters: None
//Returns: None
void clear();
//push_back function
//Purpose: Adds the integer value n to the end of the vector
//Parameters: Takes a integer to be placed in the vector
//Returns: None
void push_back(const T& n)
{
//If statement to handle if array is full
if (numElements == cap)
{
//Doubling the capacity
cap = cap * VECTOR_CAP;
//Allocating new array
T* newVectorData = new T[cap];
//Copying data
for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i];
//Deleting previous data
delete[] vectorData;
//Pointing to new data
vectorData = newVectorData;
}
//Storing data
vectorData[numElements++] = n;
}
//at function
//Purpose: Returns the value of the element at position n in the vector
//Parameters: None
//Returns: Returns your current place with the vector
T& at(std::size_t);
//assignment
//Purpose: Overload the = operator
//Parameters: The two myVector objects we want to assign
//Returns: The assignment
myVector operator=(const myVector&);
void pop_back();
int last();
T& operator[](std::size_t);
};
//Independant Functions
template <typename T>
int myVector<T>::capacity() const
{
return cap;
}
template <typename T>
int myVector<T>::at(std::size_t n)
{
return vectorData[n];
}
template <typename T>
myVector<T> myVector<T>::operator[](std::size_t)
{
return T&;
}
template <typename T>
myVector<T> myVector<T>::operator=(const myVector& rho)
{
//Test for assingment
if (this == &rho)
{
return *this;
}
//Delete lho
delete[] this->vectorData;
//Creating new array to fit rho data
cap = rho.cap;
this->vectorData = newint[cap];
//Copying data
for (int i = 0; i < numElements; i++)
{
this->vectorData[i] = rho.vectorData[i];
}
//Returning myVector object
return *this;
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const myVector<T>& rho)
{
for (int n = 0; n < rho.size(); n++)
{
out << rho.at(n);
}
return out;
}
Okay this is all making a lot of sense thanks for walking me through this guys. The one thing that I don't understand is what operator[] should return, with n.
That makes sense, so I still need definition for my constructor, destructor, size function, pop_back function, and last functions. I will post if I need more questions there. Thanks for everyone's help.
I am trying to do a pop_back function and am getting errors, I am not sure how to implement it properly, it is supposed to remove last integer pushed into vector, reducing its size by one, and if the vector is empty it will do nothing. Here is what I have so far.
1 2 3 4 5 6 7 8 9
template <typename T>
void myVector<T>::pop_back()
{
while (!myVector.empty())
{
sum += myVector.back();
myVector.pop_back();
}
}