Sp3ct3R
Sp3ct3R

Reputation: 725

I get an 'identifier not found' error

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.

Upvotes: 3

Views: 13295

Answers (4)

jdawg
jdawg

Reputation: 329

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

Upvotes: 0

Heisenbug
Heisenbug

Reputation: 39174

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.

Upvotes: 3

sashang
sashang

Reputation: 12204

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.

Upvotes: 3

Robᵩ
Robᵩ

Reputation: 168636

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
}

Upvotes: 3

Related Questions