C++ constructor undefined reference

I'm trying to declare an object from a class in another file. I have added the #include "transfer.h" into my metadata.cpp file, but I'm getting the following error:

 metadata.o: In function `importMetadata':
 metadata.cpp:(.text+0x81): undefined reference to A::B::C::Transfer::Transfer()'
metadata.cpp:(.text+0x81): undefined reference to A::B::C::Transfer::~Transfer()'

And Transfer is defined with in transfer.h

namespace A{
  namespace B{
    namespace C{
       class Transfer {
            public:
               Transfer();
               ~Transfer();
               int copydata();
            ... more code goes here.
        };
    }
   }
}

The file transfer.cpp looks like this:

Transfer::Transfer(){
}
Transfer::~Transfer(){
}

I'm also doing an using namespace A::B::C; on the header of the metadata.cpp file. Could someone please help me on that? In the function int importMetadata() in metadata.cpp I'm declaring Transfer transfer; so in metadata.cpp I'm doing 'transfer.copydata();`

Answers


Hopefully you left out part of your transfer.cpp file, but in case you didnt it should define the namespace to match your header file like such:

namespace A{
  namespace B{
    namespace C{
        Transfer::Transfer(){
        }
        Transfer::~Transfer(){
        }
    }
  }
}

In case you have fully defined the namespaces, either inline or as above, you'll want to make sure you have the transfer.cpp file included in your project. Seems like the linker is unable to find your source file.


Since you have linker error and not a compiler error this tells you that your #include statement is doing what you want and the compiler recognizes the Transfer class and its constructor. The error occurs when the linker tries to find a reference to the implementation of the Transfer::Transfer() function in one of the .o or .lib files its told to link but cannot find it.

It's likely you have a transfer.cpp to go along with the transfer.h and this file is missing the implementation of the Transfer constructor and destructor. This could be because the functions are missing altogether or have been accidentally defined with a different signature.

So transfer.cpp should look something like:

A::B::C::Transfer::Transfer()
{
}

A::B::C::Transfer::~Transfer() 
{
}

Another possibility is that you are trying to link the implementaiton of transfer from a library. In this case you would need to tell your linker to use the .lib file as input. The syntax for this will depend on the compiler you are using.


Need Your Help

Can I change the width of a view inside a viewpager?

android layout android-viewpager

I'm currently using a Viewpager with 3 fragment. I would like to know if I can change the width of the first fragment.

How to add total in SSRS 2005

reporting-services reportingservices-2005 ssrs-grouping

How to add total in ssrs report using ssrs 2005 manually without using a wizard?