Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts

    C++ Class Header Problem

    I have the following header file, ListNode.h
    Code:
    #ifndef LISTNODE_H_
    #define LISTNODE_H_
    
    template <class E>
    class ListNode {
       public:
          ListNode();
          ListNode(E element);
          E element;
          ListNode<E>* next;
    };
    
    #endif
    And here is the corresponding source file, ListNode.cpp
    Code:
    #include "ListNode.h"
    #include <cstdlib>
    
    using namespace std;
    
    template <class E>
    ListNode<E>::ListNode() {
       next = NULL;
    }
    
    template <class E>
    ListNode<E>::ListNode(E element) {
       this->element = element;
       next = NULL;
    }
    Here is essentially what my main.cpp is
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "ListNode.h"
    
    using namespace std;
    
    int main() {
       ListNode<int> node();
       return 0;
    }
    Now, let me explain my problems:
    If I do
    Code:
    node.element = 7
    I get a compile error.
    If I instead have
    Code:
    ListNode<int>* node = new ListNode<int>();
    node->element = 7;
    delete node;
    it compiles. However, if I change the import statement to "ListNode.h", it no longer compiles. This (the change of import file extension) I don't think is an issue, but the issue of not being able to use my class without the new keyword is.
    Last edited by Trinithis; 10-21-2007 at 08:55 PM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?

    The next few questions are also pertinent. Long story short: unlike non-template functions, the compiler always needs the definition as well as the declaration of a template function to determine what you're trying to do.

    Quick fix:

    Leave ListNode.cpp as it is.

    Alter ListNode.h to include its own definitions:
    Code:
    #ifndef LISTNODE_H_
    #define LISTNODE_H_
    
    template <class E>
    class ListNode {
       public:
          ListNode();
          ListNode(E element);
          E element;
          ListNode<E>* next;
    };
    
    #include "ListNode.cpp" // ***
    
    #endif
    Do whatever in main, I combined most of the relevant stuff:
    Code:
    #include <iostream>
    #include "ListNode.h"
    
    int main() {
       ListNode<int> node(12);
       ListNode<int> node2; 
       ListNode<int>* dnode = new ListNode<int>(12);
       ListNode<int>* dnode2 = new ListNode<int>;
       node2.element = 12;
       dnode2->element = 12;
       std::cout << node.element << node2.element << dnode->element << dnode2->element;
       delete dnode;
       delete dnode2;
       return 0;
    }
    All the template stuff is now in the same compilation unit:
    Code:
    $ g++ -Wall -Wextra -o main.out ./main.cpp && ./main.out
    12121212

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Thank you so much. I spent hours trying to figure this baby out


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •