Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C++链表创建错误编译。架构x86_64的未定义符号:_C++_Linked List - Fatal编程技术网

用C++链表创建错误编译。架构x86_64的未定义符号:

用C++链表创建错误编译。架构x86_64的未定义符号:,c++,linked-list,C++,Linked List,我正在尝试创建一个链接列表电子书。我需要创建一个add方法,它接受一个Recipe对象参数并将其添加到链表的末尾 在mac上编译时,我收到错误: Undefined symbols for architecture x86_64: "Recipe::Recipe()", referenced from: RecipeBook::add(Recipe) in assign6-sendOnline-d41d53.o ld: symbol(s) not found for archit

我正在尝试创建一个链接列表电子书。我需要创建一个add方法,它接受一个Recipe对象参数并将其添加到链表的末尾

在mac上编译时,我收到错误:

Undefined symbols for architecture x86_64:
  "Recipe::Recipe()", referenced from:
      RecipeBook::add(Recipe) in assign6-sendOnline-d41d53.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
当我在我的linux服务器上编译时,我收到:

/tmp/ccFZOs7z.o: In function `RecipeBook::add(Recipe)':
/home/cs/dbrinkley/csci133/assign6/assign6.cpp:30: undefined reference to `Recipe::Recipe()'
collect2: error: ld returned 1 exit status
我觉得我试过各种各样的东西,但不管是什么,我就是找不到。我不确定是否错误地传递了对象参数或是什么

当我在main中删除对rb的函数调用时,文件就会编译。另外,当我删除整个add方法定义时,文件将编译

#include <iostream>
#include <string>

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe();
    Recipe(std::string inName, std::string inBodyText){
      name = inName;
      inBodyText = bodyText;
    };

    Recipe *next, *prev;
};


class RecipeBook {
  private:
    Recipe *head, *tail;    

  public:
    RecipeBook(){
      head = NULL;
      tail = NULL;
    }

    void add(Recipe r){
      Recipe *temp = new Recipe;
      *temp = r;
      if(head == NULL){
        head = temp;
        tail = temp;
        temp = NULL;
      } else {
        tail->next = temp;
        temp->prev = tail;
        tail = temp;
      }
    }
};



int main(){
  RecipeBook rb;
  Recipe r1("hamburger", "cook it");

  rb.add(r1);

  return 0;
}
错误解释 您在此处声明Recipe::Recipe存在:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe();
但实际上并没有定义它,因此编译器希望定义来自外部源,如单独链接的cpp文件

解决这个问题 修复它真的很容易。只需添加一个定义:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe() {} // Recipe has a definition now
class Recipe {
   public:
    std::string name;
    std::string bodyText;

    Recipe() = default;
或者,更好的是,只需默认定义:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe() {} // Recipe has a definition now
class Recipe {
   public:
    std::string name;
    std::string bodyText;

    Recipe() = default;
默认定义将使构造函数变得微不足道,这使编译器更容易执行某些优化。

错误解释 您在此处声明Recipe::Recipe存在:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe();
但实际上并没有定义它,因此编译器希望定义来自外部源,如单独链接的cpp文件

解决这个问题 修复它真的很容易。只需添加一个定义:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe() {} // Recipe has a definition now
class Recipe {
   public:
    std::string name;
    std::string bodyText;

    Recipe() = default;
或者,更好的是,只需默认定义:

class Recipe {
  public:
    std::string name;
    std::string bodyText;

    Recipe() {} // Recipe has a definition now
class Recipe {
   public:
    std::string name;
    std::string bodyText;

    Recipe() = default;
默认定义将使构造函数变得简单,这使编译器更容易执行某些优化。

new Recipe创建并默认构造一个新的Recipe对象。要能够默认构造对象,必须实现默认构造函数。新建配方创建并默认构造新配方对象。为了能够默认构造一个对象,您必须实现一个默认构造函数;在类声明中。然后构造函数也将保持平凡,没有用户定义的构造函数是平凡的——即使是空的;在类声明中。然后构造函数也将保持平凡,没有用户定义的构造函数是平凡的,即使是空的。