未定义的引用 [英] Undefined reference to

查看:202
本文介绍了未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码:

//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'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.

推荐答案

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

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

main.cpp中,(从List.h)看到嘿,具有此功能的此类将存在",但是由于您实际上并未在List.cpp中进行构建/链接,因此找不到该功能正在寻找.

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.

这篇关于未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