2011-03-13 3 views
17

링크 된 목록에 대한 코드를 컴파일 할 때 정의되지 않은 참조 오류가 많습니다. 코드는 다음과 같습니다. 나는 두 문장 모두 컴파일되었습니다정의되지 않음 참조

g++ test.cpp 

뿐만 아니라

g++ LinearNode.h LinearNode.cpp LinkedList.h LinkedList.cpp test.cpp 

나는 이러한 오류를 얻고있다 왜 C에서 클래스에 정말 녹슨이기 때문에 난 정말 ++ 이해하지 못하고 있습니다. 나는 약간의 도움을 정말로 사용할 수있다.

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.cpp :

#ifndef LINEARNODE_cpp 
#define LINEARNODE_cpp 
#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 = 0; 
}//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 

#endif 

LinkedList.h :

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

using namespace std; 

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

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

#endif 

LinkedList.cpp :

#ifndef LINKEDLIST_CPP 
#define LINKEDLIST_CPP 

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

    while((found == 0) && (current !=count)) 
    { 
     if (contents.getElement() == element) 
      found = 1; 
     else  
     { 
      contents = contents.getNext(); 
      current++; 
     }//ends the else statement 
    }//ends the while loop 

    if (found == 0) 
    { 
     LinearNode node = new LinearNode(element); 
     node.setNext(contents); 
     contents.setPrevious(node); 
     count++; 
    }//ends the found == 0 if statment 
}//ends the add function 

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

    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 

#endif 
,

Test.cpp에가 :

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

using namespace std; 

int main() 
{ 

    LinearNode node1, node2, node3, move; 
    LinkedList list;  

    node1.setElement(1); 
    node2.setElement(2); 
    node3.setElement(3); 
} 
+2

먼저 ".h"파일을 컴파일러의 매개 변수로 사용하지 마십시오. 이미 ".cpp"파일에 포함되어 있습니다. 누락 된 참조는 무엇입니까? 링커 오류가 무엇입니까? – littleadv

+1

또한, .h 파일에'using namespace std;'를 절대 사용하지 마십시오. –

+0

다음에 오류 메시지를 붙여 넣습니다. – karlphillip

답변

13
  1. 보통 가드가없는 소스 파일 (즉, .cpp)에 대한 (즉, .h) 헤더 파일에있는 헤더.
  2. 소스 파일에 필요한 표준 헤더 및 네임 스페이스를 포함하십시오.

LinearNode.h :

#ifndef LINEARNODE_H 
#define LINEARNODE_H 

class LinearNode 
{ 
    // ..... 
}; 

#endif 

LinearNode.cpp :

#include "LinearNode.h" 
#include <iostream> 
using namespace std; 
// And now the definitions 

LinkedList.h :

#ifndef LINKEDLIST_H 
#define LINKEDLIST_H 

class LinearNode; // Forward Declaration 
class LinkedList 
{ 
    // ... 
}; 

#endif 
,174,

LinkedList.cpp

#include "LinearNode.h" 
#include "LinkedList.h" 
#include <iostream> 
using namespace std; 

// Definitions 

Test.cpp에은 소스 파일이 괜찮이다. 헤더 파일은 컴파일되지 않습니다. 가정의 모든 파일을 하나의 폴더에 -

g++ LinearNode.cpp LinkedList.cpp test.cpp -o exe.out 
+0

도움을 주셔서 감사합니다! 나는 C++로 프로그래밍을 한 이래로 오랜 시간 동안 그 실수를 저 지르게 된 것은 정말 어리 석다. – tpar44

+2

@ tpar44 - 문제 없습니다. 나조차 너무 초보 다. 그리고 경험있는 사람조차도'C++ '에서 실수를 저지를 수 있습니다. 그런 언어입니다 :) – Mahesh

3
g++ test.cpp LinearNode.cpp LinkedList.cpp -o test 
+0

이것은 '정의되지 않은 참조'오류를 생성하는 링커 종속성을 해결합니다. 그러나 이것은 모든 파일이 같은 디렉토리에있는 경우에만 작동합니다. – karlphillip

+0

도움을 주셔서 감사합니다! 그게 효과가있어! – tpar44

+0

문제 없어요, 친구. – karlphillip

1

이 오류를 얻을 수있는 또 다른 방법입니다 실수로 익명의 공간에서 무언가의 정의를 작성하여 :

foo.h :

namespace foo { 
    void bar(); 
} 

foo.cc :

namespace foo { 
    namespace { // wrong 
     void bar() { cout << "hello"; }; 
    } 
} 

기타cc 파일 :

#include "foo.h" 

void baz() { 
    foo::bar(); 
} 
+0

내 prog에 무엇이 잘못되었는지 알아낼 수있게 도와주었습니다. –