[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

C++ templates



Hej,

Kan det passe at GCC's g++ ikke vil acceptere at en template-klasses
medlemsfunktioner er erklæret uden for den pågældende template-klasses
definition?

Eksempel: Nedenstående filer givende følgende fejl under linkning:

g++ -Wall -ansi -pedantic   -c -o main.o main.cpp
g++ -Wall -ansi -pedantic   -c -o foo.o foo.cpp
g++ main.o foo.o -o test
main.o(.text+0x1f): In function `main':
: undefined reference to `foo<int>::setBar(int)'
main.o(.text+0x2a): In function `main':
: undefined reference to `foo<int>::getBar()'
collect2: ld returned 1 exit status

Hvis jeg flytter foo's medlemsfunktioner fra foo.cpp ind i foo.h
fungerer det fint. Kan det passe at dette er den korrekte opførsel?


// foo.h
#ifndef __TEMPLATE_TEST_H__
#define __TEMPLATE_TEST_H__

template<class T>
class foo
{
public:
  void setBar (T bar);
  T    getBar (void);
private:
  T myBar;
};

#endif  /*  __TEMPLATE_TEST_H__  */
// EOF foo.h


// foo.cpp
#include "foo.h"

template<class T>
void
foo<T>::setBar (T bar)
{
  myBar = bar;
}

template<class T>
T
foo<T>::getBar (void)
{
  return myBar;
}
// EOF foo.cpp


// main.cpp
#include <iostream>
#include "foo.h"
using namespace std;

int
main (int   argc,
      char *argv[])
{
  foo<int> baz;

  baz.setBar (42);
  cout << baz.getBar () << endl;
}
// EOF main.cpp

Mvh
Brix
-- 
Henrik Brix Andersen <sslug@sslug>



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *