链表显示;缺少..但事实并非如此

Linked List says ; is missing...but its not

本文关键字:事实 并非如此 缺少 显示 链表      更新时间:2023-10-16

我正在尝试制作一个简单的链表。一切都很顺利,然后突然发生了一场错误大屠杀。我不知道我改变了什么来破坏我的代码。这是我的文件,得到了一些错误:

#pragma once
#include <string>
#include "Node.h"
class LinkedList
{
private:
    Node *head;
public:
    LinkedList();
    ~LinkedList();
    void AddNode(int);
    string GetList();  //missing ';' before identifier 'GetList'
    bool Contains(int);
    void Remove(int);
};

它声称我在string GetList();上方的行上缺少一个分号,或者看起来。。。但显然我不是。确切的错误是:

Error   1   error C2146: syntax error : missing ';' before identifier 'GetList' c:...linkedlist.h 15  1   ProjectName

这条线上的另一个错误是:

Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:...linkedlist.h 15  1   ProjectName

但它被标识为字符串返回类型。

在LinkedList.cpp中,这是GetList()方法:

string LinkedList::GetList(){
    string list;
    Node *currentNode = head;
    while (currentNode->next_node){
        currentNode = currentNode->next_node;
        list += currentNode->get_value() + " ";
    }
    return list;
}

这一切看起来都很好,但在方法标题中,我得到了以下2个错误:

Error   4   error C2556: 'std::string LinkedList::GetList(void)' : overloaded function differs only by return type from 'int LinkedList::GetList(void)' c:...linkedlist.cpp   28  1   ProjectName

错误5错误C2371:"LinkedList::GetList":重新定义;不同的基本类型c:。。。\linkedlist.cpp 28 1项目名称

我甚至创建了一个新项目,复制并粘贴回我所有的文件,但没有效果。我之前已经在这个程序中成功地运行了GetList()。

有人知道这里到底发生了什么吗?我的IDE在骗我!(Visual Studio社区2013更新4)

您在LinkedList.cpp中的某个位置有using namespace std;,但在LinkedList.h中没有。这就是为什么在类定义中,当您编写string时,它不知道您指的是std::string

我建议停止使用using namespace std;以避免此类问题。

使用std::string,而不仅仅是string