Compile file template.h in C++ ?

I have some problem when compling with C++ in linux. Please help me.
Compile: g++34 -m32 -c allocate.C
But error when compile?
Source code:
1. allocate.h
#ifndef _ALLOCATE
#define _ALLOCATE

template <class T,class U> T *alloc1D(U nx);
template <class T,class U> complex<T> *alloc1Dcomp(U nx);
template <class T,class U> void delete1D(T *arr,U nx);

template <class T,class U> T **alloc2D(U nx,U ny);
template <class T,class U> complex<T> **alloc2Dcomp(U nx,U ny);
template <class T,class U> void delete2D(T arr,U nx,U ny);

template <class T,class U> T ***alloc3D(U nx,U ny,U nz);
template <class T,class U> complex<T> ***alloc3Dcomp(U nx,U ny, U nz);
template <class T,class U> void delete3D(T ***arr,U nx,U ny,U nz);

template <class T,class U> T *convertArray3to1(U nx,U ny,U nz, T ***arr3);
template <class T,class U> T ***convertArray1to3(U nx,U ny,U nz, T *arr);

#endif


2. allocate.C
#include<complex>
#include<iostream>
#include<stdlib.h>
#include "allocate.h"
using namespace std;


//-------------------- Array 1D
template <class T,class U>
T *alloc1D(U nx){
T *arr=new T[nx];
return arr;
}

template <class T,class U>
complex<T> *alloc1Dcomp(U nx){
complex<T> *arr=new complex<T>[nx];
return arr;
}

template <class T,class U>
void delete1D(T *arr,U nx){
delete []arr;
arr=NULL;
}

//-------------------- Array 2D
template <class T,class U>
T **alloc2D(U nx,U ny){
T **arr=new T*[nx];
for(int i=0;i<nx;i++)
arr[i]=new T[ny];
return arr;
}

template<class T,class U>
complex<T> **alloc2Dcomp(U nx,U ny){
complex<T> **arr=new complex<T>*[nx];
for(int i=0;i<nx;i++)
arr[i]=new complex<T>[ny];
return arr;
}

template <class T,class U>
void delete2D(T arr,U nx,U ny){
for(int i=0;i<nx;i++){
delete [] arr[i];
arr[i]=NULL;
}
delete [] arr;
arr=NULL;
}

//-------------------- Array 3D
template <class T,class U>
T ***alloc3D(U nx,U ny,U nz){
T ***arr=new T**[nx];
for(int i=0;i<nx;i++){
arr[i]=new T*[ny];
for(int j=0;j<ny;j++)
arr[i][j]=new T[nz];
}
return arr;
}

template <class T,class U>
complex<T> ***alloc3Dcomp(U nx,U ny, U nz){
complex<T> ***arr=new complex<T>**[nx];
for(int i=0;i<nx;i++){
arr[i]=new complex<T>*[ny];
for(int j=0;j<ny;j++)
arr[i][j]=new complex<T>[nz];
}
return arr;
}

template <class T,class U>
void delete3D(T ***arr,U nx,U ny,U nz){
for(int i=0;i<nx;i++){
for(int j=0;j<ny;j++){
delete[] arr[i][j];
arr[i][j]=NULL;
}
delete[] arr[i];
arr[i]=NULL;
}
delete[] arr;
arr=NULL;
}

//----------------------- Lib to Convert Array
template <class T,class U>
T *convertArray3to1(U nx,U ny,U nz, T ***arr3){
T *arr=alloc1D<T>(nx*ny*nz);
int i,j,k,tmp=0;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
for(k=0;k<nz;k++)
arr[tmp++]=arr3[i][j][k];
return arr;
}

template <class T,class U>
T ***convertArray1to3(U nx,U ny,U nz, T *arr){
T ***arr3=alloc3D<T>(nx,ny,nz);
int i,j,k,tmp=0;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
for(k=0;k<nz;k++)
arr3[i][j][k]=arr[tmp++];
return arr3;
}
And thie error is ...
Check this out:
http://cplusplus.com/doc/tutorial/templates/

At the bottom of the page, it says:

From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a "header file" with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.


So, if I understood right, templates have to be declared in header files also.
Error when i'm compling. Why's that?
Error:
-------------------------------------------------------------------
In file included from allocate.C:4:
allocate.h:5: error: expected constructor, destructor, or type conversion before '<' token
allocate.h:9: error: expected constructor, destructor, or type conversion before '<' token
allocate.h:13: error: expected constructor, destructor, or type conversion before '<' token

-------------------------------------------------------------------
You're referencing complex<T*> but haven't provided a definition.
I have a similar problem, but, for a program with multiple files, the code must be inthe same file with the header.
Rules for Templates:

The declaration AND the implementation must all be in the header files (.hpp).

Only the instantiation of a template object should be in a source file (.cpp).

Please re-read HenriK's post very carefully.
Yes, the declaration and the implementation also bothers me, because it would be nice to declare template function in a source file (.cpp) even though the reason makes a clear sense. Declaration in a source file would make the code more clear, but unless you´re a friggin´genius, you can´t do anything to this.
[code] "Please use code tags" [/code]

If you want to separate implementation from definition
1
2
3
4
5
6
#ifndef HEADER_H
#define HEADER_H
//header.h
//definitions
#include "header_implementation.hpp"
#endif 


Or (I think it is standard)
1
2
3
4
5
6
7
8
9
10
11
//header.cpp
#include "header.h"
//implementations

//explicit template instantiation (the types that you will use)
//classes
template class A<int>;
template class A<std::string>;

//functions
template void bar<int>(int);

Edit:
Here is an article that explains it better http://www.cplusplus.com/forum/articles/14272/

However template <class T,class U> T ***alloc3D(U nx,U ny,U nz); Why don't just create 1d arrays (maybe with std::vector) and encapsulate its behaviour in a class? I mean, the algorithms will likely just traverse it as an 1d array.
Also, you are asking for default constructor in the class
Last edited on
Topic archived. No new replies allowed.