Thread: Template class

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Template class

    I had my template class with two-file organization
    Code:
    // MyClass.h
    #ifndef MY_CLASS_H
    #define MY_CLASS_H
    
    template <class T>
    class MyClass {
    ...
    };
    
    #include "MyClass.cpp"
    #endif
    
    //MyClass.cpp
    #include "Myclass.h"
    ...
    I included this class in my main program (#include "Myclass.h") and everything worked untill at some point after I added some stuff in my main program and add another class it gives me error "template function has already been defined" for every single member function in the template class. I could get away from getting that error by putting evertyhing in one file "MyClass.h" but I kinda like the two-file organization above.
    So, what's wrong
    Note that everything runs fine with two files when I compile using g++ but not .NET
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    Code:
    //The template class 
    #ifndef DICT_H
    #define DICT_H
    
    class Dictionary
    {
    ...
    ... };
    
    #endif
    //The .cpp file needs
    #include "dict_h.h"
    Code:
    //Main needs to include both
    #include "dict_h.h"
    #include "diction.cpp"
    
    void main ()
    {
    	Dictionary x;
    	x.readDict ();
    	return;
    }
    Hope this helps to clear things up for you
    Sophie
    simple is always an understatement.....

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    NEVER #include a .c or .cpp.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include "MyClass.cpp"
    >#include "diction.cpp"
    There's no need to include a .cpp file. When the object files are linked together everything will be fine. You do need to include the header files so that everything will compile before linking though.

    >void main ()
    I'm surprised you have so many posts and still haven't stopped using void main. Main returns an int, always. Void doesn't even save you keystrokes anymore by allowing you to omit the return statement. Standard C++ returns 0 by default if main returns int and the return statement is omitted. So using void is one character less efficient than int. And there's this little matter about undefined behavior, but it pales in comparison to the one character difference in source code size.
    My best code is written with the delete key.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Hooiii, you guys! Did you know that we're talking about template class ?
    Anyway, I'm gonna go for one file .h(declaration and definition in one file). It seems to be the most common way; I looked up MSDN as well. I tried keyword export but .NET does not support it. What still puzzles me is that the method that I mentioned on my first post worked one time on .NET but never work again since then. It always works on g++, tho. But whatever, I made up my mind with one file template class
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM