我收到“找不到标识符”错误

发布于 2024-12-06 05:00:54 字数 745 浏览 3 评论 0原文

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

这是脚本:

#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':找不到标识符

我不知道为什么我得到这个...抱歉,如果这是一个愚蠢的问题,但我对此很陌生。感谢您的理解。

This is my first attempt to create a basic list (i need this at school) and i get a strange error.

This is the script:

#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;
    }
}

The exact error is:
error C3861: 'addToList': identifier not found

I dont know why I get this... sorry if it is a stupid question but i am very new at this. Thanks for understanding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就像说晚安 2024-12-13 05:00:54

您需要在实现之前提出一个前向声明才能使用该方法。将其放在 main 之前:

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

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

you need to put a forward declaration to use a method before it's implementation. Put this before main :

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

In C/C++ a method should be used only after it's declaration. To allow recursive call between different methods you can use forward declarations in order to allow the use of a function/method that will be forward implemented.

白龙吟 2024-12-13 05:00:54

标识符必须在使用前声明。将 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
}

Identifiers must be declared before they are used. Move your declaration and definition of addToList earlier in the text file.

Thus:

#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
}
情深如许 2024-12-13 05:00:54

尝试在 main 上方声明 addToList:

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

对于 showList 类似。编译器在使用函数之前需要查看函数的声明。

Try declaring addToList above main:

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

Similarly for showList. The compiler needs to see a declaration of the function before it can use it.

仙女 2024-12-13 05:00:54

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

Try placing the declarations of showList() and addToList() before main().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文