Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template Class: Problem with Header file

Template Class: Problem with Header file

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresdebugging
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gvanto
    wrote on last edited by
    #1

    I'm having a problem with getting a template class's code split into a header and source file. The following three files below simply refuse to compile, error (using Eclipse Ganymede on Kubuntu Hardy Heron) below. W.r.t. the 3 files below, when I include the GenArray.cpp 's code INTO the GenArray.h file or include both GenArray.h and .cpp 's contents into the TestDemo, it works just fine! Any help on how to use this header file correctly would be greatly appreciated ! gvanto [CODE] Compiler output: make all Building file: ../src/GenArray.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/GenArray.d" -MT"src/GenArray.d" -o"src/GenArray.o" "../src/GenArray.cpp" Finished building: ../src/GenArray.cpp Building file: ../src/TestDemo.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TestDemo.d" -MT"src/TestDemo.d" -o"src/TestDemo.o" "../src/TestDemo.cpp" Finished building: ../src/TestDemo.cpp Building target: STLTest Invoking: GCC C++ Linker g++ -o"STLTest" ./src/GenArray.o ./src/TemplateClassDemo.o ./src/TemplateFunctionDemo.o ./src/TestDemo.o ./src/VirtualClassDemo.o ./src/rectangle.o ./src/TestDemo.o: In function `main': /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:21: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:22: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:25: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:26: undefined reference to `GenArray::operator[](int)' collect2: ld returned 1 exit status [/CODE] [CODE] /* * TestDemo.cpp * * Created on: 1/02/2009 * Author: pacific */ #include #include #include "GenArray.h" using namespace std; int main() { GenArray intob; //int array of size 10 GenArray doubleob; //double array of size 15 int i; cout << "Integer array: "; for(i=0; i<10; i++) intob[i] = i; for(i=0; i<10; i++) cout << intob[i] << " "; cout << '\n'; cout << "Double array: "; for(i=0; i<15; i++) doubleob[i] = (double) i/3; for(i=0; i<15; i++) cout << doubleob[i] << " "; return 0; } [/CODE] [CODE] /* * GenArray.h * * Created on: 1/02/2009 * Author: pacific */ //A Generic Safe-Array #include #include

    _ 1 Reply Last reply
    0
    • G gvanto

      I'm having a problem with getting a template class's code split into a header and source file. The following three files below simply refuse to compile, error (using Eclipse Ganymede on Kubuntu Hardy Heron) below. W.r.t. the 3 files below, when I include the GenArray.cpp 's code INTO the GenArray.h file or include both GenArray.h and .cpp 's contents into the TestDemo, it works just fine! Any help on how to use this header file correctly would be greatly appreciated ! gvanto [CODE] Compiler output: make all Building file: ../src/GenArray.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/GenArray.d" -MT"src/GenArray.d" -o"src/GenArray.o" "../src/GenArray.cpp" Finished building: ../src/GenArray.cpp Building file: ../src/TestDemo.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TestDemo.d" -MT"src/TestDemo.d" -o"src/TestDemo.o" "../src/TestDemo.cpp" Finished building: ../src/TestDemo.cpp Building target: STLTest Invoking: GCC C++ Linker g++ -o"STLTest" ./src/GenArray.o ./src/TemplateClassDemo.o ./src/TemplateFunctionDemo.o ./src/TestDemo.o ./src/VirtualClassDemo.o ./src/rectangle.o ./src/TestDemo.o: In function `main': /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:21: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:22: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:25: undefined reference to `GenArray::operator[](int)' /home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:26: undefined reference to `GenArray::operator[](int)' collect2: ld returned 1 exit status [/CODE] [CODE] /* * TestDemo.cpp * * Created on: 1/02/2009 * Author: pacific */ #include #include #include "GenArray.h" using namespace std; int main() { GenArray intob; //int array of size 10 GenArray doubleob; //double array of size 15 int i; cout << "Integer array: "; for(i=0; i<10; i++) intob[i] = i; for(i=0; i<10; i++) cout << intob[i] << " "; cout << '\n'; cout << "Double array: "; for(i=0; i<15; i++) doubleob[i] = (double) i/3; for(i=0; i<15; i++) cout << doubleob[i] << " "; return 0; } [/CODE] [CODE] /* * GenArray.h * * Created on: 1/02/2009 * Author: pacific */ //A Generic Safe-Array #include #include

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      When template classes are instantiated, a new class is created at compile time. For example, GenArray<int, 10> intob; will create a new class. Internally this class will have some mangled name (maybe .?AV?$GenArray@H$09@@) And GenArray<double, 15> doubleob; will create another class (maybe .?AV?$GenArray@N$0P@@@) So now you have 2 different classes. In order to create a new class, the compiler needs the full information about the template class. Since these classes are created when compiling the main function in the file TestDemo.cpp, the whole template information is necessary in the TestDemo.cpp file. So you either put the whole template class in the GenArray.h file or include both GenArray.h and GenArray.cpp in TestDemo.cpp. You will notice that template libraries like STL, ATL etc. have only .h files with all template class information in the header file iteself.

      «_Superman_»

      G 1 Reply Last reply
      0
      • _ _Superman_

        When template classes are instantiated, a new class is created at compile time. For example, GenArray<int, 10> intob; will create a new class. Internally this class will have some mangled name (maybe .?AV?$GenArray@H$09@@) And GenArray<double, 15> doubleob; will create another class (maybe .?AV?$GenArray@N$0P@@@) So now you have 2 different classes. In order to create a new class, the compiler needs the full information about the template class. Since these classes are created when compiling the main function in the file TestDemo.cpp, the whole template information is necessary in the TestDemo.cpp file. So you either put the whole template class in the GenArray.h file or include both GenArray.h and GenArray.cpp in TestDemo.cpp. You will notice that template libraries like STL, ATL etc. have only .h files with all template class information in the header file iteself.

        «_Superman_»

        G Offline
        G Offline
        gvanto
        wrote on last edited by
        #3

        Hi Superman, Wow thanks a million for a great response! This makes sense ... I guess I've just been taught never to use executable code in a header file (purely for prototype purposes) but I guess template classes are the exception eh?! Thanks again! Gvanto

        Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups