c ++ - 带模板的链接列表(错误c2039)

时间:2017-05-11 12:24:54

标签: c++ templates

你好我有一个问题我无法解决而且我不明白为什么,我很乐意解释这个问题以及如何解决它。

对我来说,一切似乎都很好。我不明白问题出在哪里。

我得到的错误(更多的错误,写下所有内容太多了):

Error   7   error C2039: 'getData' : is not a member of '`global namespace''    
c:\projects\linkelisted\ListNode.h  31  1   LinkeListed
Error   26  error C2039: 'getNewNode' : is not a member of 'List'   
c:\projects\linkelisted\List.h  127 1   LinkeListed
Error   6   error C2039: 'ListNode' : is not a member of '`global 
namespace'' 
c:\projects\linkelisted\ListNode.h  26  1   LinkeListed
Error   5   error C2059: syntax error : '<' 
c:\projects\linkelisted\ListNode.h  26  1   LinkeListed

ListNode.h:

#ifndef LISTNODE_H
#define LISTNODE_H
#include <iostream>
#include <vector>
using namespace std;

template<class NODETYPE> List;
template<class NODETYPE>
class ListNode {
friend class List<NODETYPE>;

private:
NODETYPE data;
ListNode<NODETYPE> *nextPtr;

public:
ListNode(const NODETYPE &);
NODETYPE getData()const;
};

template<class NODETYPE>
ListNode <NODETYPE>::ListNode(const NODETYPE &info) :data(info), nextPtr(0) 
{}

template<class NODETYPE>
NODETYPE ListNode<NODETYPE>::getData()const{
return data;
}
#endif

List.h:

#ifndef List_H
#define List_H
#include <iostream>
#include <vector>

using namespace std;

#include "ListNode.h"

template<class NODETYPE>
class List {
ListNode<NODETYPE> *firstPtr;
ListNode<NODETYPE> *lastPtr;
ListNode <NODETYPE> *getNewNode(const NODETYPE &value);

public:
List();
~List();
void insertAtFront(const NODETYPE &value);
void insertAtBack(const NODETYPE &value);
void removeFromFront(NODETYPE &value);
void removeFromBack(NODETYPE &value);
bool isEmpty()const;
void print()const;
};

template<class NODETYPE>
List<NODETYPE>::List() : firstPtr(0), lastPtr(0){
}

template<class NODETYPE>
List<NODETYPE>::~List(){
if (!isEmpty()){
    cout << "Destroying nodes ...\n";

    ListNode<NODETYPE> *currentPtr = firstPtr;
    ListNode<NODETYPE> *tempPtr;

    while (currentPtr != 0){
        tempPtr = currentPtr;
        cout << tempPtr->data << endl;
        delete tempPtr;
    }}}

template<class NODETYPE> 
void List<NODETYPE>::insertAtFront(const NODETYPE &value){
ListNode<NODETYPE> *newPtr = getNewNode(value);

if (isEmpty())
    firstPtr = lastPtr = newPtr;
else{
    newPtr->nextPtr = firstPtr;
    firstPtr = newPtr;
}}


template<class NODETYPE> 
void List<NODETYPE>::insertAtBack(const NODETYPE &value){
ListNode<NODETYPE> *newPtr = getNewNode(value);

if (isEmpty()) 
    firstPtr = lastPtr = newPtr;
else{
    lastPtr->nextPtr = newPtr;
    lastPtr = newPtr;
}}

template<class NODETYPE>
bool List<NODETYPE>::removeFromFront(NODETYPE &value){
if (isEmpty())
    return false;
else{
    ListNode<NODETYPE> *tempPtr = firstPtr;

    if (firstPtr == lastPtr)
        firstPtr = lastptr = 0;
    else{
        firstPtr = firstPtr->nextPtr;   
        delete tempPtr;
        return true;
        value = tempPtr->data;
        } }}

template<class NODETYPE> 
bool List <NODETYPE>::removeFromBack(NODETYPE &value){
if (isEmpty())
    return false;
   else{
    ListNode<NODETYPE> *tempPtr = lastPtr;

    if (firstPtr == lastPtr)
        firstPtr = lastPtr = 0;
    else{
        ListNode<NODETYPE> *current = firstPtr;

        while (currentPtr->nextPtr != lastPtr)
            currentPtr = currentPtr->nextPtr;
        lastPtr = currentPtr;
        currentPtr->nextPtr = 0;
    }
    value = tempPtr->data;
    delete tempPtr;
    return true;
     }
 }

template<class NODETYPE>
    bool List < NODETYPE >::isEmpty()const{
    return firstPtr == 0;
}

template<class NODETYPE>
ListNode<NODETYPE> *List <NODETYPE>::getNewNode(const NODETYPE &value){
    return new ListNode<NODETYPE>(value);
  }

  template<class NODETYPE> 
  void List<NODETYPE>::print()const{
    if (isEmpty()){
        cout << "The list is empty\n\n";
        return
    }

    ListNode<NODETYPE> *currentPtr = firstPtr;

    cout << "The list is: "; 

    while (currentPtr != 0){
        cout << currentPtr->data << endl;
        currentPtr = currentPtr->nextPtr;
    }
    cout << "\n\n";
 }
 #endif

主要:

#include <iostream>
#include <vector>
#include "List.h"

using namespace std;

int main() {

List <int> integerList;

integerList.isEmpty();

system("pause");
return 0;
}

感谢&#39; S

0 个答案:

没有答案