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
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
-------------------------------------------------------------------
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.
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