首页 > 解决方案 > 错误 LNK2019 未解析的外部符号 _main

问题描述

我在编译时遇到了代码中的错误,我对如何继续感到有些困惑。我认为我没有使用最新版本的 Visual Studio。我对 C++ 并不完全陌生,也不是经验丰富的人——但我确信我的函数在正确的位置并且int main()很好。所以我只是有点困惑。如果有人能给我第二双眼睛,并解释我的错误/错误行为,以及我如何才能做得更好,我将不胜感激!

#include <iostream>
#include <queue>
#include <fstream>
#include "binaryTreeType.h"
#include "binarySearchTree.h"
using namespace std;

struct node {

    int data;
    node* left;
    node* right;
};

node* newNode(int item) {

    node* temp = new node;
    temp->data = item;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

//Inorder traversal of tree
void inorder(node* root) {

    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

//calculate height of tree
int height(node* root) {

    if (root == NULL)
        return 0;

    else
    {
        int l = height(root->left);
        int r = height(root->right);
        return max(l, r) + 1;
    }
}

node* binarySearchTree(node* head, int data) {

    if (head == NULL) return newNode(data);

    if (data < head->data)

        head->left = binarySearchTree(head->left, data);

    else if (data > head->data)

        head->right = binarySearchTree(head->right, data);

    return head;

}

//caluclate single parents of Binary Search Tree
int singleParent(node* root) {

    queue<node*> qp;
    int ans = 0;

    if (root == NULL) {

        return 0;
    }

    qp.push(root);

    while (!qp.empty()) {

        node* f = qp.front();

        qp.pop();

        if ((f->left != NULL && f->right == NULL) || (f->right != NULL && f->left == NULL)) {

            ans++;

        }

        if (f->left != NULL) {

            qp.push(f->left);

        }

        if (f->right != NULL) {

            qp.push(f->right);

        }

    }

    return ans;
}

int main() {

    node* root = NULL;
    cout << "Please enter elements ending with -1:" << endl;
    int x;
    cin >> x;

    while (x != -1) {

        root = binarySearchTree(root, x);

        cin >> x;
    }


    //display
    cout << "The tree elements within Inorder are: " << inorder(root) << endl;
    cout << "Tree height is: " << height(root) << endl;
    cout << "Number of single parents: " << singleParent(root) << endl;

    return 0;
}

标签: c++visual-studiodata-structures

解决方案


推荐阅读