User2012384
User2012384

Reputation: 4919

c++ identifier not found when compiling source

Here is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int userInput = -9999;
    userInput = ReadNumber();
    WriteAnswer(userInput);
    system("pause");
    return 0;
};

int ReadNumber ()
{
    int liInput = -9999;
    cin >> liInput;
    return liInput;
};

 void WriteAnswer(int data)
{
    cout << data << endl;
};

When I try to compile, it saids :

1>error C3861: 'ReadNumber': identifier not found

1>error C3861: 'WriteAnswer': identifier not found

Why did the above error occurs? and how to solve this problem?

Thanks

Upvotes: 0

Views: 1844

Answers (4)

Alex
Alex

Reputation: 10126

You have to write a function prototype or function itself before first call of the function.

In your code compiler see call of the ReadNumber() but it doesn't know what is that function.

Upvotes: 0

LihO
LihO

Reputation: 42103

In your code you try to call ReadNumber function, that hasn't been declared yet. The compiler doesn't know anything about this function:

int _tmain(int argc, _TCHAR* argv[])
{
    ...
    ReadNumber();   // call to function ReadNumber, but what is ReadNumber ??? 
}

// definition of ReadNumber:
int ReadNumber ()
{
    ...
}

You should declare it first:

// declaration:
int ReadNumber();

int _tmain(int argc, _TCHAR* argv[])
{
    ...
    ReadNumber();   // call ReadNumber that takes no arguments and returns int
}

// definition of ReadNumber:
int ReadNumber ()
{
    ...
}

Upvotes: 1

Drew Dormann
Drew Dormann

Reputation: 63839

C++ sources are compiled from beginning to end.

When the compiler has gotten this far:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int userInput = -9999;
    userInput = ReadNumber();   // <-- What is this?

It's true -- there is no evidence of ReadNumber existing.

Declare the existence of your functions before they are used.

int ReadNumber ();
void WriteAnswer(int data);

Upvotes: 6

user1944441
user1944441

Reputation:

You forgot to type function prototypes.

int ReadNumber ( void );
void WriteAnswer(int );

Put them in your code before calling the functions.

Upvotes: 2

Related Questions