2011-03-16 7 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 :

,536,913을 다음과 같이 오류가 있습니다
#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

, 당신은 http://www.yolinux.com합니다 ([STL 목록]을 사용할 수 있습니다 /TUTORIALS/LinuxTutorialC++STL.html#LIST) 이미 디버깅 된 것을 얻으십시오. STL 목록 템플릿에 대한 자세한 내용은 [cplusplus.com에 자세한 참조 정보가 있습니다.] (http://cplusplus.com/reference/stl/list/)를 참조하십시오. – sarnold

+0

@sarnold 숙제 문제 일 수 있습니다. – mgiuca

+0

@mgiuca, 또는 취업 인터뷰 전에 포인터를 얻으려는 시도 :) 그러나 원래 포스터가 C++을 수행 한 지 오래 되었 더라면 강력한 사전 작성된 놀라운 범위에 익숙하지 않을 수도 있습니다 도구. – sarnold

답변

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

+0

@mguica - 정확합니다. '-> '사용은 쉽고 읽기 쉽습니다. 그것이 스 니펫에서 사용되기 때문에 나는 그것을 지적하고있다. – Mahesh