Hello all;
Recently , I write a link list project using c++ template .But it's does work well.
Firstly , I use Inclusion compilation model so I add a sentence "#include "LinkList.cpp"" in head file, but it comes out an error " error: redefinition of 'LinkList<T>::LinkList()' ";
Secondly , I use seperate compilation model so I add a sentence "export template <class T> class LinkList;" in source file . However, there comes an error "error: undefined reference to `LinkList<char>::LinkList()' " too.
Thirdly , I add a sentence "#include "LinkList.cpp"; ". It's worked and the results are easy to understand!
But , how to use Inclusion compilation Model and separate compilation Model in Qt?
Thank for you reply!!!
//head file LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
template <class T>
struct ListNode
{
T data;
ListNode<T> *next;
};
template <class T>
class LinkList
{
public:
LinkList();
virtual ~LinkList();
bool GetElem(int loca,T &elem);
bool ListInsert(int loca,T elem);
bool ListDelete(int loca,T &elem);
void ListAppending(T item);
int ListLens();
private:
ListNode<T> *p_head;
int i_listlen;
};
#endif // LINKLIST_H
//source file LinkList.cpp
#include<iostream>
#include "LinkList.h"
template <class T>
...
//test file main.cpp
#include <iostream>
#include "LinkList.h"
int main()
{
LinkList<char> c_list;
c_list.ListAppending('a');
c_list.ListAppending('b');
c_list.ListAppending('c');
int lens = c_list.ListLens();
std::cout<<"lens = "<<lens<<std::endl;
return 0;
}
//head file LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
template <class T>
struct ListNode
{
T data;
ListNode<T> *next;
};
template <class T>
class LinkList
{
public:
LinkList();
virtual ~LinkList();
bool GetElem(int loca,T &elem);
bool ListInsert(int loca,T elem);
bool ListDelete(int loca,T &elem);
void ListAppending(T item);
int ListLens();
private:
ListNode<T> *p_head;
int i_listlen;
};
#endif // LINKLIST_H
//source file LinkList.cpp
#include<iostream>
#include "LinkList.h"
template <class T>
...
//test file main.cpp
#include <iostream>
#include "LinkList.h"
int main()
{
LinkList<char> c_list;
c_list.ListAppending('a');
c_list.ListAppending('b');
c_list.ListAppending('c');
int lens = c_list.ListLens();
std::cout<<"lens = "<<lens<<std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks