Thread: Linker error in templated vector class

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    80

    Linker error in templated vector class

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i'm not sure, but are you trying to build console or winapi? check your project.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    I am building a Win32 Console App, so as to mimic the Unix environment as much as possible. After developing on Windows, I transport to Unix, and recompile on g++. This has been my approach forever, and has never caused problems in the past, even with much more complex things. In fact, this program compiles on g++, but I have some bugs to work out, that I would MUCH prefer to do with the debugger in MSVC.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Ever try the debugger in KDevelop? It offers the same basic functionality (set breakpoints from the IDE, jump into, jump out of, step, etc).

    No winmain means you set up a windows project and are using conventional int main, instead of winmain or whatever. Make a console project from MSVC, and it should all work out.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    that's what i was referring to, what type of main you were using, if it matched your project. as silentstrike stated.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Ok, I'll give it a try...!

    We'll see what happens. Thanks

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Haha, yeah. Believe it or not, I had accidently created a regular Win App, nota console app. Thanks for your input guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2009, 12:56 PM
  2. Linker problem... no idea
    By cyreon in forum C Programming
    Replies: 2
    Last Post: 04-03-2009, 02:53 PM
  3. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 01:25 AM
  4. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  5. Linker error using system(*.*)
    By Winston4u in forum C Programming
    Replies: 5
    Last Post: 05-09-2003, 05:54 PM