2011-03-16 104 views
1

3年多来我还没有使用过指针,我对这个主题非常生疏。编译下面的代码时,我收到了很多错误。这些错误如下:指针编译器问题

[email protected]:~/Desktop/stuff$ g++ test.cpp LinearNode.cpp LinkedList.cpp 

LinkedList.cpp: In member function ‘void LinkedList::add(int)’: 

LinkedList.cpp:26: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:31: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:39: error: request for member ‘setPrevious’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:40: error: cannot convert ‘LinearNode’ to ‘LinearNode*’ in assignment 

LinkedList.cpp: In member function ‘int LinkedList::remove(int)’: 

LinkedList.cpp:60: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:62: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:63: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 

LinkedList.cpp:67: error: invalid conversion from ‘LinearNode*’ to ‘int’ 

LinkedList.cpp:67: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’ 

LinkedList.cpp:68: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’ 
LinkedList.cpp: In member function ‘void LinkedList::print()’: 
LinkedList.cpp:97: error: invalid conversion from ‘LinearNode*’ to ‘int’ 
LinkedList.cpp:97: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’ 

链接List.h:

#ifndef LINKEDLIST_H 
#define LINKEDLIST_H 
#include<iostream> 
#include"LinearNode.h" 

using namespace std; 

class LinearNode; 

class LinkedList 
{ 
    public: 
     LinkedList(); 
     void add(int element); 
     int remove (int element); 
     void print(); 

    private: 
     int count; 
     LinearNode* contents; 
};//ends the class linked list 

#endif 

链接列表:

#include<iostream> 
#include"LinearNode.h" 
#include"LinkedList.h" 

using namespace std; 

//linkedlist constructor for an empty linked list 
LinkedList::LinkedList() 
{ 
    count = 0; 
    contents = NULL; 
}//ends the constructor 

//adds an element to the front of the linked list 
void LinkedList::add(int element) 
{ 
    int found = 0, current = 0; 

    for (int index = 0; index < count; index++) 
    { 
     if (contents.getElement() == element) 
      found = 1; 
     else  
     { 

      contents = *contents.getNext(); 
     }//ends the else statement 
    }//ends the while loop 

    if (found == 0) 
    { 
     LinearNode node(element); 
     node.setNext(contents); 
     contents.setPrevious(&node); 
     contents = node; 
     count++; 

//print(); 
cout << endl; 

    }//ends the found == 0 if statment 
}//ends the add function 

//this function removes one element from the linked list. 
int LinkedList::remove(int element) 
{ 
    int found = 0, result = 0; 
    LinearNode previous; 
    LinearNode current; 

    if (count == 0) 
     cout << "The list is empty" << endl; 
    else 
    { 
     if (contents.getElement() == element) 
     { 
      result = contents.getElement(); 
      contents = *contents.getNext(); 
     }//ends the contents.getElement() == element 
     else 
     { 
      previous = contents; 
      current = *contents.getNext(); 
      for (int index = 0; ((index < count) && (found == 0)); index++) 
       if (current.getElement() == element) 
        found = 1; 
       else 
       { 
        previous = current; 
        current = *current.getNext(); 
       }//ends the else statement 

      if (found == 0) 
       cout << "The element is not in the list" << endl; 
      else 
      { 
       result = current.getElement(); 
       previous.setNext(current.getNext()); 
      }//ends else statement 

     }//ends the else stamtement 

     count--; 
    }//ends the else statement of count == 0 
    return result; 
}//ends the remove function 


void LinkedList::print() 
{ 
    LinearNode current; 
    current = contents; 

    for (int index = 0; index < count; index++) 
    { 
     cout << current.getElement() << endl; 
     current = *current.getNext(); 
    }//ends the for loop 
}//ends Print function 

LinearNode.h:

#ifndef LINEARNODE_H 
#define LINEARNODE_H 

#include<iostream> 

using namespace std; 

