Question

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?

Was it helpful?

Solution

For string you have to use std::string.

OTHER TIPS

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...

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top