1

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";
    }
}
Dawson
  • 573
  • 6
  • 24

2 Answers2

4

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.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

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

#include "Menu.hpp"
Saksham
  • 9,037
  • 7
  • 45
  • 73