473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem with template in extern .h and .cpp files

Hi,

i wonder about a problem with functions i cannot put out of my
main.cpp file into one *.h and *.cpp. When I run this little
testprogram:

#include <iostream>
#include <vector>
//#include"potential.h"
using namespace std;

template<class T>void test(T t);

template<class T>void test(T t)
{
cout << t << endl;
}

int main ()
{
cout << "Il sole splende!" << endl;
vector<vector<doubleA(2);
for (int i=0;i<A.size();i++)
A[i].resize(4,2);
test((double)5.);

return 0;
}

everything is well.
If i compile the following code everythings fine at first, too, but
running it produces a crash.
Code:

--- main.cpp ---

#include <iostream>
#include <vector>
#include"potential.h"
using namespace std;

int main ()
{
cout << "Il sole splende!" << endl;
vector<vector<doubleA(2);
for (int i=0;i<A.size();i++)
A[i].resize(4,2);
test((double)5.);

return 0;
}

--- potential.h ---

#include <iostream>
using namespace std;

template<class T>void test(T t);

--- potential.cpp ---

#include "potential.h"

template<class T>void test(T t)
{
cout << t << endl;
}

console:

myPC>g++ main.cpp potential.h potential.cpp
/usr/bin/ld: Undefined symbols:
void test<double>(double)
collect2: ld returned 1 exit status

I wonder about it, because if i don't use templates everything is fine
again:

--- potential.h ---

#include <iostream>
using namespace std;

void test(double t);

-- potential.cpp ---

#include "potential.h"

void test(double t)
{
cout << t << endl;
}

and the main.cpp like the second example.
What am I doing/thinking wrong?

Thanks for reading ;-) ,
Stefan
Nov 19 '07 #1
3 1976
Stefan Bollmann wrote:
i wonder about a problem with functions i cannot put out of my
main.cpp file into one *.h and *.cpp. When I run this little
testprogram:
[..]
Please read the FAQ, the section on templates. Then look over
the rest of the FAQ for link errors, and such. That should answer
your concernes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 19 '07 #2
On 11/19/2007 2:38 PM, Stefan Bollmann wrote:
Hi,

i wonder about a problem with functions i cannot put out of my
main.cpp file into one *.h and *.cpp. When I run this little
testprogram:

#include <iostream>
#include <vector>
//#include"potential.h"
using namespace std;

template<class T>void test(T t);

template<class T>void test(T t)
{
cout << t << endl;
}

int main ()
{
cout << "Il sole splende!" << endl;
vector<vector<doubleA(2);
for (int i=0;i<A.size();i++)
A[i].resize(4,2);
test((double)5.);

return 0;
}

everything is well.
If i compile the following code everythings fine at first, too, but
running it produces a crash.
Code:

--- main.cpp ---

#include <iostream>
#include <vector>
#include"potential.h"
using namespace std;

int main ()
{
cout << "Il sole splende!" << endl;
vector<vector<doubleA(2);
for (int i=0;i<A.size();i++)
A[i].resize(4,2);
test((double)5.);

return 0;
}

--- potential.h ---

#include <iostream>
using namespace std;

template<class T>void test(T t);

--- potential.cpp ---

#include "potential.h"

template<class T>void test(T t)
{
cout << t << endl;
}

console:

myPC>g++ main.cpp potential.h potential.cpp
/usr/bin/ld: Undefined symbols:
void test<double>(double)
collect2: ld returned 1 exit status

I wonder about it, because if i don't use templates everything is fine
again:

--- potential.h ---

#include <iostream>
using namespace std;

void test(double t);

-- potential.cpp ---

#include "potential.h"

void test(double t)
{
cout << t << endl;
}

and the main.cpp like the second example.
What am I doing/thinking wrong?

Thanks for reading ;-) ,
Stefan
Read this:
http://www.ensta.fr/~diam/c++/online...html#faq-35.12

S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Nov 19 '07 #3
Ok, thx 2 all. The faq showed me the problem and now I know why I've
got a problem. Sry for not looking there..

Stefan
Nov 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
1
by: arun | last post by:
Hello team, I know that extern keyword before template class or function delays the instantiation of that template declaration. However,, my question is does this utility only works for...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
5
by: Sunny | last post by:
Hi, I want to create a library out of a C++ sources. The implementation should be in object files and there will be a header file which the client will #include. I want to use templates to...
0
by: mqrk | last post by:
On Sep 12, 4:58 am, dizzy <di...@roedu.netwrote: I don't know that there is any way around this with g++ and ld. I find that surprising, but that may well be. Have you tried using explicit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.