undefined reference to Vector<String> g++ link error

Discussion in 'C++' started by dany2704, Feb 7, 2013.

  1. dany2704

    dany2704 New Member

    Joined:
    Feb 4, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    have compiled the dependent files of my source file mycpp.c and linked the files in the order which they have used .All the dependant files are available in the same folder.
    **//Compilation**
    $ g++ -c -w -Wno-deprecated String.c Vector.c DPStream.c CJ_Base.c mycpp.c
    **//Linking**
    $ g++ -g -o myexe String.o Vector.o DPStream.o CJ_Base.o mycpp.o
    Vector.h
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    #include "String.h"
    #include "Storage.h"
    
    
    template <class Object>
    class Vector : public Storage{
    
    public:
    // ctors & dtor
    Vector ();
    Vector (int);
    ~Vector();
    
    // overloaded operators
    Object& operator[] (int nSubscript);
    void operator<<(Object &);
    void operator<<(char *);
    
    // access methods
    int Count(){return _iCurrElem;};
    int Print(ofstream &);
    void Flush();
    Resize(int);
    
    int IsType() {return VectorType;};
    
    private:
    long _iCurrElem;
    Object *moj;
    long _iUpperBound;
    
    void _reserve(int);
    };
    #endif
    
    Vector.c

    Code:
    #include"Vector.h"
    
    template <class Object>
    Vector<Object>::Vector ()
    {
    moj = new Object[3];
    }
    
    template <class Object>
    Vector<Object>::Vector (int e)
    {
    moj = new Object[e];
    }
    
    template <class Object>
    Vector<Object>::~Vector ()
    {
    delete[] moj;
    }
    
    template <class Object>
    Vector<Object>::operator<<(Object &)
    {
    //stmt
    }
    
    template <class Object>
    Vector<Object>::operator<<(char* ch)
    {
    //stmt
    }
    
    template <class Object>
    Vector<Object>::Print(ofstream &foutput)
    {
    //stmt
    }
    
    Included the header files in the below order in mycpp.h
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include "String.h"
    #include "Vector.h"
    #include "DPStream.h"
    #include "CJ_Base.h"
    
    g++ compiler throws an error

    DPStream.o: In function `DPStream::eek:perator<<(Vector<String>&)':
    DPStream.c:(.text+0x396): undefined reference to `Vector<String>::Print(std::basic_ofstream<char, std::char_traits<char> >&)'
    mycpp.o: In function `mycpp::ProcessFile()':
    mycpp.o:(.text+0x24e): undefined reference to `Vector<String>::eek:perator<<(String&)'
    mycpp.o:(.text+0x2e5): undefined reference to `Vector<String>::eek:perator<<(char*)'
    mycpp.o:(.text+0x310): undefined reference to `Vector<String>::Flush()'
    mycpp.o:(.text+0x32b): undefined reference to `Vector<String>::eek:perator<<(String&)'
    mycpp.o:(.text+0x346): undefined reference to `Vector<String>::eek:perator<<(String&
    mycpp.o: In function `mycpp::~mycpp()':
    mycpp.o:(.text+0x24e): undefined reference to `Vector<String>::eek:perator<<(String&)'


    Could you please help me to resolve the issue
    Thanks for looking into this
     
  2. DRK

    DRK New Member

    Joined:
    Apr 13, 2012
    Messages:
    44
    Likes Received:
    3
    Trophy Points:
    0
    1. C++ source files should have .cpp extension, not .c .
    2. Include files for C++ standard library don't require .h extension:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    3. Include files inherited from C standard library have c prefix in their names:
    Code:
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    4. You missed return type in operators and methods definitions:
    Code:
    template <class Object>
    void Vector<Object>::operator<<(Object &)
    Code:
    template <class Object>
    void Vector<Object>::operator<<(char* ch)
    Code:
    template <class Object>
    int Vector<Object>::Print(ofstream &foutput)
    5. Semicolon after method definition is dispensable:
    Code:
    int Count(){return _iCurrElem;}
    int IsType() {return VectorType;}
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice