Menu

Linker / template class problem

Anonymous
2002-02-28
2012-09-26
  • Anonymous

    Anonymous - 2002-02-28

    Have this error -
    z:\c++\prog\main.o(.text$__8progi+0x23):main.cpp: undefined reference to `sac<int>::sac(int)'

    Have two cpp files, main and sac
    Have one header for sac

    Compiles OK, but Linker gives above error

    I know what causes error, but don't know how to fix

    sac is a very simple template
    template <class Dtype> sac {
    public:
    sac( int size ) ;
    // rest of definition
    };

    main creates object {
    sac<int> list(100);
    }

    And when I compile I get above error

    However, if I put definition of sac::sac(int)
    within class, all is ok
    But if template <class Dtype> sac<Dtype>::sac(int)
    is in sac.cpp, I get the error from the linker

    This doesn't seem to happen with ordinary classes
    just templates

    I can fix this error by placing #include "sac.cpp"
    at start of main.cpp - but then the code for sac.cpp is being compiled with main.cpp code

    Any ideas on how to fix ?

     
    • Anonymous

      Anonymous - 2002-03-23

      why cant you just always include #include "sac.cpp" if that fixes it?

      (just an idea, i really have no idea what this program means or does)

       

Log in to post a comment.