delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/24/00:48:55

Message-ID: <397BC997.9C504DD9@puckbell.net>
From: Tasci Synx <nospam AT puckbell DOT net>
X-Mailer: Mozilla 4.73 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Enabling Static Data in Templates
Lines: 65
Date: Sun, 23 Jul 2000 21:44:07 -0700
NNTP-Posting-Host: 63.197.122.98
X-Complaints-To: abuse AT pacbell DOT net
X-Trace: news.pacbell.net 964413410 63.197.122.98 (Sun, 23 Jul 2000 21:36:50 PDT)
NNTP-Posting-Date: Sun, 23 Jul 2000 21:36:50 PDT
Organization: SBC Internet Services
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

This might be a problem with djgpp, so I wanted to ask here.

The Ansi C++ standard gives an example of a static data member in a
templated class:
---------------------------------------------------------
template<class T> class X {
	static T s;
};
template<class T> T X<T>::s = 0;
---------------------------------------------------------
However, upon putting such a thing in a header file, I received a linker
error.

test.o: In function `foo<int>::blech(void)':
test.cpp:15: undefined reference to `foo<int>::bar'
collect2: ld returned 1 exit status

Here is the test code I compiled:

---------------------------------------------------------
// in test.h
#include <iostream>
using std::cout;

template <class Type>
struct foo
{
	static Type bar;
	static void blech(void);
};

template <class Type> Type foo<Type>::bar = 33;

template <class Type> inline void foo<Type>::blech(void)
{
	cout << "Pass the bar:" << bar << endl;
}
---------------------------------------------------------
---------------------------------------------------------
// in test.cpp

#include "test.h"

void main(void)
{
	foo<int> test;
	test.blech();
}
---------------------------------------------------------

Is this a problem specific to djgpp?  Is there a switch I can use to
make sure my static templated data members are created properly?

Thank you.


Tasci

-- 

Stories 'n stuff:
http://velar.ctrl-c.liu.se/vcl/Authors/Starling/

To send me an email, just replace the @ and the .
     -----wassdamo at pacbell dot net

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019