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
  • 39,941
  • 12
  • 79
  • 152
blacklune
  • 21
  • 2

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
  • 39,941
  • 12
  • 79
  • 152