尝试创建我的类C ++的新对象时链接错误

时间:2015-07-19 04:14:57

标签: c++ linker-errors

我试图通过创建SingleLinkedList的对象SingleLinkedList m1;来测试我的程序。如果我不在这个对象上创建和操作它就可以很好地编译。这是我的主要内容:

int main()
{
    SingleLinkedList m1;
    displayMenu();
    int user_menu;
    cin >> user_menu;   //get input from user which pocket they want to go to 
    while (user_menu != 4)  
    {
        displayChoices();
        int user_input; //
        cin >> user_input;
        if (user_input == 1) {
            cout << "You selected the Pocket Of Magic!" << endl;
            cout << "What do you choose to do?" << endl;
            //string user_choice;
            //cin >> user_choice;
            //SingleLinkedList m1;
            m1.operationMagic();    
        }
        else if (user_input == 2) {
            cout << "You selected the Pocket Of Potions!" << endl;
            cout << "What do you choose to do?" << endl;
        }

    user_input = 4;
    }
    displayMenu();

但是,当我尝试链接时,会收到此错误:

    Undefined symbols for architecture x86_64:
  "SingleLinkedList::SingleLinkedList()", referenced from:
      _main in pocketmaindriver-df9a60.o
  "SingleLinkedList::~SingleLinkedList()", referenced from:
      _main in pocketmaindriver-df9a60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是什么原因?我尝试包含.cpp文件,但是没有用。这是我要比较的.h文件:

#ifndef POCKET_OF_MAGIC_
#define POCKET_OF_MAGIC_

//using namespace std;


class SingleNode {
private:
    std::string value;
    SingleNode *nextNode;
public:
    SingleNode();
    SingleNode(std::string s); 
    std::string getValue(); 
    SingleNode* getNextNode(); 
    void setNextNode(SingleNode *newNode); 
};


class SingleLinkedList {
private:
    SingleNode *head;
    SingleNode *tail;
    int totalNodes;
    int itemCount;
public:
    SingleLinkedList();
    ~SingleLinkedList();
    void traverseAndPrint();
    void addNode(std::string data);
    bool deleteNode(std::string data);
    void operationMagic();

}; //end pocket of magic

#endif //end pocket of magic

1 个答案:

答案 0 :(得分:0)

您没有告诉编译器您的类的.cpp文件。您需要执行g++ PocketOfMagic.cpp pocketmaindriver.cpp -o my_program

之类的操作