namespace help

L

lallous

file: prog1.cpp
#include <stdio.h>
#include "temp1.h"

int main()
{
double d;
Templ1::func(d);
return 0;
}

file: temp1.h
namespace Templ1
{
template <class T> void func(T &);
}

file:temp1.cpp
namespace Templ1
{
template <class T> void func(T &i)
{
i = 0;
}
}

When I compile using VC, I get:
Linking...
prog1.obj : error LNK2001: unresolved external symbol "void __cdecl
Templ1::func(int &)" (?func@Templ1@@YAXAAH@Z)
Debug/prog1.exe : fatal error LNK1120: 1 unresolved externals

How can I still use templated version func() from different module (temp1.*)

Regards,
Elias
 
R

Russell Hanneken

lallous said:
file: temp1.h
namespace Templ1
{
template <class T> void func(T &);
}

file:temp1.cpp
namespace Templ1
{
template <class T> void func(T &i)
{
i = 0;
}
}

When I compile using VC, I get:
Linking...
prog1.obj : error LNK2001: unresolved external symbol "void __cdecl
Templ1::func(int &)" (?func@Templ1@@YAXAAH@Z)
Debug/prog1.exe : fatal error LNK1120: 1 unresolved externals

How can I still use templated version func() from different module
(temp1.*)

Check the FAQ:

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
 
L

lallous

Russell Hanneken said:
Check the FAQ:

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.1
2

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
Hello,
So I understand that I must say: #include "temp1.cpp" instead of ".h" (or
move implementation into header file) ?

What alternative do you suggest, even if I have to redesign my code...
I want to use templates and namespace...

Currently, I put in header file different version of func() as:
funcInt(int &r);
funcDouble(double &r);

then in cpp file I put:
void funcInt(int &i)
{
func(i);
}

void funcDouble(double &i)
{
func(i);
}


Regards,
Elias
 
R

Rolf Magnus

lallous said:
So I understand that I must say: #include "temp1.cpp" instead of ".h"
(or move implementation into header file) ?

or #include the .cpp file at the end of the header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top