0

I 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
#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
---------
#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 orde in mycpp.h

#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"

But still the g++ compiler throws an error Pasted below the link error

        DPStream.o: In function `DPStream::operator<<(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>::operator<<(String&)'
    mycpp.o:(.text+0x2e5): undefined reference to `Vector<String>::operator<<(char*)'
    mycpp.o:(.text+0x310): undefined reference to `Vector<String>::Flush()'
    mycpp.o:(.text+0x32b): undefined reference to `Vector<String>::operator<<(String&)'
    mycpp.o:(.text+0x346): undefined reference to `Vector<String>::operator<<(String&
    mycpp.o: In function `mycpp::~mycpp()':
    mycpp.o:(.text+0x24e): undefined reference to `Vector<String>::operator<<(String&)'

Could you please help me to resolve the issue

user2040497
  • 15
  • 2
  • 5

2 Answers2

1

You don't show us where and how Vector is defined, but just a guess: you're compiling a Vector.c. If this file contains the implementation of a template, you're doing it wrong; the implementation of a template must be visible when the template is instantiated. You can either put it in the header, or include the source from the header, but you don't compile the source.

Also, most compilers consider a .c to be C source, and not C++. You almost certainly want to change the names of your sources to .cpp or .cc. While you're at it, I'd change the names of the pure C++ headers as well. While ubiquitous, using the same naming convention for C headers and for pure C++ headers can be confusing.

And finally, the <name.h> forms of the C standard headers are not standard; if they're present (they usually aren't), they should refer to older, pre-standard versions of the library. (Judging from your error messages, this is apparently not the case.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • :Thanks for the information.I have updated my question i.e. provided the code of "vector.c" and "vector.h" Could you please check and advise me – user2040497 Feb 07 '13 at 17:23
0

All your errors are related to template functions.

Remember that when defining a template you must put the definitions in the header, not in a .cpp file. Other modules need the definition to perform their own instantiations of the template.

So a quick fix would be to do a simple #include "Vector.c" at the end of Vector.h.

Alternatively put your whole template implementation inside .h files.


(Also please use .cpp not .c for C++ source files; using hpp for C++ headers is nice too)

Kos
  • 70,399
  • 25
  • 169
  • 233