2011-03-13 56 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 2011-03-13 22:58:43

+1

另外,*从不*在'.h文件中放置'使用namespace std;'。 – 2011-03-13 23:01:12

+0

下次还粘贴错误信息。 – karlphillip 2011-03-13 23:12:05

回答

13
  1. 一般标头警卫头文件(即,.h)不是源文件(即,.cpp)。
  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 

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 2011-03-14 23:28:33

+2

@ tpar44编程以来,这已经很长时间了 - 没有问题。即使我也是一个初学者。即使是经验丰富的人也可以在C++中犯错误。这是语言:) – Mahesh 2011-03-14 23:37:38

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

这将解决生成'Undefined Reference to'错误的链接器依赖性。但是,只有当所有文件都在同一个目录中时,这才会起作用。 – karlphillip 2011-03-13 23:09:42

+0

感谢您的帮助!这样可行! – tpar44 2011-03-14 23:27:31

+0

没问题,哥们。 – karlphillip 2011-03-14 23:29:47

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

帮助我找出我的编剧有什么问题 – 2017-10-01 13:02:36