identifier not found error on function call

在C++工程中,自定义一个方法 void fgetsDemo(),在main 方法中调用,源代码如下:

#include "stdafx.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



int _tmain(int argc, _TCHAR* argv[])

{

    /*Example for fgets*/

    fgetsDemo();



    return 0;

}



void fgetsDemo()

{

FILE *stream;

   char line[100];



   if( fopen_s(&stream, "c:\crt_fgets.txt", "r" ) == 0 )

   {

      if( fgets( line, 100, stream ) == NULL)

         printf( "fgets error\n" );

      else

         printf( "%s", line);

      fclose( stream );

   }

}
View Code

 

编译,出现  error C3861: 'fgetsDemo': identifier not found,即标示符未找到的错误。

identifier not found error on function call

在C++ 中,要调用变量或方法,需要提前声明。

编译器会从上到下的读取你的源代码,如果自定义的方法没有提前声明,那么编译器读到这个方法时,就不知道它是何物,就会出现 “标示符未找到”的错误。

 

解决方案1:在 main 方法前声明这个自定义的方法,

void fgetsDemo();

int _tmain(int argc, _TCHAR* argv[])

{

    /*Example for fgets*/

    fgetsDemo();



    return 0;

}
View Code

 

解决方案2:将整个方法体 移到 main 方法之前。

 

参考链接:

http://stackoverflow.com/questions/8329103/identifier-not-found-error-on-function-call

你可能感兴趣的:(function)