Including header files in C++ (class definition and method implementation)

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.

main.cpp:

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

using namespace std;

int main()
{
    Message *t = new (Message);

    t->display();

    return 0;
}

Message.h:

#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED

class Message {
public:
    void display();
};

#endif // MESSAGE_H_INCLUDED

Message.cpp:

#include "Message.h"

void Message::display() {
    cout << "Hello!";
}

I don't understand why I keep getting the following error

undefined reference to 'Message::display()'

Upvotes: 3

Views: 127

Answers (1)

user4386938
user4386938

Reputation:

Compile this with the command g++ -std=c++11 Message.cpp main.cpp

Upvotes: 2

Related Questions