Creating a new type of template container in C ++

Ok, so I am trying to implement a template two-line list in C ++. The problem I am facing is that when I try to separate the class and function definition from the .h and .cpp files respectively, the compiler continues to spit on me errors related to the lack of template parameters.

Here is the header file, cdl_list.h

#ifndef CDL_LIST_H
#define CDL_LIST_H

#include <iostream>


using namespace std;


template <typename T>
class cdl_list {

        public:

        cdl_list(){
                first = last = current = NULL;};     // constructor, "current" = "null"
        ~cdl_list();    // deconstructor
        void insertFromFront (T &);     // inserts an element of type T in the front properly
        void insertFromBack (T &);      // inserts an element of type T in the back properly
        void deleteFront();     // removes the first element in the list, updating relevant pointers
        void deleteBack();      // removes the last element in the list, updating relevant pointers
        void reset();   // makes the "current" pointer the front element
        void next();    // makes the "current" pointer the next node neighbor
        T currentValue();       // return the data in the node that "current" refers to
        void deleteCurrent(); // delete the node that the current pointer refers to; current = old -> next
        bool isEmpty(); // returns true if and only if the list is empty
        void print();   // displays the current data in the linked list


        private:

        struct listNode* first;
        struct listNode* last;
        struct listNode* current;


        protected:

        struct listNode {
                listNode* prev; // "previous" pointer
                T data; // data in the node
                listNode* next; // "next" pointer
        };
};

#include "cdl_list.h"
#include <iostream>

using namespace std;



//And here is the .cpp file, what I have so far, because I can't even get just this one function to compile

template < typename T > void cdl_list::insertFromFront(const T &frontInsert) {
        listNode *oldFirst;
        oldFirst = (listNode *) malloc(sizeof(listNode));
        oldFirst = first;
        oldFirst -> prev = frontInsert;
        while(current -> next != first)
                current = current -> next;
        frontInsert -> prev = current;
        current -> next = frontInsert;
        first = frontInsert;

}



#endif
+3
source share
4 answers

Unfortunately, because of how the C ++ compilation process works, they must be in the same file (there are other workarounds besides this, but this is the most common option). For more details see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12 .

+4

.cpp . , "" . . , , - Foo<Bar> , . , , . , .

. , "bleah.h"

template<typename T>
struct Foo{ T value; }

"Yuck.h":

#include "bleah.h"

Foo<int> something;  //compiler stops here and compiles a new class for Foo<int>
Foo<int> another; //compiler doesn't need to generate a new Foo<int>, already done
Foo<double> oh; //compiler needs to make a new class Foo<double>

, , "Foo".

+1

try adding <T> to class name when defining function in cpp

eg.

template<class T> void myclass<T>::func_name() {}

as other posters pointed out, you should put the material that you put the .cpp in the .inl file and #include "myfile.inl" right at the end of the .h file and avoid using .cpp files for templates.

0
source

You cannot separate an implementation from an declaration using template classes, as you can, using regular classes. You can do it, but it's a little "hacked:"

// Template.h

#ifndef TEMPLATE_H_INCLUDED
#define TEMPLATE_H_INCLUDED

template <typename ClassDatatype>
class MyTemplateClass
{
    template <typename MethodDatatype>
    void MyMethod(MethodDatatype Argument);
}

#include "Template.cpp"

#endif

// Template.cpp

#ifndef TEMPLATE_CPP_INCLUDED
#define TEMPLATE_CPP_INCLUDED

#include "Template.h"

template <typename ClassDatatype>
template <typename MethodDatatype>
void MyTemplateClass<ClassDatatype>::MyMethod(MethodDatatype Argument)
{
    // Implementation goes here.
}

#endif
0
source

Source: https://habr.com/ru/post/1796194/


All Articles