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";
    }
}
1
  • 3
    #include "Menu.hpp" in your Menu.cpp. Commented Aug 25, 2013 at 6:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't realise each class had to include it's own header.
3

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

#include "Menu.hpp"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.