【问题标题】:C++, Xcode. Same error every time i try and use classesC++,Xcode。每次我尝试使用类时都会出现同样的错误
【发布时间】:2012-10-13 12:01:22
【问题描述】:

每次我在头文件中有一个类并且我在源文件中使用该类时,都会遇到同样的错误。不管是哪个类或哪个项目。

我正在尝试在链表数据结构的头部插入一个新节点。

现在我有一个非常简单的头文件main.h:

namespace linkedlistofclasses {
    class Node {
        public:
            Node();
            Node(int value, Node *next);
            //Constructor to initialize a node

            int getData() const;
            //Retrieve value for this node

            Node *getLink() const;
            //Retrieve next Node in the list

            void setData(int value);
            //Use to modify the value stored in the list

            void setLink(Node *next);
            //Use to change the reference to the next node

        private:
            int data;
            Node *link;
        };

    typedef Node* NodePtr;
}

我的源文件main.cpp 如下所示:

#include <iostream>
#include "main.h"

using namespace std;
using namespace linkedlistofclasses;

void head_insert(NodePtr &head, int the_number) {

    NodePtr temp_ptr;
    //The constructor sets temp_ptr->link to head and
    //sets the data value to the_number

    temp_ptr = new Node(the_number, head);
    head = temp_ptr;
}

int main() {

    NodePtr head, temp;

    //Create a list of nodes 4->3->2->1->0
    head = new Node(0, NULL);

    for (int i = 1; i < 5; i++) {
        head_insert(head, i);
    }

    //Iterate through the list and display each value 
    temp = head;
    while (temp !=NULL) {
        cout << temp->getData() << endl;
        temp = temp->getLink();
    }

    //Delete all nodes in the list before exiting
    //the program.
    temp = head;
    while (temp !=NULL) {
        NodePtr nodeToDelete = temp;
        temp = temp->getLink();
        delete nodeToDelete;
    }

    return 0;
}

我的问题是我得到了这些编译错误:

Undefined symbols for architecture x86_64:
  "linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
      head_insert(linkedlistofclasses::Node*&, int) in main.o
      _main in main.o
  "linkedlistofclasses::Node::getData() const", referenced from:
      _main in main.o
  "linkedlistofclasses::Node::getLink() const", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如果我在不使用类的情况下运行代码,将所有内容写入源文件main.cpp,就没有问题。 但是无论我如何编写一个类,我总是会得到这个错误的一些变体。

【问题讨论】:

  • class Node 的实现在哪里?
  • 你在哪里实现了类的成员函数?
  • 为您的班级创建单独的 .h 和 .cpp 文件。你真的不想使用main.h 文件

标签: c++ xcode class compiler-errors undefined


【解决方案1】:

您只是声明了以下类方法:

Node(int value, Node *next);
//Constructor to initialize a node

int getData() const;
//Retrieve value for this node

Node *getLink() const;
//Retrieve next Node in the list

void setData(int value);
//Use to modify the value stored in the list

void setLink(Node *next);
//Use to change the reference to the next node

这允许包含 ma​​in.h 的人看到 linkedlistofclasses::Node 及其 public 成员的存在。这样,您就可以调用它们,例如,从 ma​​in() 函数

但是,as they are simply declared and not defined,这会在以后引发问题:

**Undefined symbols for architecture x86_64:
"linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
  head_insert(linkedlistofclasses::Node*&, int) in main.o
  _main in main.o
"linkedlistofclasses::Node::getData() const", referenced from:
  _main in main.o
"linkedlistofclasses::Node::getLink() const", referenced from:
  _main in main.o**

出现这些错误消息是因为linker 不知道与这些方法名称关联的代码所在的程序内部表示中的区域。而且找不到是因为你一开始没有定义它!考虑一下这种情况:调用一个代码不存在的函数有什么意义?

我建议您创建一个 Node.h 和 Node.cpp 文件,第一个带有声明,第二个带有定义 (again, differences between the two concepts),然后是 #include Node。 h 来自 main.c?

在当前情况下,您会注意到,如果您另外调用其中任何一个:

void setData(int value);
//Use to modify the value stored in the list
void setLink(Node *next);
//Use to change the reference to the next node

他们的名字将被添加到您已经收到的错误消息中! 另外,为了提高可读性,我是否可以建议将命名空间更改为 LinkedListOfClasses

编辑:关于您对您在 pastebin 中发布的内容的评论:

// Node.h
namespace LinkedListOfClasses {
  class Node {

  public:
      Node();
      Node(int value, Node *next);
      int getData() const;
      Node *getLink() const;
      void setData(int value);
      void setLink(Node *next);
  private:
      int data;
      Node *link;
  };
  typedef Node* NodePtr;
}

上面的代码包含您在评论中提到的方法的声明。关于 Node 类中声明的以下方法缺少定义

int getData() const;
Node *getLink() const;

您希望将它们的定义包含在您的 Node**.cpp** 文件中,而不是 .h 中! Node.cpp 将如下所示:

// Node.cpp
#include "Node.h"

using namespace LinkedListOfClasses;

void head_insert(NodePtr &head, int the_number) {

    NodePtr temp_ptr;
    temp_ptr = new Node(the_number, head);
    head = temp_ptr; 
}

Node::Node(int value, Node *next) {  
}

void Node::setData(int value) {
}

void Node::setLink(Node *next) {
}

int Node::getData() const {
  // getData definition was missing, now it's defined in Node.cpp!
}
Node *Node::getLink() const {
  // getLink definition was missing, now it's defined in Node.cpp!
}

希望我能有所帮助。

【讨论】:

  • 我想我明白你在说什么,但我仍然不确定如何定义“int getData() const;”和“节点 *getLink() const;”在我的 Node.h 中。这是三个文件的粘贴箱:pastebin.com/xAmgqD2N
  • 非常感谢!您的代码和声明/定义的链接都有很大帮助。它现在正在工作。我会尝试再上几节课来掌握它! =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多