托管C ++ - 一般编译错误

时间:2013-01-31 20:29:48

标签: visual-c++ generic-list

我有这三个文件给了我很多令人难以置信的错误,我必须忽略一些基本问题。我的目标是创建一个有效的链表。我意识到存在一些逻辑问题,但是现在,我对编译它感兴趣。

基本节点类:

#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;
    }
};
}

链接类:

// 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类,我想保存从链表中删除的节点,以便可以重复使用它们,避免重复调用delete和new。

// 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 个答案:

答案 0 :(得分:1)

您的标头具有循环依赖关系:FreeList.h依赖于依赖于FreeList.h的LinkNode.h。头文件依赖关系必须形成有向的非循环图。