我收到"找不到标识符"错误
I get an 'identifier not found' error
这是我第一次尝试创建一个基本列表(我在学校需要这个),我得到一个奇怪的错误。
脚本如下:
#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()
之前。
相关文章:
- 设置 Visual Studio for MPI: 找不到标识符错误
- 从子类访问模板类成员时出现"找不到标识符"错误
- 未声明的标识符错误,但变量定义正确 (?)
- 尝试使用类时获取标识符错误
- 我收到未声明的标识符错误,但我已包含头文件?
- 常量自动/标识符错误
- 为什么我的类对象会导致未申报的标识符错误
- C 未申报的标识符错误
- 在Visual Studio中使用C 类 - 未申报的标识符错误
- 编译器不喜欢我的类C++。获取未声明的标识符错误等
- 奇怪的未知标识符错误
- C 数组未申报的标识符错误
- 定义中未声明的标识符错误 C2065,显然存在
- 找不到标识符错误
- 稍后在程序中使用if-else构造内部声明的变量会导致未声明的标识符错误
- Xcode C++未声明的标识符错误
- 编译时未找到"log10"标识符错误
- 标识符错误之前缺少';' - 无法运行 MPI 示例
- 时间开始未找到标识符错误
- 声明项时出现未声明的标识符错误