‘Foo’ was not declared in this scope

From Cppsyntax

Jump to: navigation, search

This error message means that the compiler couldn't figure out what 'Foo' was. Is it a class name? A local variable? A global variable, what? The compiler just couldn't figure it out.

One way that you can get this message is by forgetting to #include a declaration in your source. (While you can certainly declare Foo in a file that defines the Blort class, it is much more common for you to include foo.h in blort.cc.)

WRONG

blort.cc:

 class Blort {
   Blort::Blort() {
   {
     Foo *aFoo = new Foo();
     // stuff
   }
 };

RIGHT

blort.cc:

 #include "foo.h"
 class Blort {
   Blort::Blort() {
   {
     Foo *aFoo = new Foo();
     // stuff
   }
 };

foo.h

 class Foo {
 public:
   Foo();
 };

You can also define Foo in blort.cc:

RIGHT

blort.cc:

 class Foo;
 class Blort {
   Blort::Blort() {
   {
     Foo *aFoo = new Foo();
     // stuff
   }
 };
Personal tools