Error 1 error LNK2019: unresolved external symbol "public: __thiscall vector::my_vector<int>::~my_vector<int>(void)" (??1?$my_vector@H@vector@@QAE@XZ) referenced in function _main test.obj

Error 2 error LNK2019: unresolved external symbol "public: __thiscall vector::my_vector<int>::my_vector<int>(void)" (??0?$my_vector@H@vector@@QAE@XZ) referenced in function _main test.obj

Error 3 fatal error LNK1120: 2 unresolved externals C:\Users...\Documents\Visual Studio 2008\Projects\Iterator and Generic Vector Container\Debug\Iterator and Generic Vector Container.exe

if i have the code below and close my_vector<int> v1 then their are no errors which leads me to believe the errors are in my_vector class. and it probably deals with the begin() and end() in my_vector.cpp class not having a template type. That's what i think. Any help appreciated thanks again.

test program(test.cpp)

#include "my_vector.h"
#include "my_vector_iterator.h"

using namespace vector;

int main()
{
    my_vector<int> v1;
    vector_iterator<int> itr;
    return 0;
}

my_vector.h

//my_vector.h

#ifndef MY_VECTOR_H
#define MY_VECTOR_H

#include "my_vector_iterator.h"
#include "my_vector_iterator.cpp"

namespace vector
{
    template<class DT>
    class my_vector
    {
    public:
        //destructor
        ~my_vector();

        //copy constructor
        my_vector(const my_vector<DT>& b);

        //constructor that create an empty vector
        my_vector();

        /*constructor that create an vector with capacity specified 
        as a parameter but doesn't assign values to each element
        in the vector*/
        my_vector(unsigned int intial_Size);

        /*constructor that create a vector with capacity specified 
        as a parameter and assign all the elements the value 
        specified*/
        my_vector(unsigned int intial_Size, DT assign);

        //overloaded assignement operator
        my_vector& operator=(const my_vector<DT>& a);

        //returns total number of elements in an array
        int capacity();

        //returns number of elements in array that are filled
        int size();

        //adds an value at next position in vector
        void push_back(DT user_number);

        //prints out the vector that contains the numbers recursively
        void show_vector(unsigned int siz);

        //deletes the number from last element that stores a number
        void pop_back();

        //return value at the index specified by user
        DT at(size_t arrayindex);

        //return value at index specified by user
        DT operator[](size_t array_index);

        //return a iterator to first value in container
        vector_iterator<DT> begin();

        //return a iterator to value after one in container
        vector_iterator<DT> end();

    private:

        //array to store numbers
        DT* numbers;

        //total amount of elements that can be stored in array
        int capacit;

        //number of elements that are filled
        int siz;
    };
}
#endif

my_vector.cpp

//Implementation of functions in my_vector.h
#include "my_vector.h"
#include "my_vector_iterator.h"

namespace vector
{
    template<class DT>
    my_vector<DT>::~my_vector()
    {
        delete[] numbers;
    }

    template<class DT>
    my_vector<DT>::my_vector(const my_vector<DT>& b)
    {
        capacit=b.capacit;
        numbers=new DT[capacit];
        siz=b.siz;
        for(long j=0; j<siz; j++)
        {
            numbers[j]=b.numbers[j];
        }
    }

    template<class DT>
    my_vector<DT>& my_vector<DT>::operator=(const my_vector<DT>& a)
    {
        capacit=a.capacit;
        siz=a.siz;
        if(this!=&a)
        {
            DT* temp;
            temp=a.numbers;
            delete[] numbers;
            numbers=new DT[capacit];
            for(long g=0; g<siz; g++)
            {
                numbers[g]=temp[g];
            }
        }
        return *this;
    }

    template<class DT>
    my_vector<DT>::my_vector()
    {
        siz=0;
        capacit=0;
        numbers=NULL;
    }

    template<class DT>
    my_vector<DT>::my_vector(unsigned int intial_Size)
    {
        siz=0;
        capacit=intial_Size;
        numbers=new DT[capacit];
    }

    template<class DT>
    my_vector<DT>::my_vector(unsigned int intial_Size,DT assign)
    {
        siz=0;
        capacit=intial_Size;
        numbers=new DT[capacit];
        for(long b=0; b<capacit; b++)
        {
            numbers[b]=assign;
            siz++;
        }
    }

    template<class DT>
    DT my_vector<DT>::at(size_t arrayindex)
    {
        return numbers[arrayindex];
    }

    template<class DT>
    DT my_vector<DT>::operator [](size_t arra)
    {
        return numbers[arra];
    }

    template<class DT>
    void my_vector<DT>::push_back(DT user_number)
    {
        DT* temp;
        //empty vector
        if(capacit==0)
        {
            capacit=capacit+1;
            numbers=new DT[capacit];
            numbers[0]=user_number;
            siz++;
        }
        //last element doesn't contain a value
        else if(siz!=capacit)
        {
            numbers[siz]=user_number;
            siz++;
        }
        //last element contain a value so must expand the array
        else if(siz==capacit)
        {
            temp=numbers;
            numbers=NULL;
            delete[] numbers;
            numbers=NULL;
            capacit=capacit*2;
            numbers=new DT[capacit];
            for(long a=0; a<siz; a++)
            {
            numbers[a]=temp[a];
            }
            delete[] temp;
            numbers[siz]=user_number;
            siz++;
        }
    }

    template<class DT>
    int my_vector<DT>::capacity()
    {
        return capacit;
    }

    template<class DT>
    int my_vector<DT>::size()
    {
        return siz;
    }

    template<class DT>
    void my_vector<DT>::pop_back()
    {
        siz--;
    }

    template<class DT>
    void my_vector<DT>::show_vector(unsigned int siz)
    {
        if(siz!=1)
        {
            show_vector(siz-1);
        }
        cout<<numbers[siz-1]<<"\n"; 
    }

    template<class DT>
    vector_iterator<DT> my_vector<DT>::begin()
    {
        vector_iterator c;
        c.cursor=numbers;
        c.index=0;
        return c;
    }

    template<class DT>
    vector_iterator<DT> my_vector<DT>::end()
    {
        vector_iterator c;
        c.cursor=numbers;
        c.index=siz;
        return c;
    }
}

my_vector_iterator.h

#ifndef MY_VECTOR_ITERATOR_H
#define MY_VECTOR_ITERATOR_H


namespace vector
{
    template<class DT>
    class vector_iterator
    {
    public:
        //default constructor
        vector_iterator();

        //increment the pointer
        void operator++();

        //return value by derefencing 
        DT operator*();

        //checks on
        bool operator!=(vector_iterator<DT>* b);

    private:
        //used to get the data in the counter
        DT* cursor;

        //counter which says the current position
        int index;  
    };
}
#endif

my_vector.cpp

#include "my_vector_iterator.h"

namespace vector
{
    template<class DT>
    vector_iterator<DT>::vector_iterator()
    {
        cursor=0;
        index=0;
    }

    template<class DT>
    void vector_iterator<DT>::operator ++()
    {
        index++;    
    }

    template<class DT>
    DT vector_iterator<DT>::operator *()
    {
        return cursor[index];
    }

    template<class DT>
    bool vector_iterator<DT>::operator !=(vector_iterator<DT> *b)
    {
        if(this->cursor==b->cursor)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Recommended Answers

All 7 Replies

line 9, my_vector.h NEVER EVER include a *.cpp file in a header file. It doesn't work. You didn't say what compiler you are using but you have to compile all *.cpp files and then link the object files together to make a single executable program.

thanks will look into it.

so i still need to #include both the .h in my main, just move my code cpp code into my .h file. Do i still need a namespace? Not necessary correct? still need those #ifndef? Thanks

>>just move my code cpp code into my .h file.
You don't read very well, do you? The answer is NO NO NO.

thanks! It works.

>>just move my code cpp code into my .h file.
You don't read very well, do you? The answer is NO NO NO.

The template thing is kind of a funky in-between in which an accepted solution is to in fact put the active code in the header:

http://parashift.com/c++-faq-lite/templates.html#faq-35.12 ?

Notice that foo-impl.cpp #includes a .cpp file, not a .h file. If that's confusing, click your heels twice, think of Kansas, and repeat after me, "I will do it anyway even though it's confusing." You can trust me on this one. But if you don't trust me or are simply curious, the rationale is given earlier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.