Dawson
Dawson

Reputation: 573

Error when using namespaces - class not declared

I'm new to C++ and using namespaces and I can't see what I'm doing wrong here. When I compile the code below, I get the error:

error: 'Menu' has not been declared

Here is my header file Menu.hpp

#ifndef MENU_H //"Header guard"
#define MENU_H

namespace View
{
class Menu
    {
    void startMenu();
    };
}
#endif

and my Menu.cpp:

#include "stdio.h"
using namespace std;

namespace View
{
 void Menu::startMenu()
    {
    cout << "This is a menu";
    }
}

Upvotes: 1

Views: 1920

Answers (2)

Alok Save
Alok Save

Reputation: 206526

You missed including the header file which defines the class.

Menu.cpp:

#include "Menu.hpp"

Each translation unit is compiled separately by the compiler and if you do not include the header file in Menu.cpp, there is no way for the compiler to know what Menu is.

Upvotes: 4

Saksham
Saksham

Reputation: 9380

You will have to include header Menu.hpp in your cpp file Menu.cpp like

#include "Menu.hpp"

Upvotes: 3

Related Questions