I've been trying to create some custom system libraries with templating
and it surprised me when I starting to get linking problems. Generally I
create a .h file and a .cpp file and develop objects which can later be
developed into shared libraries. Years ago I did quite a bit of work
with C++ and libraries and I don't recall having this problem, but looking
back at the code, I see I was cramming a lot of class definitions directly
into the .h file. So I must have hit this snag before but I don't
remember it.
You'll have to excuse me if I sound a little vague on this problem I am
having, but it is very complicated and I'm slow at getting my head around
this issue. And it seems to affected somehow by name space.
I'm looking over the text by Vandevoorde and Josuttis, "C++ Templates The
complete guild, 12th printing in June of 2010 and copyrighted in 2003.
They seem to sum up the problem fairly well, although there explanation
is a bit cryptic to me. Section 6.1.1 and 6.1.2 on pages 63 after show
some sample code of a 3 file break out of C++ code they write:
A C++ compiler will most likely accept this program
without any problems, but the linker will probably
report an error, implying that there is no definition
for the function print_typeof()...
(adlib) for a common solution...
We include the defintions of a template in the header
file that declares that template.. For our example,
we can do this by adding
#include "myfirst.cpp"
at the end of myfirst.cpp in every do-C file that uses
the template. A third way, or course, is to do away
with myfirst.cpp and rewrite myfirst.hpp so that it
contains all template declarations and template
definitions.
Ugg. So I'm looking over old code that I wrote and it seems that
namespace influences the result of this, at least with gcc on GNU.
In this file here, I seem to be having no problem creating templated
objects in a .cpp file, but everything is being dumped the std namespace
http://www.nylxs.com/docs/workshops/linklist_templates/linklist.cpp.html
1 #include "linklist.h"
2 #include <iostream>
3
4 using namespace std;
5
6 template<class unk>
7 NODE<unk>::~NODE<unk>(){
8 //cout << "List Item Destroyed" << endl;
9
10 }
11
12
13
14
15 template<class unk>
16 void LIST<unk>::remove_front(){
17 if(_at_front == 0){
18 cout << "No List" << endl;
19 return;
20 }
21 NODE<unk> * tmp = _at_front;
22 down_size();
23 _at_front = tmp->next();
24 if(tmp->next() == NULL)
25 _at_end = 0; //need to clear _at_end as well once
you delete the last node
26
27
28 delete tmp;
29 return;
30 }
31
The code I'm working on currently is less fortunate. I created a
namespace for it
~~nodes.cpp
#include "nodes.h"
#include <iostream>
using namespace std;
template<class unk>
tree::NODE<unk>::NODE( unk states, NODE<unk> *cl, NODE<unk> *cr, NODE<unk>
*p ){
cout << "Im here" << endl;
}
template<class unk>
tree::NODE<unk>::~NODE<unk>(){
};
~~~~
|| g++ -Wall -ggdb -c nodes.cpp
nodes.cpp|5 col 1| error: ‘tree’ does not name a type
|| tree::NODE<unk>::NODE( unk states, NODE<unk> *cl, NODE<unk> *cr,
NODE<unk> *p ){
|| ^~~~
nodes.cpp|10 col 1| error: ‘tree’ does not name a type
|| tree::NODE<unk>::~NODE<unk>(){
|| ^~~~
make: *** [makefile|8| nodes.o] Error 1
before this, it was saying it couldn't find definitions of my templates
in this file. I did a circular thing and included, as per what it said
in the text,
#include node.cpp
in file node.h
/*
*
=====================================================================================
*
* Filename: nodes.h
*
* Description: description of phylogentic nodes as per
* Fitch and Sannkoff as describd by Felenstein
*
* Version: 1.0
* Created: Nov 4 21:15:49 EDT 2016
* Revision: none
* Compiler: gcc
*
* Author: Ruben Safir,
* Company: LIU Thesis
*
*
=====================================================================================
*/
#ifndef NODES_H
#define NODES_H
#include<iostream>
#include<string>
#include<vector>
#include "nodes.cpp"
namespace tree {
/*
==============================================================================
* Class NODE -
* Description - This is a node in the tree that must undergo parsimony
evaluation
*
================================================================================
*/
Now the namespace issue is cropping up.