f



How to fix Undefined Reference compile error?

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<int>(int const&, std::basic_stringstream<char,
std::char_traits<char>, std::allocator<char> >&)'
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! ]
0
vaghn
6/27/2003 2:03:35 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

2 Replies
2036 Views

Similar Articles

[PageSpeed] 13


GRalphE schrieb:

> #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;
> }

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! ]
0
Thomas
6/27/2003 8:16:04 PM

GRalphE schrieb:

 > 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 <class T>
 >           void SetValue(const T &value);
 >     private:
 >          stringstream Value;
 > };
 >
 > ========================
 >
 > In My Class.c :
 >
 > template <class T>
 > 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! ]
0
Thomas
6/29/2003 11:37:28 AM
Reply:

Similar Artilces:

8.14.8 compile error: smdb2.c:(.text+0x6e0): undefined reference to `db_version'
Hi, I try to compile sendmail on a 3.1.10-1.16 x86_64 GNU/Linux openSuSE system. All prior versions compile well (tested: 8.14.5 8.14.7), but 8.14.8 seems to have problems with the vacation program. This is my site.config.m4: define(`confMANROOT', `/usr/share/man/man') define(`STATUS_FILE', `/etc/mail/statistics') APPENDDEF(`confENVDEF',`-DSTARTTLS') APPENDDEF(`confLIBS', `-lssl -lcrypto') APPENDDEF(`confENVDEF', `-UNIS') APPENDDEF(`confENVDEF', `-DSASL=2') APPENDDEF(`confENVDEF', `-DMILTER') APPENDDEF(`confLIBDIRS...

error by compiling an c-sfunction: simulink.c errors
hi! i looked around in this forum but it look's like i'm the only one with this problem. ok, i would like to mex a c-code s-function but it doesn't work. i used the "sfuntmpl_basic.c" skeleton for my programm and there is no error anymore (i think so) :-). the error message is the following: Error csfunctionSystem.c: C:\Programme\MATLAB7\simulink\include\simulink.c: 198 syntax error; found `(' expecting `)' Error csfunctionSystem.c: C:\Programme\MATLAB7\simulink\include\simulink.c: 198 skipping `(' `(' Error csfunctionSystem.c: C:\Programme\MATLAB7\...

Compile error undefined reference to ifx_checkAPI
This bug is fixed according to http://bugs.php.net/bug.php?id=33469 As I am using the rpm version of Suse with all the patches Suse provides, I would like to exchange only those files of the sources, that are related to that bug and would not use a possibly instable snapshot. So can someone tell me, which files are related to that bug? Thanks Norbert Wegener ...

CodeWright Compiler Error Parser Support for GreenHills C/C++ compiler
Hello All I have Version 6.5 of CodeWright (CW) and wish to compile inside of CW using the Greenhills compiler (ie. outside of the GreenHills Multi shell) and get CW to interpret this compilers error codes allowing CW to jump to next error ect. I have done with the Microtec Compiler before. I guess my copy of the errparse.dll is out of date and cannot find a later version of it on the Premia web site. Can anybody provide any pointers, or a later version of this dll that support the GHS compiler. Thanks in advance for any help Robert ...

compile error in C++, not C
The following program compiles just fine in C, but not in C++. Can anyone explain why? I have a chunk of code that defines stuff like this in headers (without the extern) that I can not easily change. The C compiler recognizes the first foo and second foo as the same. The C++ compiler not so much. Is there a way to get this to compile in C++ without changing all the headers? int foo; int foo=1; int main(void) { return foo; } On Aug 23, 2:44 am, Tim H <thoc...@gmail.com> wrote: > The following program compiles just fine in C, but not in C++. Can > anyone explain why...

undefined reference(compile error), plz help
Dear advance c++ programers: I tried to type in/test your code in chapter 2 of c++ primer (3rd ed) page 45, 46, 49 IntArray.h IntArrayRC.h (and add IntArray.cpp myself) /* they are all simple */ the following is my code copied from your book ---------------------------------------------- #include <iostream> #include "IntArray.h" #include "IntArrayRC.h" using namespace std; void swap(IntArray&, int, int); int main() { int array[ 4 ] = { 0, 1, 2, 3 }; IntArray ia1( array, 4 ); IntArrayRC ia2( array, 4 ); // error: off-by-one: should be size - 1 /...

undefined reference error compiling demo program
I get compile error: undefined reference to snd_pcm_format_width. I included the library directory with -I flag on gcc. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sched.h> #include <errno.h> #include <getopt.h> #include "../include/asoundlib.h" #include <sys/time.h> #include <math.h> vfunc@talktalk.net wrote: > I get compile error: > undefined reference to snd_pcm_format_width. > I included the library directory with -I flag on gcc. > > #include <stdio.h> > #include <stdlib.h> ...

Undefined reference error reading Accelerated C++
Hi all I am a newbie learning c++ by reading Accelerated C++ Practical Programming by Example. I have some experience with C and some oo programming with python. I am following the examples from the book and try to reproduce the same results. I am trying to learn from the chapter 4 of the book about splitting the functions/headers into different files. When I tried to compile the code as the book said, $ g++ -o run final.cpp I got following errors /tmp/ccsmGutF.o: In function `main': final.cpp:(.text+0xd7): undefined reference to `read (std::basic_istream<char, s...

compilation error: undefined reference (g++ on Linux)
Hi, I am getting undefined reference error for the code below: list.h: ------------- #ifndef _LIST_H #define _LIST_H template <class T> struct ListNode { T data; ListNode<T> *prev,*next; ListNode(T d=0):data(d) { prev = 0; next = 0; } ~ListNode() { } }; template <class T> class List { private: ListNode<T> *head, *tail; unsigned int size; public: List(); ~List(); bool insert_head(ListNode<T>* node); bool insert_tail(ListNode<T>* node); bool insert_next(ListNode<T>* node,ListNode<T>* next); bool insert_prev(ListNode<T&g...

program that compiles in C compiler but not in C++ compiler
Hi, I need a small program that compiles in C compiler but not in C++ compiler. Thx in advans, Karthik Balaguru KBG <karthik.balaguru@lntinfotech.com> wrote: > I need a small program that compiles in C compiler but not in C++ > compiler. No problem, just send $10 to paypal@zevv.nl and I'll do your homework for you. -- :wq ^X^Cy^K^X^C^C^C^C KBG said: > Hi, > > I need a small program that compiles in C compiler but not in C++ > compiler. Can you think of any syntactic differences between C and C++? For example, what about keywords? They are very, very sen...

Mex Compilation error : undefined reference to `engPutVariable'
Hi This might be a very simple question but i am not able to solve it of my own. I am trying to create a mex file which would call a .m file to perform some computations. I need to pass some variables of the type double from mex to .m file and i am trying to construct a mxArray and use the function "mexCallMatlab". Here is what i am trying to do : double D[12] = {1,2,3,4,5,6,7,8,9,10,11,12}; mxArray *B, *Bat ; B = mxCreateDoubleMatrix(3,4,mxREAL); double *data = mxGetPr(B); for(int i = 0; i<= 11; i++){ data[i] = D[i]; } engPutVariab...

kernel compiling error: undefined reference to critical exit
Hello folks I tried to compile a kernel for FreeBSD 5.3. The Kernel config file is called K53DEC04 Config and make depend worked well, but when I try to run "make" it puts some error msg`s on the screen. clock.o(.text+0xc3): In function `clkintr': : undefined reference to `critical_exit' clock.o(.text+0x13d): In function `clkintr': : undefined reference to `critical_enter' clock.o(.text+0x1ec): In function `clkintr': : undefined reference to `critical_exit' clock.o(.text+0x22c): In function `clkintr': : undefined reference to `critical_e...

Are sun studio C/C++ compilers and Forte C/C++ compilers same ??
Is there any difference between sun studio compilers and forte compilers??? or the names have been changed??? In article <1145338052.955429.256610@v46g2000cwv.googlegroups.com>, "ameya_agnihotri" <ameyaagnihotri22@gmail.com> writes: > Is there any difference between sun studio compilers and forte > compilers??? > or the names have been changed??? One of many name changes in the product's history (including amongst others, Java somethingortheother, Workshop, Proworks/Teamworks, and probably more I've forgotten). Of course, the versions and features cha...

undefined reference to `sqlcxt' (Pro C compilation problem)
Hi Greetings to all I am new to ProC. I am compiling a very simple program written in ProC. I can compile the program using proc filename.pc and it generates the corresponding c code :) , but when i try to compile the C program using cc -O3 -DPRECOMP -I. -I/opt/oracle/products/9.2.0/rdbms/public -I/opt/oracle/products/9.2.0/precomp/public -I/opt/oracle/products/9.2.0/rdbms/demo -I/opt/oracle/products/9.2.0/plsql/public -I/opt/oracle/products/9.2.0/network/public -DLINUX -D_GNU_SOURCE -D_GNU_SOURCE -DSLTS_ENABLE -DSLMXMX_ENABLE -D_REENTRANT -DNS_THREADS -L /opt/oracle/pro...

Web resources about - How to fix Undefined Reference compile error? - comp.lang.c++.moderated

Undefined - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Mike Duffy trial: Senate's ex-law clerk says senator residence rules undefined
A former Senate law clerk testifying at the Mike Duffy trial in Ottawa admitted that the rules governing a senator's residency do not include ...

Washington's Olympic bid moves forward with extent of Maryland role undefined
... offered his preliminary backTing, but the extent of Maryland's role &#8211; and potential financial commitment &#8211; remained largely undefined. ...

Undefined discussion subject.
Undefined discussion subject. Read the full story here

undefined - Daily News & Analysis
Toggle navigation analysis DNA Edit Blogs Standpoint news India World Mumbai Delhi Bangalore Pune Ahmedabad sport money sci/tech dna pix entertainment ...

More Thoughts on Undefined Behavior
Programmers are still trying to resolve the tension between safety and performance.

Mexico's Energy Battlegrounds Come Into Focus, But Rules Of Engagement Remain Undefined
By Dwight Dyer International investors have been abuzz about the prospect of reforms to Mexico’s energy industry ever since that country’s Congress ...

FOSS Patents: undefined
Florian Mueller is an award-winning intellectual property activist-turned-analyst with 25 years of software industry expertise spanning across ...

Microsoft’s Xbox guru Don Mattrick leaves for key, but undefined role at Zynga
With the Xbox One coming to stores sometime this holiday season, it looks like Microsoft will have to find a new leader to oversee the launch. ...

Fix for undefined error when all filtering funcs are disabled · brave/browser-laptop@e6ea975 · GitHub ...
browser-laptop - Brave browser for Desktop and Laptop computers running Windows, OSX, and Linux

Resources last updated: 2/6/2016 2:35:18 AM