首页 > 解决方案 > 我不断收到“缺少类型说明符”错误 C4430

问题描述

如果我在解释我的问题方面做得很糟糕,我提前道歉。我也很抱歉这篇文章的长度。这是我第一次尝试在这个网站上提问,一般来说我对提问有点紧张。我正在开发一个程序,使用我正在学习的大学数据结构课程的链表制作堆栈数据结构,并且在我的程序中遇到了一个错误,导致我的整个程序无法工作。我们使用了我们最近做的一个程序中的几个文件来创建项目。如果它对我使用 Visual Studio 2019 有任何帮助。任何帮助将不胜感激,我感谢任何花时间阅读所有这些内容的人。

这是我的第一个头文件:“SingleLinkedList Head.h”

#include <string>
typedef string Item;     //This is the line which causes the problem.  It keeps saying that there is a
                         //missing type specifier, yet the type is clearly specified as "string"?
class StringNode {
private:
    Item item;          //The error above is causing some of the things in my program which
    StringNode* next;   //use "Item" as the datatype to also give me a C4430 Error

    friend class StringLinkedList;
};

class StringLinkedList {
public:
    StringLinkedList();
    ~StringLinkedList();
    bool vacant() const;     //Checks if the list is empty
    const Item& topDeck() const;          //Displays the front item of the list (C4430 Error)
    void drawCard(const Item& item);      //Adds an item to the front of the list (C4430 Error)
    void discard();     //Removes an item from the front of the list
    void showHand();    //Prints the entire list
private:
    StringNode* head;
};

这是我的第一个实现文件:“SingleLinkedList Imp.cpp”

#include "SingleLinkedList Head.h"
#include <iostream>
using namespace std;

StringLinkedList::StringLinkedList()
    :head(NULL) {}

StringLinkedList::~StringLinkedList() {
    while (!vacant()) discard();
}

void StringLinkedList::discard() {
    StringNode* excess = head;
    head = head->next;
    delete excess;
}

bool StringLinkedList::vacant() const { return head == NULL; }

const Item& StringLinkedList::topDeck() const { return head->item; }    //C4430 Error

void StringLinkedList::drawCard(const Item& i) {        //C4430 Error
    StringNode* draw = new StringNode;
    draw->item = i;
    draw->next = head;
    head = draw;
}

void StringLinkedList::showHand() {
    StringNode* card = head;

    while (card != nullptr) {
        cout << card->item;
        card = card->next;
    }
    cout << endl << endl;
}

这是我的第二个头文件:“LinkedStack Head.h”

#include "SingleLinkedList Head.h"

class LinkedStack
{
public:
    LinkedStack();
    int size() const;
    bool empty() const;
    const Item& top();     //Displays the top of the stack
    void stack(const Item& item);     //Adds an item to the top of the stack
    void unStack();     //Removes an item from the top of the stack
private:
    StringLinkedList S;
    int n;
};

这是我的第二个实现文件:“LinkedStack Imp.cpp”

#include "LinkedStack Head.h"
#include <iostream>
using namespace std;

LinkedStack::LinkedStack()
    : S(), n(0) {}

int LinkedStack::size() const { return n; }

bool LinkedStack::empty() const { return n == 0; }

const Item& LinkedStack::top() { return S.topDeck(); }

void LinkedStack::stack(const Item& i) {
    ++n;
    S.drawCard(i);
}

void LinkedStack::unStack() {
    if (empty())
        cout << "The stack is empty.";
    else
    {
        --n;
        S.discard();
    }
}

这是我的主文件:“LinkedStack Main.cpp”

#include "LinkedStack Head.h"
#include <iostream>
using namespace std;

int main()
{
    LinkedStack B;

    B.stack("Bob");

    if (!B.empty())
        cout << B.top() << endl;
    else
        cout << "The stack is empty.";

    B.stack("Devin");
    B.stack("Claire");
    B.stack("Malachi");
    B.stack("Diana");

    B.unStack();
    B.unStack();
    B.unStack();


    return 0;
}

我不完全知道是什么导致了这种情况发生。我也收到一些奇怪的错误,告诉我将分号放在奇怪的地方(有人要求我在 typedef 行中的“项目”之前放一个分号,这没有多大意义)。我最好的猜测是,在我的程序中的某个地方我忽略了我的一个错误,而程序只需要用一双新的眼睛来检查。

注意:我知道使用“使用命名空间标准;” 在编程方面是一种大罪。我在我的代码中使用了它,因为它在我们在课堂上做的例子中使用过。我将不再在不基于此类示例的程序(即完全由我创建的程序)中使用它。

标签: c++compiler-errors

解决方案


您缺少std“SingleLinkedList Head.h”中的命名空间。使用typedef std::string Item;. 您不断收到“缺少类型说明符”,因为尚未定义“字符串”(因此它不是类型说明符),编译器“认为”您正在编写typedef <sometype> string<sometype>错过了。

注意:using namespace std;这是编译器将接受的另一种可能的法律修复,但强烈不建议在头文件中使用。


推荐阅读