0

This is my code:

  • mainheader.h

    void displaygrid(int (* _grid)[10][10] , string msg );
    
  • something.cpp

    #include <string>
    #include "mainheader.h"
    
    void displaygrid(int (* _grid)[10][10], string msg = "" )
    {
       //body goes here
    }
    

Compiler says at line of forward declaration in header:

_grid and string are undeclared identifiers

however since its just prototype, it shouldnt be worried about existence of _grid, right ?

Also, header is included after the string , but it doesnt know about string as well. It looks like its trying to call function instead of forward declaring. What's wrong?

888
  • 3,246
  • 8
  • 41
  • 60
user1849353
  • 87
  • 1
  • 10
  • When posting a question about compiler or linker errors, it is always helpful to include the actual errors. Not edited or shorted down but complete and unedited. – Some programmer dude Feb 05 '13 at 16:33
  • Also, ALWAYS have your default values in the header file (and _NOT_ in the implementation - otherwise, some smart guy will change one, and not the other, and either it won't work as expected, or someone else will think "What the **** does this value come from". Default arguments are added by the compiler when it compiles the function - which typically happens from the header file. – Mats Petersson Feb 05 '13 at 16:36

4 Answers4

4

For string you have to use std::string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

It needs to know about __grid in the header as well as string - forward declare or not. You need to at least place a struct __grid; somewhere above it or the likes so it has some sort of clue of what it is. Same for string...

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
1

You need to #include <string> in your mainheader.h and refer to string as std::string. You also need to include whatever is necessary to get _grid:

//mainheader.h
#include <string>
void displaygrid(int (* _grid)[10][10] , std::string msg );

Related post here.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

You also want the default argument in the header file, not in the implementation, because you want code that calls it to know that msg can be defaulted.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234