Error: expected constructor, destructor, or type conversion before '*' token|
Error: expected constructor, destructor, or type conversion before '*' token|
我正在尝试实现二叉搜索树。代码不完整,但我还是构建了它以查看可能出现的错误。
这是它的代码:
BST.h
class BST {
public:
struct node
{
//All nodes must be able to point to left and right
int key; //All nodes can hold a key value
node* left; //All nodes have a left pointer
node* right;//All nodes have a right pointer
};
node* root; //References the very top of the tree
public:
BST(); //Constructor that initializes each time instance is called
node* CreateLeaf(int key);
};
BST.cpp
#include<iostream>
#include<cstdlib>
#include "BST.h"
using namespace std;
BST::BST()
{
root = NULL;
}
node* BST::CreateLeaf(int key) //Causing errors
{
node* n = new node;
n->key = key;
n->left = NULL;
n->right = NULL;
return n;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "BST.cpp"
using namespace std;
int main()
{
return 0;
}
这给出了错误:
错误:在“*”标记
之前需要构造函数、析构函数或类型转换
在 BST.cpp 文件中,如果我将 CreateLeaf() 函数声明为:
typedef node* BST::CreateLeaf(int key)
错误变为:
错误:'*' 标记
之前的预期初始值设定项
现在,根据常识,因为我在 class 之外声明 CreateLeaf 函数,所以我这样做:
BST::node* BST::CreateLeaf(int key)
现在错误变成:
错误:在函数 BST 中:`BST::BST()'
的多重定义
我在 Windows 10 上使用 CodeBlocks IDE。
编辑:
我删除了 .cpp 文件并在头文件中声明了所有函数(并在 main 函数中包含了头文件)。现在正在编译。但是,如果有人能让我知道错误发生的原因,那就太好了。
在 class 声明之外,您需要将范围前缀为 node
:
BST::node* BST::CreateLeaf(int key) {
// ^^^^^ ...
}
此外,您不应包含 .cpp
文件。包含 header,并分别编译和 link .cpp
文件。
在声明中
node* BST::CreateLeaf(int key)
…编译器不知道名称 node
,因为它是在 BST
class 中定义的,并且在 class 之外使用。
一个简单的修复方法是使用更新的尾随 return 类型语法:
auto BST::CreateLeaf(int key)
-> node*
这里编译器在遇到node
.
的地方知道声明属于BST
class
或者您可以限定名称,
BST::node* BST::CreateLeaf(int key)
…但这很快就会变得丑陋,尤其是模板代码。
在其他新闻中,
#include "BST.cpp"
… 在文件 main.cpp
中是不好的做法。一个实际原因是,在 IDE 项目中,这可能会导致该代码被编译两次:一次编译 BST.cpp
,另一次编译包含在 main.cpp
.[= 中的相同代码。 23=]
而是单独编译 BST.cpp
。
或者,将其设计为头文件模块(主要是将函数声明为inline
)。
我正在尝试实现二叉搜索树。代码不完整,但我还是构建了它以查看可能出现的错误。 这是它的代码:
BST.hclass BST {
public:
struct node
{
//All nodes must be able to point to left and right
int key; //All nodes can hold a key value
node* left; //All nodes have a left pointer
node* right;//All nodes have a right pointer
};
node* root; //References the very top of the tree
public:
BST(); //Constructor that initializes each time instance is called
node* CreateLeaf(int key);
};
BST.cpp
#include<iostream>
#include<cstdlib>
#include "BST.h"
using namespace std;
BST::BST()
{
root = NULL;
}
node* BST::CreateLeaf(int key) //Causing errors
{
node* n = new node;
n->key = key;
n->left = NULL;
n->right = NULL;
return n;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "BST.cpp"
using namespace std;
int main()
{
return 0;
}
这给出了错误: 错误:在“*”标记
之前需要构造函数、析构函数或类型转换在 BST.cpp 文件中,如果我将 CreateLeaf() 函数声明为:
typedef node* BST::CreateLeaf(int key)
错误变为: 错误:'*' 标记
之前的预期初始值设定项现在,根据常识,因为我在 class 之外声明 CreateLeaf 函数,所以我这样做:
BST::node* BST::CreateLeaf(int key)
现在错误变成: 错误:在函数 BST 中:`BST::BST()'
的多重定义我在 Windows 10 上使用 CodeBlocks IDE。
编辑: 我删除了 .cpp 文件并在头文件中声明了所有函数(并在 main 函数中包含了头文件)。现在正在编译。但是,如果有人能让我知道错误发生的原因,那就太好了。
在 class 声明之外,您需要将范围前缀为 node
:
BST::node* BST::CreateLeaf(int key) {
// ^^^^^ ...
}
此外,您不应包含 .cpp
文件。包含 header,并分别编译和 link .cpp
文件。
在声明中
node* BST::CreateLeaf(int key)
…编译器不知道名称 node
,因为它是在 BST
class 中定义的,并且在 class 之外使用。
一个简单的修复方法是使用更新的尾随 return 类型语法:
auto BST::CreateLeaf(int key)
-> node*
这里编译器在遇到node
.
BST
class
或者您可以限定名称,
BST::node* BST::CreateLeaf(int key)
…但这很快就会变得丑陋,尤其是模板代码。
在其他新闻中,
#include "BST.cpp"
… 在文件 main.cpp
中是不好的做法。一个实际原因是,在 IDE 项目中,这可能会导致该代码被编译两次:一次编译 BST.cpp
,另一次编译包含在 main.cpp
.[= 中的相同代码。 23=]
而是单独编译 BST.cpp
。
或者,将其设计为头文件模块(主要是将函数声明为inline
)。