C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to fix Undefined Reference compile error?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
GRalphE
Guest





PostPosted: Fri Jun 27, 2003 2:03 pm    Post subject: How to fix Undefined Reference compile error? Reply with quote



The code can be found at:

http://www.anora.org/grellsworth/muvie/index.html

Just click on the "Download MUVIE Development Snapshot" link to get
the Gzipped Tarball.

The problem is:

In Muvie.c, in main() I have:

#include "Data.h"
#include "Muvie.h"

int main(int argc, char **argv)
{
stringstream Stream;
Logger = new Log();

SetValue(32, Stream);

delete Logger;

pthread_exit(NULL);
return 0;
}

In Data.c, I have a function:

template <class T>
void SetValue(const T &value, stringstream &ss)
{
ss.clear();
ss.str("");
ss << value;
}

I have the function prototype (declaration) in Data.h which is
#included in Muvie.c

I get the following compile error:

../objs/Muvie.o: In function `main':
/home/muvie/muvie/src/Muvie.c:30: undefined reference to `void
SetValue std::char_traits&)'
collect2: ld returned 1 exit status
make: *** [Muvie] Error 1

I don't see what the problem is, since Data.h is #included in Muvie.c
where main() is.

How can I fix this?

Sincerely,

GRalphE

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Mang
Guest





PostPosted: Fri Jun 27, 2003 8:16 pm    Post subject: Re: How to fix Undefined Reference compile error? Reply with quote





GRalphE schrieb:

Quote:
#include "Data.h"
#include "Muvie.h"

int main(int argc, char **argv)
{
stringstream Stream;
Logger = new Log();

SetValue(32, Stream);

delete Logger;

pthread_exit(NULL);
return 0;
}

In Data.c, I have a function:

template void SetValue(const T &value, stringstream &ss)
{
ss.clear();
ss.str("");
ss << value;
}

Move this function definition out of the *.c - file into the header
file.

Remember, templates are special - the compiler has to see their
definitions from outside the current file, in order to instantiate it.
At least under the inclusion-model - which I strongly guess you are
using.

regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Sun Jun 29, 2003 11:37 am    Post subject: Re: How to fix Undefined Reference compile error? Reply with quote





GRalphE schrieb:

Quote:
OK, I should have known that...
But what if the template was inside a class (like I intended it to be
in the beginning)?

=========================

In MyClass.h :

class MyClass {
public:
template void SetValue(const T &value);
private:
stringstream Value;
};

========================

In My Class.c :

template void MyClass::SetValue(const T &value)
{
Value.clear();
Value.str("");
Value << value;

return;
}

=======================

Would I still have to put the template definition within the class in
this
case, also?

No, not necessarily inside the class definition (inline function - you
can do that, but probably only if it's a very small function), but you
have to place it into the header file.

Imagine some file, say "main.c", calls your template function:

MyClass myClass;
myClass.SetValue(23);

Now suppose the definition of your template function is in a "MyClass.c".
How should the compiler be able to instantiate the function with an int
as parameter?
Short answer: It can't.

However, if you place the definition of your template function into the
header file, there is no problem. Why? Because in order to use class
MyClass, "main.c" MUST include the header-file "MyClass.h". Otherwise
simple statements such as

MyClass myClass;

won't compile either. And as a result of #including the header file, the
compiler is able to see the definition for your template functions and is
able to instantiate them.

Conclusion:
When using the "inclusion model" of templates, place both the declaration
AND the definition of template functions into the header file. Other
files can then #include this file and instantiate the templates.
The only exception is, of course, if a template function / class is NOT
INTENDED to be used by other files - then you can place it into the *.c -
file.

regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.