Неопределенная ссылка на функцию двоичного дерева класса C ++ - PullRequest
0 голосов
/ 09 апреля 2020

Я получаю следующую ошибку:

 c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Chris\AppData\Local\Temp\ccVDdszy.o:bt_test.cpp:(.text+0x4b7): undefined reference to `bt::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2.exe: error: ld returned 1 exit status

при попытке скомпилировать мой bt_test. cpp, чтобы увидеть, работает ли мое двоичное дерево так, как я хочу. Я определил все свои функции для своего класса двоичного дерева в одном и том же заголовочном файле, что, как я видел, вызывает много других проблем, связанных с тем, что они определили свой конструктор в файле, отличном от заголовочного файла, где класс был первым создал.

Я не могу понять, почему мой компоновщик не может ссылаться на мой конструктор, поскольку я также недавно создал класс очереди и реализовал конструктор так же, как я делаю для моего двоичного дерева. Ниже приведены мои следующие файлы bt_test. cpp и my_bt_not_a_template.h. Любая помощь будет благодарна за то, почему эта ошибка генерируется.

bt_test. cpp

#include <string>
#include <set>
#include <iostream>
#include "my_bt_not_a_template.h"

using namespace std;

int main()
{
    bt bt_1("t5");
    string lesse[5] = {"NAND", "NOT", "NOT","NOT","NOT"};
    int i = 0;
    while(!lesse[i].empty())
    {
        bt_1.insert(lesse[i]);
    }
    bt_1.printLevelOrder();

    return 0;
}

my_bt_not_a_template.h

#pragma once
#include <iostream>
#include <string>
#include <set>

using namespace std;

class bt
{
private:
    struct node
    {
        string value;
        node* left;
        node* right;
    };
    node *root;
    void printLevelOrder_DO(node* rooto);

public:
    bt(string val)
    {
        root->value = val;
        root->left = NULL;
        root->right = NULL;
    }
    void insert(string val);
    node *search(string val, node *leaf);
    bool isEmpty() const {return root==NULL;}
    void insert(string val, set<string> inputs);
    node* search(string val);
    void printLevelOrder() {node* root; printLevelOrder_DO(root);}
    void printLevel(node* root, int level);
    int height(node* node);
};
void bt::insert(string val, set<string> inputs)
{
    node* nod = new node;
    node* parent;
    nod->value = val;
    nod->left = NULL;
    nod->right = NULL;
    parent = NULL;

    node* current;
    current = root;
    while(current)
    {
        parent = current;
        if(!(current->left && current->right))
        {
            //If both left and right are NULL
            //enter value into left leaf
            if(inputs.count(current->value) != 0)
            {
                //then is input and there should be no leafs containing anything below it
            }
            else
            {
                nod->left->value = val;
                nod->left->left = NULL;
                nod->left->right = NULL;
            }
        }
        else
        {
            if(current->value == "NOT")
            {
                nod->left->value = val;
                nod->left->left->left = NULL;
                nod->left->left->right = NULL;
            }
            else
            {
                nod->right->value = val;
                nod->right->left = NULL;
                nod->right->right = NULL;   
            }
        }    
    }
}
...other functions defined

РЕДАКТИРОВАТЬ : Я понял, что у меня есть две декларации для вставки.

1 Ответ

0 голосов
/ 09 апреля 2020

bt(string val);

У вас есть лишняя точка с запятой

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...