简体   繁体   English

找不到成员函数的标识符

[英]Identifier not found for member function

For some reason, I keep getting an "identifier not found" error whenever I try to use appendNode(). 出于某种原因,每当尝试使用appendNode()时,我都会不断收到“找不到标识符”错误。 In addition, I keep getting an error that tells me I'm missing ";" 此外,我不断收到一条错误消息,告诉我我缺少“;” before "using" which doesn't make any sense. 在“使用”之前没有任何意义。

header file 头文件

#ifndef INTEGERLIST_H
#define INTEGERLIST_H
#include <iostream>
using namespace std;

class IntegerList
{
private:
    struct ListNode
    {
        int value;
        struct ListNode *next;
    };

    ListNode *head;

public:
    IntegerList()
    {
        head = NULL;
    }

    ~IntegerList();

    void appendNode(int);
    void insertNode(int);
    void deleteNode(int);
    void printList() const;
}
#endif

Implementation file 实施文件

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


void IntegerList::appendNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;

    //new allocation
    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;

    if (!head)
        head = newNode;

    else
    {
        nodePtr = head;

        while(nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}

void IntegerList::insertNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode = NULL;

    newNode = new ListNode;
    newNode->value = num;

    if(!head)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else
    {
        nodePtr = head;

        previousNode = NULL;

        while(nodePtr != NULL && nodePtr->value < num)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if(previousNode == NULL)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else
        {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}

void IntegerList::deleteNode(int num)
{
    ListNode *nodePtr;
    ListNode *previousNode;

    if(!head)
        return;

    if(head->value == num)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }

    else
    {
        nodePtr = head;

        while(nodePtr != NULL && nodePtr->value != num)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if(nodePtr)
        {
            previousNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

void IntegerList::printList() const
{
    ListNode *nodePtr;

    nodePtr = head;

    while(nodePtr)
    {
        cout << nodePtr->value << endl;

        nodePtr = nodePtr->next;
    }
}

#include does a straight copy from the other file, and since this is done by the preprocessor, the compiler doesn't really know this happened and gives you the error as if it was just one large file. #include从另一个文件中直接复制一个文件,由于这是由预处理程序完成的,因此编译器实际上并不知道发生了什么,并且会给您错误,就好像它只是一个大文件一样。 So in your code you have (simplified below): 因此,在您的代码中(以下简化):

// header.h
class A{
   // ...
}

and

// code.cpp
#include "header.h"
using namespace std;

So this is converted to: 因此,将其转换为:

// code.cpp
class A{
   // ...
}
using namespace std;

This makes the error clearer. 这样可以使错误更清晰。 It is expecting a ; 期待一个; before using since class declarations must end in a ; using之前using因为类声明必须以;结尾; .

As mentioned in the comments, you're missing the ; 如评论中所述,您缺少; at the end of your class declaration. 在类声明的末尾。 You are getting the error in your implementation file because the header is included (copied in its entirety) right before your using statement. 您在实现文件中遇到错误,因为在您的using语句之前包含了标头(完整复制了标头)。 Makes perfect sense once you know what you're looking at. 一旦知道了要看的内容,便说得很对。

As others have already said, you're missing a ; 正如其他人已经说过的那样,您缺少了; from the end of your class declaration in your header. 从标头中类声明的末尾开始。

However, the far more serious problem with the code is the line using namespace std; 但是,代码更严重的问题是using namespace std;的行using namespace std; in the header. 在标题中。 This is bad practice everywhere but particularly in header files. 到处都是不好的做法尤其是在头文件中。 The only line that even uses anything from std is the single line in printList 甚至使用std中的任何内容的唯一行是printList的单行

    cout << nodePtr->value << endl;

Remove both of the using namespace std lines and replace the line in printList with this: 删除两条using namespace std行,并用以下命令替换printList的行:

    std::cout << nodePtr->value << std::endl;

You and everyone else who might later use your code will thank you for it. 您和以后可能使用您的代码的其他所有人都会感谢您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM