0

I'm teaching myself how to use header files with .cpp files. I have been working on this issue for awhile and couldn't figure it out. Would anyone help me to address two errors? Thank you :)

driver.cpp

#include <cstdlib>

using namespace std;
#include "F.h"
#include "G.h"


int main()
{

    FMMoore::hello();
    GMMoore::hello();

    system("pause");
    return 0;
}

F.cpp

#include <iostream>
using std::cout; 
#include "F.h"

namespace FMMoore
{
    void hello()
    {
        cout << "hello from f.\n";
    }
}

F.h

#ifndef F_H
#define F_H

namespace FMMoore
{
    class FClass
    {
    public:
        void hello();
    };
}

#endif // F_H

G.cpp

#include <iostream>
using std::cout; 
#include "G.h"

namespace GMMoore
{
    void hello()
    {
        cout << "hello from g.\n";
    }
}

G.h

#ifndef G_H
#define G_H

namespace GMMoore
{
    class GClass
    {
    public: 
        void hello();
    };
}

#endif // G_H

The errors are 'hello' is not a member of 'FMMoore' and 'GMMoore' has not been declared.

Also I have been checking spelling typo and other things. I don't know why it hasn't declared.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
blacklune
  • 21
  • 2
  • 3
    You need a C++ book. – n. m. could be an AI Oct 27 '16 at 14:56
  • Yes I have a book but not really a helpful book – blacklune Oct 27 '16 at 14:57
  • You missed to specify the namespace scope. – πάντα ῥεῖ Oct 27 '16 at 14:57
  • You should really read a tutorial. This has no structure what you are doing. Your `hello` is each time inside a class. So you need to learn how to define functions of a class. Then you have non-static methods so you also need instantiations of the object. ... A tutorial/book would help a lot. – Hayt Oct 27 '16 at 14:58
  • 1
    @blacklune: Try one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Fred Larson Oct 27 '16 at 14:58
  • Which book is that? Perhaps you need a better one. – n. m. could be an AI Oct 27 '16 at 15:05
  • GMMoore::hello has not been declared. Look at G.h to confirm this. You will only find GMMoore::GClass::hello, which is something different. I'd recommend not messing with "class" until you get to that topic. – Kenny Ostrom Oct 27 '16 at 15:24
  • Despite your question title, this has nothing to do with header files. You really need to get your hands on some instructional material. Here is [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329) for your convenience. – IInspectable Oct 27 '16 at 17:38

1 Answers1

0

In F.h, hello is declared as a member function of the FClass, which is defined under the FMMoore namespace:

#ifndef F_H
#define F_H

namespace FMMoore
{
    class FClass
    {
    public:
        void hello();
    };
}

#endif // F_H

However, in F.cpp, you implemented a function hello under FMMoore namespace, but that function is not a member function of FClass:

namespace FMMoore
{
    void hello()
    {
        cout << "hello from f.\n";
    }
}

Similarly for G.h/G.cpp.

Based on your code in driver.cpp:

FMMoore::hello();
GMMoore::hello();

sounds like you want a free function hello (not a class member function), so you should fix the headers, e.g. for F.h:

#ifndef F_H
#define F_H

namespace FMMoore
{
    // hello is a free function under the FMMoore namespace
    void hello();
}

#endif // F_H
Mr.C64
  • 41,637
  • 14
  • 86
  • 162