Hi guys,
I'm writing a rather basic templated vector class. I'm compiling in MSVC++ 6.0. I'm getting the "LNK2001: unresolved external symbol _WinMain@16" error when I attempt to compile. I've included my code below. I'm sure some of my functions may be a little off, but I can't correct that until I can run the debugger, and of course, test cases. Let me know if you see anything stupid, or otherwise, thanks!
my_vec.h:
Code:
#include<iostream>
#include<iomanip>
using namespace std;
template<class T>
class my_vec{
//class requires that the operator = is defined for T.
private:
T *array; // pointer to the vector
int back; // index of last element. -1 if vector empty
int max_index; // last available index. -1 if vector invalid
void increase() // increases the size of the vector if needed.
{ if(back == -1) { back = 0; max_index = 32; } else max_index = max_index * 2 - 1; }
public:
// Misc useful
typedef T* iterator;
void printMe();
// Constructors
my_vec(); // back=max_index=-1. array=Null.
my_vec(int); // put in "int" elements, uninitialized, back=-1
// Destructor
~my_vec(); // free the array
// General purpose
int size() // return current size
{ return(back+1); }
iterator begin() // return iterator to start of array
{ iterator i = array; return i; }
iterator end() // return iterator to 1 past last elem of array.
{ iterator i = &(array[back + 1]); return i; }
// add and remove
void push_back(T); // add to end of array
void pop_back(); // remove value at end of array (note: is void !!!)
void erase(iterator);
void insert(iterator,T); // insert element into location pointed to
// by the iterator, shifting rest forward.
// operator overload for indexing, both rhs and lhs!
T& operator[](int i)
{ return array[i]; }
};
template <class T>
void my_vec<T>::printMe()
{
cout << "* STRUCT " << endl;
cout << " max_index=" << max_index << endl;
cout << " back=" << back << endl;
if(max_index!=-1)
for(int i=0;i<=back;i++)
{
cout << setw(8) << array[i] << " ";
if( (i%8) ==7 )
cout<< endl;
}
else
cout << "BLANK"<<endl;
cout << endl;
return;
};
template <class T>
my_vec<T>::my_vec()
{
back = max_index = -1;
array = NULL;
}
template <class T>
my_vec<T>::my_vec(int n)
{
array = new T[n];
back = n - 1;
if (n <= 32)
{
max_index = 31;
}
else
{
max_index = n * 2;
}
}
template <class T>
my_vec<T>::~my_vec()
{
while (back + 1 > 0)
pop_back();
}
template <class T>
void my_vec<T>::push_back(T t)
{
if (back == max_index) increase();
back++;
T* temp = new T[back + 1];
temp[back] = t;
for (int i = 0; i < back; i++) temp[i] = array[i];
delete [] array;
array = temp;
}
template <class T>
void my_vec<T>::pop_back()
{
if (back != -1)
{
back--;
T* temp = new T[back + 1];
for (int i = 0; i < back + 1; i++) temp[i] = array[i];
delete [] array;
array = temp;
}
}
template <class T>
void my_vec<T>::erase(iterator i)
{
if (back != -1)
{
back--;
T* temp = new T[back + 1];
bool found = false;
for (int j = 0; j < back + 1; j++)
{
if (i != &(array[j]) && !found) temp[j] = array[j];
else if (found) temp[j] = array[j + 1];
else found = true;
}
delete [] array;
array = temp;
}
}
template <class T>
void my_vec<T>::insert(iterator i, T t)
{
if (back == max_index) increase();
back++;
T* temp = new T[back + 1];
bool found = false;
for (int j = 0; j < back + 1; j++)
{
if (i != &(array[j + 1]) && !found) temp[j] = array[j];
else if (found) temp[j] = array[j - 1];
else { found = true; temp[j] = t; }
}
delete [] array;
array = temp;
}
main1.cpp
Code:
#include<iostream>
#include<iomanip>
#include"my_vec.h"
using namespace std;
int main(int argc, char * argv[])
{
my_vec<int> vec;
my_vec<int>::iterator i;
vec.printMe();
vec.push_back(1);
cout << "1" << endl;
vec.push_back(2);
cout << "2" << endl;
vec.push_back(3);
cout << "3" << endl;
vec.push_back(4);
cout << "4" << endl;
vec.printMe();
vec[0]=vec[3];
vec.printMe();
i=vec.end()-2;
vec.insert(i,9);
vec.printMe();
i=vec.begin();
vec.erase(i);
vec.printMe();
cout << "All done!"<<endl;
return 0;
}