-2

ヘッダー ファイルにクラスがあり、そのクラスをソース ファイルで使用するたびに、同じエラーが発生します。それがどのクラスまたはどのプロジェクトであるかは関係ありません。

リンク リスト データ構造の先頭に新しいノードを挿入しようとしています。

今、私は非常に単純なヘッダーファイルを持っています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にすべてを記述しても問題はありません。しかし、どのようにクラスを記述しても、常にこのエラーの変種が発生します。

4

1 に答える 1

2

次のクラス メソッドを宣言しているだけです。

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をインクルードするユーザーは、 linkedlistofclasses::Nodeとそのパブリックメンバーの存在を確認できます。このようにして、たとえば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.hNode.cppファイル作成することを提案できますか?

現在の状況では、さらに次のいずれかを呼び出すと、次のことがわかります。

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に変更することをお勧めしますか?

編集: ペーストビンに投稿した内容についてのコメントについて:

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

.hではなく Node**.cpp** ファイルにそれらの定義を含めたい! 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!
}

私が何とか助けてくれることを願っています。

于 2012-10-13T12:17:18.193 に答える