未定义的引用

发布于 2024-08-27 22:03:31 字数 3302 浏览 8 评论 0原文

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

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 函数首先工作。任何帮助将不胜感激。

I keep getting this error message every time I try to compile, and I cannot find out what the problem is. any help would be greatly appreciated:

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

code:

//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;
}

I'm doing everything I think I'm suppose to be doing. main.cpp isn't complete yet, just trying to get the add function to work first. Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-09-03 22:03:31

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

Describe your build process. It looks as though you're not building List.cpp, or else not linking it with main.cpp.

多情癖 2024-09-03 22:03:31

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

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

You're not compiling List.cpp. Add it to the command line.

In main.cpp, it's seeing (from List.h) "Hey, this class with this functionality will exist", but since you're not actually building/linking with List.cpp, it can't find the functions it's looking for.

雨巷深深 2024-09-03 22:03:31

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

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

还有其他方法可以做到这一点,但这应该可以帮助您入门。

Your command line should look something like g++ -o test.exe main.cpp List.cpp.

The key feature being to include both main.cpp and List.cpp.

There are other ways to do this, but this should get you started.

咋地 2024-09-03 22:03:31

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

正确格式:

进入正确的目录

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

然后您将不再获得未定义的结果函数引用

祝您构建 3 或 4 年前的程序好运...

Your problem is not including all the different files in your command line arg compiler

Correct format:

get in correct directory

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

then you won't get anymore undefined references to functions

Good luck on building your 3 or 4 year old program...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文