Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Templates

Author
Message
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 26th Sep 2010 23:50 Edited at: 26th Sep 2010 23:52
So this more of just a general programming question but I am making a simple calculator using templates and I got it working but this is how I have it set up:



Now I have it so it is all in one file called Template.h now when I tried to separate the files like a Template.h and a Template.cpp I was getting some linker errors. This is what the files looked like:

.h:



.cpp



What do I need to do to be able to seperate them into .h and .cpp files?
Abraxas77
13
Years of Service
User Offline
Joined: 26th Aug 2010
Location:
Posted: 27th Sep 2010 01:40
Yeah, that is the thing with templates. A template class cannot be defined in more than one file.

Quote: "source: cplusplus.com tutorial
Templates and multiple-file projects
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."
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 27th Sep 2010 03:51
Ahhh very helpful thanks.

Login to post a reply

Server time is: 2024-07-02 08:37:16
Your offset time is: 2024-07-02 08:37:16