views:

46

answers:

2

Hi All

Is the following approach correct? Well i get a compilation error.

a.hpp is

#include <iostream>

class a
{

public:

void classa_f();


};

a.cpp is

#include "a.hpp"


void a::classa_f()
{

   std::cout<< "a::classa_f\n";


}

main.cpp

#include <iostream>
namespace myname {
#include "a.hpp"
}

int main ()

{

   myname::a obj;
   obj.classa_f();

  return 0;

}

I get the following error

g++ main.cpp a.o /tmp/ccOOf5s7.o: In function main': main.cpp:(.text+0x11): undefined reference tomyname::a::classa_f()' collect2: ld returned 1 exit status

Well my question is, is it possible to have just the includes under the namespace but not the actual implementation, because I can see that compiler is searching the namespace for he definition of the function.which is actually not there.

A: 

In the implementation, you must

namespace myname
{
    void a::classa_f()
    {
        std::cout<< "a::classa_f\n";
    }
}

and please remove #include <iostream> from the hpp file, it gets imported into the namespace too.

Alexandre C.
+2  A: 
namespace myname {
    #include "a.hpp"
}

Declares a class method myname::a::classa_f , which obviously doesn't exist in your program. It's not valid.

Billy ONeal