C++ "error: multiple types in one declaration"和 "error: expected initializer before ' KeyType'”

标签 c++ compiler-errors codeblocks

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

#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

但我收到错误“错误:一个声明中有多种类型”我已经查找了这两个错误的错误,但没有找到任何帮助。我复习了一遍类(class),以确保我在需要的地方有分号,而且我似乎有所有的分号。

我没有实现文件只是因为我不需要一个,但这会是问题所在吗?

这只是二叉搜索树的一个类。我在使用 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

最佳答案

你需要用g++而不是gcc编译

关于C++ "error: multiple types in one declaration"和 "error: expected initializer before ' KeyType'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295352/

相关文章:

c++ - 运算符重载 : memory leaks

c++ - C++ 是否保证 cstdint sizeof 的顺序?

c++ - NULL 函数指针

c - 递增指针时未使用的表达式

compiler-errors - mingw提供错误-C编译器无法创建可执行文件

C++ 继承 - 引用类型的无效初始化

java - 将 ArrayList<String> 数据转换/传输到 String[] 时出现不兼容类型错误

opencv - 制作失败 98% Linking CXX shared module ..\..\lib\cv2.pyd

linux - 管道重定向在 Code::Blocks 中的构建前/构建后步骤失败

c++ - 权限被拒绝是什么意思