我收到"找不到标识符"错误

I get an 'identifier not found' error

本文关键字:标识符 错误 找不到      更新时间:2023-10-16

这是我第一次尝试创建一个基本列表(我在学校需要这个),我得到一个奇怪的错误。

脚本如下:

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
    int info;
    nod *leg;
};
int n, info;
nod *v;
void main()
{
    ....
    addToList(v, info); //I get the error here
    showList(v); //and here
}
void addToList(nod*& v, int info)
{
    nod *c = new nod;
    c->info=info;
    c->leg=v;
    v=c;
}
void showList(nod* v)
{
    nod *c = v;
    while(c)
    {
        cout<<c->info<<" ";
        c=c->leg;
    }
}

确切的错误是:错误C3861: 'addToList':标识符未找到

我不知道为什么我得到这个…抱歉,如果这是一个愚蠢的问题,但我是很新的。

您需要在方法实现之前提出一个前向声明来使用它。把它放在main:

前面
void addToList(nod*& v, int info);

在C/c++中,方法只能在声明之后使用。为了允许不同方法之间的递归调用,您可以使用前向声明,以便允许使用将被前向实现的函数/方法。

标识符必须在使用之前声明。将addToList的声明和定义移到文本文件的前面。

:

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
    int info;
    nod *leg;
};
int n, info;
nod *v;
void addToList(nod*& v, int info)
{
    nod *c = new nod;
    c->info=info;
    c->leg=v;
    v=c;
}
void showList(nod* v)
{
    nod *c = v;
    while(c)
    {
        cout<<c->info<<" ";
        c=c->leg;
    }
}

void main()
{
    ....
    addToList(v, info); //No more error here
    showList(v); //and here
}

尝试在main上面声明addToList:

void addToList(nod*& v, int info);

同样适用于showList。编译器在使用函数之前需要看到函数的声明

尝试将showList()addToList()的声明放在main()之前。