Search code examples
c++codeblocksmingw32

Codeblocks: 'Class' was not declared in this scope


So I am trying to compile a very simple project. For some reasons it doesn't find my class in the .cpp file.

Here is the code: main.cpp:

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

using namespace std;


int main()
{
    Dog myDog;
    return 0;
}

Dog.h:

#ifndef DOG_H
#define DOG_H


class Dog
{
    public:
        Dog();
        virtual ~Dog();
        Dog(const Dog& other);
        Dog& operator=(const Dog& other);
    protected:
    private:
};

#endif // DOG_H

Dog.cpp:

#include "Dog.h"

Dog::Dog()
{
    //std::cout << "I'm alive!";
}

Dog::~Dog()
{
    //dtor
}

Dog::Dog(const Dog& other)
{
    //copy ctor
}

Dog& Dog::operator=(const Dog& rhs)
{
    if (this == &rhs) return *this; // handle self assignment
    //assignment operator
    return *this;
}

So that's pretty basic, still I get the error: 'Dog' was not declared in this scope.

I believe that i need to add this to the build, but I did that already by right clicking the dog.cpp in the projects window, and build settings.

build settings of Dog.cpp

Compiler log:

-------------- Build: Debug in MyProject (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -Iinclude -c C:\Users\tamas\Documents\MyProject\include\Dog.cpp -o obj\Debug\include\Dog.o
mingw32-g++.exe -Wall -fexceptions -g -Iinclude -c C:\Users\tamas\Documents\MyProject\main.cpp -o obj\Debug\main.o
C:\Users\tamas\Documents\MyProject\main.cpp: In function 'int main()':
C:\Users\tamas\Documents\MyProject\main.cpp:9:5: error: 'Dog' was not declared in this scope
     Dog myDog;
     ^
C:\Users\tamas\Documents\MyProject\main.cpp:9:9: error: expected ';' before 'myDog'
     Dog myDog;
         ^

Solution

  • The problem was that the correct Dog.h file was elsewhere where is should have been (in include/ directory).