C++ C++;,Xcode。每次我尝试使用类时都会出现相同的错误

C++ C++;,Xcode。每次我尝试使用类时都会出现相同的错误,c++,xcode,class,compiler-errors,undefined,C++,Xcode,Class,Compiler Errors,Undefined,每次我在头文件中有一个类,并且在源文件中使用该类时,我都会得到相同的错误。不管它是哪个类或哪个项目 我试图在链表数据结构的头部插入一个新节点 现在我有一个非常简单的头文件main.h: namespace linkedlistofclasses { class Node { public: Node(); Node(int value, Node *next); //Constructor to in

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

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

现在我有一个非常简单的头文件
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;
}
// 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!
}
如果我在不使用类的情况下运行代码,将所有内容写入源文件
main.cpp
,那么就没有问题。
但无论我如何编写一个类,我总是会得到这个错误的一些变体

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

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
它允许包含main.h的用户查看LinkedListofClass::Node及其public成员的存在。这样,您就可以调用它们,例如,从main()函数调用它们

然而,这在以后会引起一些问题:

**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**
出现这些错误消息是因为不知道程序内部表示中与这些方法名称关联的代码所在的区域。因为你一开始没有定义它,所以找不到它!考虑一下情况:调用不存在代码的函数有什么意义?

我建议您创建一个Node.h和Node.cpp文件,第一个文件带有声明,第二个文件带有定义(),然后从main.c
中包含Node.h

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

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;
}
上面的代码包含您在评论中提到的方法的声明。关于节点类中声明的以下方法中缺少的定义

int getData() const;
Node *getLink() const;
您希望在节点**.cpp**文件中包含它们的定义,而不是.h!Node.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;
}
// 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!
}

希望我能提供一些帮助。

您的
类节点的实现在哪里
?您在哪里实现了类的成员函数?为您的类创建一个单独的.h和.cpp文件。您不想真正使用
main.h
文件我想我理解您的意思,但我仍然不确定如何在Node.h中定义“int getData()const;”和“Node*getLink()const;”。这是一个由三个文件组成的粘贴箱:非常感谢!您的代码和指向声明/定义的链接都有很大帮助。它现在正在工作。我会尝试多上几节课来掌握它的窍门!=)