c++中的可视化语法错误

visual syntax error in c++

本文关键字:语法 错误 可视化 c++      更新时间:2023-10-16

i wana在c++中创建了一个bst树,但我在这段代码中有一个语法错误:

#pragma once
#include "BSTNode.h"
using namespace std;
class BST
{
private:
    BSTNode* root;
public:
    BST(void);
    bool insert(int );
    int search(int);
    ~BST(void);
};

CCD_ 3为

#pragma once
#include "BST.h"
class BST;
class BSTNode
{
    friend  class BST;
private:
    int data;
    BSTNode * LeftChild, *RightChild;
public:
    BSTNode(void);
    int getData();
    ~BSTNode(void);
};

我的错误是:

Error   1   error C2143: syntax error : missing ';' before '*'

我认为dont有错误。请帮帮我!

您有一个循环include,包含2个文件,由于#Pragma一次,两个文件都只包含一次,因此首先解析BSTNode并包含BST,但BST不再包含BSTNode(因为它是Pragma一)。

这导致BST不知道BSTNode是什么,解决方案是:

删除include并转发声明类,如下所示:

#pragma once
using namespace std;
class BSTNode;  //Forward declare class so that BST knows BSTNode (move include to .cpp file)
class BST
{
private:
    BSTNode* root;
public:
    BST(void);
    bool insert(int );
    int search(int);
    ~BST(void);
};

主要功能示例:

int main( int argc, const char* argv[] )
{
    printf( "nHello Worldnn" );
}

不带类型的"BSTNode"声明。在使用之前,您需要声明该类