C++ “错误:一个声明中有多种类型”和“错误:预期的初始化程序在“KeyType”之前

发布于 2024-10-05 07:27:40 字数 1687 浏览 7 评论 0原文

当我运行以下代码时...

#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED

#include <string>

typedef std::string KeyType;

class KeyedItem {
    public:
        KeyedItem() {}
        KeyedItem(const KeyType& keyValue) : searchKey(keyValue) {}
        KeyType getKey() const
        { return searchKey;
        }

    private:
        KeyType searchKey; };

#endif // KEYEDITEM_H_INCLUDED

我收到一条错误消息“错误:'KeyType'之前的预期初始化程序”

我首先认为这可能与声明字符串类型有关,所以我将其更改为以下内容以查看是否可以会起作用...

#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED

#include <string>
//typedef std::string KeyType;

class KeyedItem
{
    public:
        KeyedItem() {}
        KeyedItem(const std::string& keyValue) : searchKey(keyValue) {}
        std::string getKey() const
        { return searchKey;
        }

    private:
        std::string searchKey;
};

#endif // KEYEDITEM_H_INCLUDED

但我收到错误“错误:一个声明中有多种类型”我已经查找了这两个错误的错误,但没有发现任何有帮助的信息。我已经仔细检查了课程,以确保在需要的地方有分号,而且我似乎已经全部都有了。

我没有实现文件只是因为我不需要它,但这可能是问题所在吗?

这只是二叉搜索树的一个类。我正在使用 GNU GCC 编译器在 CodeBlocks 中工作。

树节点.h

#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED

#include "KeyedItem.h"

typedef KeyedItem TreeItemType;

class TreeNode
{
    private:
        TreeNode() {}
        TreeNode(const TreeItemType& nodeItem,
                 TreeNode *left = NULL,
                 TreeNode *right = NULL) : item(nodeItem), leftChildPtr(left), rightChildPtr(right) {}

        TreeItemType item;
        TreeNode *leftChildPtr, *rightChildPtr;

        friend class BinarySearchTree;
};

#endif // TREENODE_H_INCLUDED

When I run the following code...

#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED

#include <string>

typedef std::string KeyType;

class KeyedItem {
    public:
        KeyedItem() {}
        KeyedItem(const KeyType& keyValue) : searchKey(keyValue) {}
        KeyType getKey() const
        { return searchKey;
        }

    private:
        KeyType searchKey; };

#endif // KEYEDITEM_H_INCLUDED

I get an error message "error: expected initializer before 'KeyType'"

I thought at first that this could be related to the declaring the string type so I changed it to the following to see if it would work...

#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED

#include <string>
//typedef std::string KeyType;

class KeyedItem
{
    public:
        KeyedItem() {}
        KeyedItem(const std::string& keyValue) : searchKey(keyValue) {}
        std::string getKey() const
        { return searchKey;
        }

    private:
        std::string searchKey;
};

#endif // KEYEDITEM_H_INCLUDED

but I got the error "error: multiple types in one declaration" I have looked for errors for both of these errors and have found nothing that helps. I have gone over the class to make sure I have semi-colons where needed and I seem to have all of them.

I don't have a implementation file simply because I didn't need one, but could that be the problem?

This is just a class for a binary search tree. I am working in CodeBlocks using the GNU GCC compiler.

TreeNode.h

#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED

#include "KeyedItem.h"

typedef KeyedItem TreeItemType;

class TreeNode
{
    private:
        TreeNode() {}
        TreeNode(const TreeItemType& nodeItem,
                 TreeNode *left = NULL,
                 TreeNode *right = NULL) : item(nodeItem), leftChildPtr(left), rightChildPtr(right) {}

        TreeItemType item;
        TreeNode *leftChildPtr, *rightChildPtr;

        friend class BinarySearchTree;
};

#endif // TREENODE_H_INCLUDED

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2024-10-12 07:27:40

您需要使用 g++ 而不是 gcc 进行编译

you need to compile with g++ not gcc

裂开嘴轻声笑有多痛 2024-10-12 07:27:40

解决了...原来我缺少一个包含在我的 main.c 文件中的标头。

Solved... Turns out I was missing a header include in my main.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文