class LinearNode 
{ 
    public: 
     //Constructor for the LinearNode class that takes no arguments 
     LinearNode(); 
     //Constructor for the LinearNode class that takes the element as an argument 
     LinearNode(int el); 
     //returns the next node in the set. 
     LinearNode* getNext(); 
     //returns the previous node in the set 
     LinearNode* getPrevious(); 
     //sets the next element in the set 
     void setNext(LinearNode* node); 
     //sets the previous element in the set 
     void setPrevious(LinearNode* node); 
     //sets the element of the node 
     void setElement(int el); 
     //gets the element of the node 
     int getElement(); 

    private: 
     LinearNode* next; 
     LinearNode* previous; 
     int element;   
};//ends the LinearNode class 

#endif 

LinearNode:

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

using namespace std; 

//Constructor for LinearNode, sets next and element to initialized states 
LinearNode::LinearNode() 
{ 
    next = NULL; 
    element = 0; 
}//ends LinearNode default constructor 

//Constructor for LinearNode takes an element as argument. 
LinearNode::LinearNode(int el) 
{ 
    next = NULL; 
    previous = NULL; 
    element = el; 
}//ends LinearNode constructor 

//returns the next element in the structure 
LinearNode* LinearNode::getNext() 
{ 
    return next; 
}//ends getNext function 

//returns previous element in structure 
LinearNode* LinearNode::getPrevious() 
{ 
    return previous; 
}//ends getPrevious function 

//sets the next variable for the node 
void LinearNode::setNext(LinearNode* node) 
{ 
    next = node; 
}//ends the setNext function 

//sets previous for the node 
void LinearNode::setPrevious(LinearNode* node) 
{ 
    previous = node; 
}//ends the setPrevious function 

//returns element of the node 
int LinearNode::getElement() 
{ 
    return element; 
}//ends the getelement function 

//sets the element of the node 
void LinearNode::setElement(int el) 
{ 
    element = el; 
}//ends the setElement function 
+1

如果你只是需要一个工作链表,您可以使用[STL列表(http://www.yolinux.com /TUTORIALS/LinuxTutorialC++STL.html#LIST)得到一个已经被调试过的。如果你想了解更多关于STL列表模板的信息,请[cplusplus.com有更详细的参考](http://cplusplus.com/reference/stl/list/)。 – sarnold 2011-03-16 01:47:41

+0

@sarnold这可能是一个家庭作业问题。 – mgiuca 2011-03-16 02:00:02

+0

@mgiuca的确,或者是在面试前试图指向好的一个尝试:)但是如果自从原始海报完成C++以来已经有一段时间了,他或她可能并不熟悉强大的预先写好的惊人范围工具。 – sarnold 2011-03-16 02:34:46

回答

3

有许多在这里单独的错误,而是指针之一是,你使用的是点(.)来访问指针到成员-类。该点用于访问类对象的成员(不是指针)。您应该使用箭头(->)来访问指向类的成员。

例如,

if (contents.getElement() == element) 

应该是

if (contents->getElement() == element) 
2

由于您使用的指针为contents,因此您需要正确地遵守以访问成员。例如,第一个错误是由LinkedList::add造成的:

void LinkedList::add(int element) 
{ 
    int found = 0, current = 0; 

    for (int index = 0; index < count; index++) 
    { 
     // This is your first error 
     //if (contents.getElement() == element) 

     // Change to: 
     if (contents->getElement() == element) 
      found = 1; 
2

.具有超过*一个更高的优先级。因此,改变

contents = *contents.getNext(); 

contents = (*contents).getNext(); 

而且你必须在多个地方进行纠正。

如果您需要摆脱所有这些,只需使用 -

contents = contents->getNext(); 
+2

这是正确的,但看到'(* contents).getNext()'很奇怪。你应该使用更传统的风格'contents-> getNext()',这意味着同样的事情。还要注意,大多数使用点的地方根本没有'*'。 – mgiuca 2011-03-16 01:58:57

+0

@mguica - 你是对的。使用' - >'很容易且可读。我将它指出来,因为它被用在片段中。 – Mahesh 2011-03-16 02:00:32