Template problem

Andrew Borthwick borthwic@johnson.cs.nyu.edu
Sun Jan 11 13:22:00 GMT 1998


Hi,

    I am trying to define a member function of a template in a .C file after
declaring it in a .H file and am striking out.  Could somebody help me?  I feel
like I must be missing something obvious.

I am using egcs 1.0.1 on a sparc-sun-solaris2.5.1. and the loader says is the
following:

ld: Software Generation Utilities (SGU) SunOS/ELF (LK-2.0 (S/I) - versioning)

    My code is as follows:
-----------------------
// Foo.H
#ifndef _FOO_H_
#define _FOO_H_

#include <string>
template <int myParm>
class Foo {
public:
  Foo() {
    data = "start";
    number = 42;
  }
  double Stupid1() {
    return double(number + 1);
  }
  double Stupid2();
private:
  string data;
  int number;
};

#endif
------------------
// Foo.C:
#include <string>

#include "Foo.H"

template <int myParm>
double Foo<myParm>::Stupid2() {
  return double(number);
}
-------------------
// main.C
#include "Foo.H"

int main() {
  Foo<1> data1;
  Foo<2> data2;
  data1.Stupid1();
  data2.Stupid2();
}
-------------------
% make
g++    -c main.C -o main.o
g++    -c Foo.C -o Foo.o
g++ -o main main.o Foo.o
Undefined                       first referenced
 symbol                             in file
Foo<2>::Stupid2(void)               main.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status
make: *** [Foo] Error 1




More information about the Gcc mailing list