link error undefined reference to vector<String>

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

g++ compiler throws an 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
Thanks for looking into this
Template classes have to be defined completely in the header.

How old is your compiler?

1
2
3
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h> 


That's pre-1998 C++.
Template classes have to be defined completely in the header.


There exists a convention to put generic (templates) implementations into a separate file as follows:


Vector.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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 */;

#include "Vector.tcc" 



Vector.tcc

1
2
3
4
5
6
7
8
9
10
template <class Object>
Vector<Object>::Vector ()
{
    moj = new Object[3];
}

template <class Object>
Vector<Object>::Vector (int e)
{
// ... 



1. Put your specification into a header as usual.
2. Put the implementation into an implementation file with the same name as your header file but suffixed with ".tcc".
3. Include your implementation file at the end of your specification file.
4. Be sure not to include the header file in your implementation file.

Now you may include your header as usual in any other file where needed.
> Be sure not to include the header file in your implementation file.
It wouldn't be a problem if you used header guards (that you should)
Last edited on
Topic archived. No new replies allowed.