I have a question that I have been trying to figure out for a few days, I don't really understand how to make this program compile. Firstly the program had all of the values set, but now I am required to make it a template based. I have a great list of errors(about 9) but I am just looking for some guidance from someone who has experience in this. Thanks for you help.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
usingnamespace std;
//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 T& copy)
{
numElements = copy.numElements;
vectorData = newint[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
T size() const;
//Capacity function
//Purpose: Returns the capacity of the vector
//Parameters: None
//Returns: Maximum value that your vector can hold
T 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&);
//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(T&) const;
//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();
myVector operator[](T&);
};
//Independant Functions
//streamInsertionOverload
//Purpose: Overload the stream insertion operator
//Parameters: The stream object to read from, and object to read to
//Returns:The stream
//template <typename T>
//ostream& operator<<(ostream& out, const T& rho);
template <typename T>
int myVector<T>::capacity() const
{
return cap;
}
template <typename T>
int myVector<T>::pushback(T&) const
{
//If statement to handle if array is full
if (numElements == cap)
{
//Doubling the capacity
cap = cap * VECTOR_CAP;
//Allocating new array
int* newVectorData = newint[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;
}
template <typename T>
ostream& operator<<(ostream& out, const myVector<T>& rho)
{
for (int n = 0; n < rho.size(); n++)
{
out << rho.at(n);
}
return out;
}
#include <iostream>
#include "vectorHeader.h"
usingnamespace std;
int main()
{
constchar START = 'A';
constint MAX = 12;
// create a vector of doubles
myVector<char> vectD;
// push some values into the vector
for (int i = 0; i < MAX; i++)
{
vectD.push_back(START + i);
}
// remove the last element
vectD.pop_back();
// add another value
vectD.push_back('Z');
// test memory management
myVector<char> vectD2 = vectD;
// display the contents
cout << "\n[";
for (int i = 0; i < vectD2.size() - 1; i++)
{
cout << vectD2[i] << ", ";
}
cout << "..., " << vectD2.last() << "]\n";
system("PAUSE");
return 0;
}
Error list
error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) LINE 31 (driver)
6 IntelliSense: no operator "[]" matches these operands
operand types are: myVector<char> [ int ] LINE 31 (driver)
error C2535: 'myVector<T>::myVector(const T &)' : member function already defined or declared LINE 39 (header)
error C2244: 'myVector<T>::capacity' : unable to match function definition to an existing declaration LINE 112 (header)
error C2039: 'pushback' : is not a member of 'myVector<T>' LINE 133 (header)
error C2535: 'myVector<char>::myVector(const T &)' : member function already defined or declared LINE 39 (header)
//Size function
//Purpose: returns the size of your vector
//Parameters: None
//Returns: The size of your vector as an integer
T size() const;
`T' is not an integer
T may be anything, like a string, a person, an integer. ¿what would be the meaning of saying that your size is Mitsubishi?
You have that error in several places. (at(), capacity(), operator[])
> 'myVector<T>::myVector(const T &)' : member function already defined or declared
¿what's the difference between line 33 and 39?
> 'pushback' : is not a member of 'myVector<T>'
typo. You've declared push_back
> Error list
¿your crappy compiler does not provide line numbers?
I have updated the list to include the line numbers, I will fix what you have pointed out. Thanks for your help. Let me know if you see anything else with the line numbers provided.
Okay taking into consideration of all of the things said above, I have narrowed it down to one statement error, and I don't know how to declare it as a 'T'. The error is on line 42 when I declare a new int. How do I declare it so that the compiler allows it? thanks for your help. I also want to know if there is any other things that I can fix to look better. I will provide the header code because the driver code hasn't changed
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
usingnamespace std;
//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 = newint [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&)
{
//If statement to handle if array is full
if (numElements == cap)
{
//Doubling the capacity
cap = cap * VECTOR_CAP;
//Allocating new array
int* newVectorData = newint[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
int at(T&) const;
//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();
myVector operator[](const myVector&);
};
//Independant Functions
template <typename T>
int myVector<T>::capacity() const
{
return cap;
}
template <typename T>
ostream& operator<<(ostream& out, const myVector<T>& rho)
{
for (int n = 0; n < rho.size(); n++)
{
out << rho.at(n);
}
return out;
}
//vectorData = new int [numElements];
vectorData = new T [numElements];
As an advice, you could have done
1 2 3 4 5 6 7 8 9 10 11 12 13
class myVector{
public:
typedefint value_type;
//using value_type instead of int where it should
//...
};
//then convert to template
template<class T>
class myVector{
public:
typedef T value_type;
//the rest remain the same
};
oh I see, that makes sense. When I switched the int to T, I now have a great deal of undeclared identifiers on my driver code. Here are the errors, what can I do to fix these.