0

i got a problem. I got 3 files, foo.h, foo.cpp and main.cpp.

foo.h looks like:

void goo();

foo.cpp

#include "foo.h"

void goo()
{
    (something)
}

and main.cpp

#include "foo.h"

int main()
{
    goo();
    return 0;
}

This generates error 'goo' was not declared in this scope, but I can't find any clue why is this happening. Is this linker error?

Gogetek
  • 15
  • 6
  • 1
    In foo.h, replace `goo();` with `void goo();`. If you don't supply a return type, it's `int` by default. – djikay Jul 03 '14 at 01:46
  • @djikay Not in C++. In this case it's just (another) call to `goo()` after preprocessing. – Praetorian Jul 03 '14 at 01:47
  • @Praetorian: Indeed, you are correct. g++ will complain but can be forced to allow it: *warning: ISO C++ forbids declaration of 'goo' with no type [-fpermissive]* – djikay Jul 03 '14 at 02:04
  • I have two suggestions. The first is get a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and the second is **read** it. – Captain Obvlious Jul 03 '14 at 02:19
  • when you compile are you writing `g++ main.cpp foo.cpp -o prog`, if not then yes it is a linker error – James Jul 03 '14 at 03:08
  • "not declared in this scope" is clearly not a linker error. – Crowman Jul 03 '14 at 03:09
  • @PaulGriffiths that's right, i'm wrong, linker error would say `undefined reference to 'goo()' `, my bad – James Jul 03 '14 at 03:11
  • Sorry guys, my mistake. I corrected it. @James I did it as you said. Still same error. It's like it does not include foo.h. – Gogetek Jul 03 '14 at 11:09
  • @Gogetek Your current code example (assuming you comment out `(something)`) compiles for me in g++ on windows version - `g++ (rev2, Built by MinGW-builds project) 4.8.1` with `g++ main.cpp foo.cpp -o prog`, there could be a problem with your installation of g++ because i cannot recreate your error. – James Jul 04 '14 at 00:59

1 Answers1

1

You didn't specify the return type of goo(). In C, the compiler assumes it's int goo(). In C++ (your case), the compiler assumes it's a call to goo(), not a function prototype.

humodz
  • 600
  • 3
  • 8
  • That's C, C++ explicitly forbids declarations with no type. Either OPs compiler is old, he's disabled the error, or he's compiling specifying C as the language. – user657267 Jul 03 '14 at 02:05
  • Sorry, i didn't put "void" in foo.h. Now i corrected it. – Gogetek Jul 03 '14 at 10:28