Click here to Skip to main content
15,907,183 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MSDN volatile sample Pin
George_George29-Dec-07 19:51
George_George29-Dec-07 19:51 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 0:22
peterchen30-Dec-07 0:22 
GeneralRe: MSDN volatile sample Pin
George_George30-Dec-07 0:39
George_George30-Dec-07 0:39 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 2:19
peterchen30-Dec-07 2:19 
GeneralRe: MSDN volatile sample Pin
George_George30-Dec-07 2:48
George_George30-Dec-07 2:48 
GeneralRe: MSDN volatile sample Pin
peterchen30-Dec-07 3:47
peterchen30-Dec-07 3:47 
GeneralRe: MSDN volatile sample Pin
George_George31-Dec-07 3:48
George_George31-Dec-07 3:48 
QuestionI define a template function in header, code in cpp. Include header. Undefined reference?? Pin
ArmchairAthlete27-Dec-07 21:47
ArmchairAthlete27-Dec-07 21:47 
I've got a main cpp file that is including a header file (MyAlgorithms.h) where I'm playing around with doing algorithms the STL way with templates and iterators.

If I put the actual code for my template algorithm in the header file everything compiles and works fine. If I define it in my header file, yet put the code in its own cpp file (MyAlgorithms.cpp) I get a compile error.

I can also put the code for the template function in the main cpp file and then it compiles/runs fine.

What gives?

I can even put the code for a non-template normal old function in the MyAlgorithms.cpp file and define in the header and it's fine.


undefined reference to `int ajo::roffeltemplate<int>(int)' CppPlayground.cpp


In main:


<br />
#include <iostream><br />
#include <string><br />
#include <vector><br />
#include <algorithm><br />
#include <br />
<br />
#include "Util.h"<br />
#include "PracticalSocket.h"<br />
#include "Protocol.h"<br />
#include "MyAlgorithms.h"<br />
<br />
...<br />
<br />
<br />
int blah = ajo::roffeltemplate(1); //compile error here</algorithm></vector></string></iostream>


MyAlgorithms.h

#ifndef MYALGORITHMS_<br />
#define MYALGORITHMS_<br />
<br />
#include <string><br />
#include <vector><br />
#include <algorithm><br />
<br />
namespace ajo<br />
{<br />
<br />
	#define ROFFEL 1<br />
<br />
	void printroffel();<br />
	<br />
	template <typename t=""><br />
	T roffeltemplate(T val);<br />
<br />
	template <typename iter,="" typename="" t=""><br />
	T sumsquares(Iter beg, Iter end, T init);<br />
	<br />
<br />
}<br />
<br />
<br />
<br />
#endif /*MYALGORITHMS_*/<br />
</typename></typename></algorithm></vector></string>


MyAlgorithms.cpp

<br />
#include "MyAlgorithms.h"<br />
<br />
#include <iostream><br />
#include <string><br />
<br />
using namespace ajo;<br />
<br />
template <typename t=""><br />
T roffeltemplate(T val)<br />
{<br />
	return val;<br />
}<br />
<br />
template <typename iter,="" typename="" t=""><br />
T sumsquares(Iter beg, Iter end, T init) <br />
{<br />
	T result = init;<br />
	for(;beg != end; ++beg) <br />
	{<br />
		result += (*beg) * (*beg);<br />
	}<br />
	return result;<br />
}<br />
<br />
void ajo::printroffel() <br />
{<br />
	printf("roffel");<br />
}<br />
<br />
<br />
</typename></typename></string></iostream>


I don't think it's a make issue, although I am experimenting with Eclipse's auto-make. I could try writing the make myself like I usually do. I can force a compiler error in the MyAlgorithms files so I know they're being compiled.
AnswerRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
ArmchairAthlete27-Dec-07 21:48
ArmchairAthlete27-Dec-07 21:48 
GeneralRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
CPallini28-Dec-07 1:03
mveCPallini28-Dec-07 1:03 
QuestionRe: I define a template function in header, code in cpp. Include header. Undefined reference?? Pin
David Crow28-Dec-07 2:40
David Crow28-Dec-07 2:40 
QuestionI want to write C program about CPU Scheduling simmulator Pin
Smith1927-Dec-07 21:40
Smith1927-Dec-07 21:40 
GeneralRe: I want to write C program about CPU Scheduling simmulator Pin
Garth J Lancaster27-Dec-07 23:32
professionalGarth J Lancaster27-Dec-07 23:32 
GeneralTrayIcon RightClick - Enable and Disable Pin
Paulraj G27-Dec-07 18:24
Paulraj G27-Dec-07 18:24 
GeneralRe: TrayIcon RightClick - Enable and Disable Pin
Nishad S27-Dec-07 19:52
Nishad S27-Dec-07 19:52 
GeneralProblem in writing template function inside class Pin
Naveen27-Dec-07 18:21
Naveen27-Dec-07 18:21 
GeneralRe: Problem in writing template function inside class [modified] Pin
Maxwell Chen27-Dec-07 18:47
Maxwell Chen27-Dec-07 18:47 
GeneralRe: Problem in writing template function inside class Pin
Naveen27-Dec-07 19:16
Naveen27-Dec-07 19:16 
GeneralRe: Problem in writing template function inside class Pin
Sarath C27-Dec-07 20:11
Sarath C27-Dec-07 20:11 
GeneralRe: Problem in writing template function inside class Pin
Maxwell Chen27-Dec-07 22:34
Maxwell Chen27-Dec-07 22:34 
GeneralRe: Problem in writing template function inside class Pin
Mahesh Kulkarni27-Dec-07 22:29
Mahesh Kulkarni27-Dec-07 22:29 
GeneralRe: Problem in writing template function inside class Pin
Naveen27-Dec-07 22:34
Naveen27-Dec-07 22:34 
GeneralRe: Problem in writing template function inside class Pin
Maxwell Chen27-Dec-07 22:39
Maxwell Chen27-Dec-07 22:39 
GeneralRe: Problem in writing template function inside class Pin
Mahesh Kulkarni27-Dec-07 22:58
Mahesh Kulkarni27-Dec-07 22:58 
GeneralRe: Problem in writing template function inside class Pin
Maxwell Chen27-Dec-07 23:05
Maxwell Chen27-Dec-07 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.