I'm trying to create an user defined header file and suffering from an error :(

80 Views Asked by At

When I'm trying to compile my code, this message appears:

undefined reference to `N::my_class::do_something()'
collect2.exe: error: ld returned 1 exit status

What can I do?

my_class.cpp

#include "my_class.h" // header in local directory
#include <iostream> // header in standard library

using namespace N;
using namespace std;

void my_class::do_something()
{
    cout << "Doing something!" << endl;
}
1

There are 1 best solutions below

0
Vlad from Moscow On

This function definition

void my_class::do_something()
{
    cout << "Doing something!" << endl;
}

is placed in the global namespace. In fact it is equivalent to

void ::my_class::do_something()
{
    cout << "Doing something!" << endl;
}

But you need to define the function in the namespace N like for example

void N::my_class::do_something()
{
    cout << "Doing something!" << endl;
}