-1

I'm fairly new to C++, and I'm starting out with this in a terminal application:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    if ( argc < 1 )
    {
        printHelp();
        return 1;
    }
    return 0;
}

void printHelp()
{
    cout << "Usage:";
    cout << "vmftomap [filename]";
}

However, I get the error "'printHelp' identifier not found" in _tmain. Since the function is declared directly beneath main, I'm assuming this is a namespace issue? I have read up on namespaces but I don't know what would apply in this case, since I haven't actually explicitly defined one for printHelp().

NoodleCollie
  • 855
  • 7
  • 24
  • Consider this post: [SO](http://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c) – bash.d Feb 08 '13 at 12:41

4 Answers4

1

You have to declare your function before you invoke it. It is not necessary to define it, but the compiler must at least know about its existence in the moment it has to resolve a function call, which means it must have met a declaration for it during the processing of your translation unit (i.e. .cpp file):

#include "stdafx.h"
#include <iostream>

using namespace std;

// Declaration
void printHelp();

int _tmain(int argc, _TCHAR* argv[])
{
    if ( argc < 1 )
    {
        printHelp();
        return 1;
    }
    return 0;
}

// Definition
void printHelp()
{
    cout << "Usage:";
    cout << "vmftomap [filename]";
}

Of course, you could directly define the printHelp() function before main() instead, thus making it visible to the compiler at the point the function call is made:

#include "stdafx.h"
#include <iostream>

using namespace std;

// Definition
void printHelp()
{
    cout << "Usage:";
    cout << "vmftomap [filename]";
}

int _tmain(int argc, _TCHAR* argv[])
{
    if ( argc < 1 )
    {
        printHelp();
        return 1;
    }
    return 0;
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Ah, makes sense. I'm used to programming where functions can come anywhere in the file and the compiler will find them, so I didn't think about this possibility. Thanks! – NoodleCollie Feb 08 '13 at 13:17
0

In C++, files are parsed top-to-bottom. With a few exceptions, identifiers have to be declared before they are used. This means you must move the definition of printHelp() before _tmain(), or add a forward declaration above _tmain():

void printHelp();
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

The function has to be defined before using it.

Move printHelp above _tmain.

parkydr
  • 7,596
  • 3
  • 32
  • 42
0

When you call a function in c++, before the call, you must either:

  • have a prototype of the function
  • have the definition of the whole function

in your case, you have neither.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169