2013-01-31 40 views
0

我有這些three.h文件給我這麼多令人難以置信的錯誤,必須有一些我忽略的基本問題。我的目標是創建一個有效的鏈表。我意識到有一些邏輯問題,但現在,我更感興趣的是編譯它。託管C++ - 一般編譯錯誤

基本節點類:

#pragma once 

#include "stdafx.h" 
#include <iostream> 
#include <deque> 
#include <climits> 
#include "FreeList.h" 

using namespace std; 
using namespace System; 
using namespace System::Collections; 
using namespace System::Collections::Generic; 

namespace ListTestProgram 
{ 
generic <typename E> ref class LinkNode 
{ 
private: 
    LinkNode<E>^ ptr; 
    FreeList<E>^ freelist; 

public: 
    E element; 
    LinkNode^ next; 

    // constructors 
    LinkNode(E elemval, LinkNode<E>^ nextval = nullptr) 
    { 
     element = elemval; next = nextval; 
     freelist = gcnew FreeList<E>; 
    } 

    LinkNode(LinkNode<E>^ nextval = nullptr) 
    { 
     next = nextval; 
     freelist = gcnew FreeList<E>; 
    } 

    LinkNode<E>^ newNode() 
    { 
     freelist->nextNode(); 
    } 

    void deleteNode(LinkNode<E>^ ptr) 
    { 
     freelist->add((LinkNode<E>^)ptr)->next; 
    } 
}; 
} 

Link類:

// LinkList.h provides for implementation of a linked list 
#pragma once 

#include "stdafx.h" 
#include "LinkNode.h" 
#include <iostream> 
#include <deque> 
#include <climits> 

using namespace std; 
using namespace System; 
using namespace System::Collections; 
using namespace System::Collections::Generic; 

namespace ListTestProgram 
{ 
generic <typename E> ref class LinkList 
{ 
private: 
    LinkNode<E>^ head; // pointer to list header 
    LinkNode<E>^ tail; // pointer to last element 
    LinkNode<E>^ curr; //access to current element 
    int cnt;  // size of list 
    static int defaultSize = 100; 

    void init()  // Initializer 
    { 
     curr = tail = head = gcnew LinkNode<E>(); 
     cnt = 0; 
    } 

    void removal()     // Return link nodes to free store 
    { 
     while (head != nullptr) 
     { 
      curr = head; 
      head = head->next; 
      delete curr; 
     } 
    } 

public: 
    LinkList<E>(){} 
    LinkList<E>(int size=defaultSize) {init(); }  // Constructor 
    ~LinkList() { removal(); }      // Destructor 
    void print();   // print list contents 
    void clear() { removal(); init(); }  // Clear list 

    // Insert "it" at current position 
    void insert (E it) 
    { 
     curr->next = gcnew LinkNode<E>(it, curr->next); 
     if(tail == curr) tail = curr->next;  // new tail 
     cnt++; 
    } 

    void append(E it) 
    { 
     tail = tail->next = gcnew LinkNode<E> (it, nullptr); 
     cnt++; 
    } 

    // Remove and return current element 
    E remove() 
    { 
     assert(curr->next != nullptr, "No element"); 
     E it = curr->next->element;  // Remember value 
     LinkNode<E>^ ltemp = curr->next;  // Rember link node 
     if(tail == curr->next) tail = curr; // reset tail 
     curr->next = curr->next->next;  // remove from list 
     delete ltemp; 
     cnt--; 
     return it; 
    } 

    void moveToNext() // Place curr to head of list 
    { 
     curr = head; 
    } 

    void moveToEnd() // Place curr at end of list 
    { 
     curr = tail; 
    } 

    // Move curr one step left; no change if already at front 
    void prev() 
    { 
     if (curr == head) return; 
     LinkNode<E>^ temp = head; 
     // March down the list until we find the previous element 
     while (temp->next!=curr) temp= temp->next; 
     curr = temp; 
    } 

    // Move curr one step right; no change if already at end 
    void next() 
    { 
     if (curr != tail) curr = curr->next; 
    } 

    // Return the position of the current element 
    int currPos() 
    { 
     LinkNode<E>^ temp = head; 
     int i; 
     for (i = 0; curr != temp; i++) 
      temp = temp->next; 
     return i; 
    } 

    // Move down list to "pos" position 
    void moveToPos(int pos) 
    { 
     assert((pos>=0)&&(pos<=cnt), "Position out of range."); 
     curr = head; 
     for (int i=0; i<pos; i++) curr = curr->next; 
    } 

    E getValue() 
    { 
     assert(curr->next != nullptr, "No value"); 
     return curr->next->element; 
    } 

}; 
} 

而且空閒列表類,我想認爲,從鏈表中刪除,以便他們可以重複使用的節點,避免重複呼叫刪除和新。

// FreeList.h provides a stack of nodes from which new nodes from the LinkNode class can be 
// derived. This is to limit the number of calls to gcnew, to minimize proceccor time. Items in the 
// FreeList stack are returned to LinkNode calls when a new node is called for. 

#pragma once 

#include "stdafx.h" 
#include <iostream> 
#include <deque> 
#include <climits> 
#include "LinkNode.h" 

using namespace std; 
using namespace System; 
using namespace System::Collections; 
using namespace System::Collections::Generic; 

namespace ListTestProgram 
{ 
generic <typename E> ref class FreeList 
{ 
    /**** members ****/ 
public: Stack^ freelist; 

    /**** constructors/Destructors ****/ 
    FreeList() { freelist = gcnew Stack; } 

    ~FreeList(){} 

    /**** methods ****/ 

    // return an instance of LinkNode 
    LinkNode<E>^ nextNode() 
    { 
     if (freelist->Count != 0) 
      return freelist->Pop(); 
     else 
     { 
      repopulateStack(); 
      nextNode(node); 
     } 
    } 

    // repopulate an empty stack 
    void repopulateStack() 
    { 
     for (int i = 0; i < 100; i++) 
      freelist->Push(gcnew LinkNode<E>()); 
    } 

    // add a deleted node to the freelist for later use 
    void add(LinkNode<E>^ trash) 
    { 
     freelist->Push(trash); 
    } 
}; 
} 

我的一些錯誤是:

Error 13 error C2039: 'nextNode' : is not a member of 'ListTestProgram::FreeList<E>' c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\LinkNode.h 43 1 TestingLists 
Error 6 error C2061: syntax error : identifier 'LinkNode' c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\FreeList.h 52 1 TestingLists 
Error 10 error C2065: 'trash' : undeclared identifier c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\FreeList.h 54 1 TestingLists 
Error 3 error C2143: syntax error : missing ';' before '<' c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\FreeList.h 33 1 TestingLists 
Error 1 error C2872: 'Stack' : ambiguous symbol c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\FreeList.h 23 1 TestingLists 
Error 6 error C2061: syntax error : identifier 'LinkNode' c:\users\ed\documents\visual studio 2012\school_projects\cpp programs\cpp_advanced\testinglists\testinglists\FreeList.h 52 1 TestingLists 

回答

1

你的頭有循環依賴:FreeList.h取決於LinkNode.h取決於FreeList.h。頭文件依賴性必須形成一個有向非循環圖。

+0

如果我沒有記錯,我只能在不打算使用任何類的方法時使用前向聲明,而只是實例化它。與它混淆,但仍然沒有喜悅。 – deadEddie

+0

感謝James的幫助。對不起,我花了很長時間才接受。 – deadEddie