melkadri melkadri - 26 days ago 7
C++ Question

Undefined reference to "Class::method" C++?

I'm not sure if this problem is with my IDE, but I tried both Code::Blocks and Visual Studio with the project, so I don't think it is.

My main file:

#include <iostream>
#include "Hello.h"

using namespace std;

int main()
{
Hello firstMan;

firstMan.greeting ();
firstMan.goodbye ();
firstMan.goodbye ();
}


My Hello.h:

#ifndef HELLO_H_INCLUDED
#define HELLO_H_INCLUDED

class Hello
{
public:
void greeting ();
void goodbye ();
};

#endif // HELLO_H_INCLUDED


And my class file Hi.cpp:

#include <iostream>
#include "Hello.h"

using namespace std;

void Hello::greeting ()
{
cout << "Hello" << endl;
}

void Hello::goodbye ()
{
cout << "Bye" << endl;
}


I've just started C++, so my apologies if this is a simple error or just a typo.

Answer

The code looks fine.

It seems that you did not add Hi.cpp to your IDE project.

It's strange that you didn't call it Hello.cpp - do you have a file of that name (perhaps empty) that you're building instead?