未定义的引用

时间:2010-04-02 21:52:34

标签: c++ linker undefined-reference

每次我尝试编译时都会收到此错误消息,但我无法找出问题所在。任何帮助将不胜感激:

C:\DOCUME~1\Patrick\LOCALS~1\Temp/ccL92mj9.o:main.cpp:(.txt+0x184): undefined reference to 'List::List()'
C:\DOCUME~1\Patrick\LOCALS~1\Temp/ccL92mj9.o:main.cpp:(.txt+0x184): undefined reference to 'List::add(int)'
collect2:  ld returned 1 exit status

代码:

//List.h

#ifndef LIST_H
#define LIST_H

#include <exception>

//brief Definition of linked list class

class List
{
    public:

    /**
    \brief Exception for operating on empty list
    */  


    class Empty : public std::exception
 {
  public:
  virtual const char* what() const throw();
 };

    /**
    \brief Exception for invalid operations other than operating on an empty list
    */

    class InvalidOperation : public std::exception
 {
  public:
  virtual const char* what() const throw();
 };

    /**
    \brief Node within List
    */


    class Node
 {
  public: 
  /** data element stored in this node */
  int element;

  /** next node in list */
  Node* next;

  /** previous node in list */
  Node* previous;

  Node (int element);
  ~Node();

  void print() const;
  void printDebug() const;
 };


    List();
    ~List();

    void add(int element);
    void remove(int element);
    int first()const;
    int last()const;
    int removeFirst();
    int removeLast();
    bool isEmpty()const;
    int size()const;
    void printForward() const;    
    void printReverse() const;
    void printDebug() const;

    /**
    enables extra output for debugging purposes
    */
    static bool traceOn;

    private:
    /** head of list */
    Node* head;
    /** tail of list */
    Node* tail;
    /** count of number of nodes */
    int count;
};
#endif


//List.cpp                   I only included the parts of List.cpp that might be the issue
#include "List.h"
#include <iostream>
#include <iomanip>

using namespace std;


List::List()
{
 //List::size = NULL;
 head = NULL;
 tail = NULL;
}


List::~List()
{
 Node* current;
 while(head != NULL)
 {
  current = head-> next;
  delete current->previous;
  if (current->next!=NULL)
  {
   head = current;
  }
  else
  {
   delete current;
  }
 }
}

void List::add(int element)
{
 Node* newNode;
 Node* current;
 newNode->element = element;
 if(newNode->element > head->element)
 {
  current = head->next;
 }
 else
 {
  head->previous = newNode;
  newNode->next = head;
  newNode->previous = NULL;
  return;
 }

 while(newNode->element > current->element)
 {
  current = current->next;
 }

 if(newNode->element <= current->element)
 {
  newNode->previous = current->previous;
  newNode->next = current;
 }

}


//main.cpp
#include "List.h"
#include <iostream>
#include <string>

using namespace std;
//void add(int element);

int main (char** argv, int argc)
{    
 List* MyList = new List();
 bool quit = false;
 string value;
 int element;

 while(quit==false)
 {
  cin>>value;

  if(value == "add")
  {
   cin>>element;
   MyList->add(element);
  }
  if(value=="quit")
  {
   quit = true;
  }
 }
    return 0;
}

我正在做我认为我想做的一切。 main.cpp还没有完成,只是试图让add函数先工作。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:3)

描述您的构建过程。看起来好像你没有构建List.cpp,或者没有将它与main.cpp链接。

答案 1 :(得分:3)

您没有编译List.cpp。将其添加到命令行。

main.cpp中,它看到(来自List.h)“嘿,这个具有此功能的类将存在”,但由于您实际上并未构建/链接List.cpp,找不到它正在寻找的功能。

答案 2 :(得分:1)

您的命令行应该类似于g++ -o test.exe main.cpp List.cpp

关键功能是同时包含main.cppList.cpp

还有其他方法可以做到这一点,但这应该让你开始。

答案 3 :(得分:0)

您的问题不包括命令行arg编译器中的所有不同文件

格式正确:

进入正确的目录

gcc -o list main.cpp List.cpp List.h

然后你不会再获得对函数的未定义引用

祝你好好建立3或4岁的课程......