S
Stefan Bollmann
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<double> >A(2);
for (int i=0;i<A.size();i++)
A.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<double> >A(2);
for (int i=0;i<A.size();i++)
A.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
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<double> >A(2);
for (int i=0;i<A.size();i++)
A.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<double> >A(2);
for (int i=0;i<A.size();i++)
A.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