ld returned 1 exit status Error

P

pai

Hi All,

I am getting this error when I compile. Can any one tell wht is
the mistake here.

I am using 4 files

vector.cpp , vector.h , main.cpp Makefile

MakeFile
-------------
all:main.o

clean:
rm main.o vector.o

main.o: main.cpp vector.h vector.o
g++ -g -o main.o vector.o main.cpp

vector.o: vector.cpp vector.h
g++ -g -c -o vector.o vector.cpp

---------------
Vector.h
--------------

#ifndef VECTOR_H_
#define VECTOR_H_

#include <iostream>
#include <stdlib.h>

using namespace std;

namespace ptl
{
template <class T>
class vec
{
private:
T* ptr;
T* allocate(int);

public:
//structor
vec():ptr(NULL){
cout << "No arg constructor" << endl;
};

vec(int val):ptr(NULL){
cout << "one arg constructor" << endl;
ptr = allocate(sizeof(int));
*ptr = val;
};

~vec(){};

};

}//namespace ptl

#endif /*VECTOR_H_*/

---------------
vector.cpp
--------------
#include "vector.h"

template <class T>
T* ptl::vec<T>::allocate(int size)
{
T *tmp = (T*)malloc(size);
cout << "allocate fn" << endl;
return tmp;

}

--------------
main.cpp
-------------
#include <iostream>
#include "vector.h"

using namespace std;
using namespace ptl;

int main()
{
cout << "Helo" << endl;
vec<int> v1(10);

return 0;

}
-------------
Error
----------------------
C:\Documents and Settings\guruduttr\workspace\vector>make
g++ -g -c -o vector.o vector.cpp
g++ -g -o main.o vector.o main.cpp
/cygdrive/c/DOCUME~1/GURUDU~1/LOCALS~1/Temp/ccRiugQz.o: In function
`_ZN3ptl3vec
IiED1Ev':
/cygdrive/c/Documents and Settings/guruduttr/workspace/vector/
vector.h:
(.text$_Z
N3ptl3vecIiEC1Ei[ptl::vec<int>::vec(int)]+0x46): undefined reference
to `ptl::ve
c<int>::allocate(int)'
collect2: ld returned 1 exit status
make: *** [main.o] Error 1

---------------

But when I put the code in vector.cpp to vector.h its gets compiled.
Wht am I missing here.
Plz suggest


Thanks
Pai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top