0

I have been coding on VC++ 2015 platform and I always get unresolved external symbol error on non-overridden base class functions. Here is the example :

//BaseClass.h
template<class T>
class BaseClass {
public:
   T* setRootPath(std::string&& rootPath);
....
}

//Implementations are in BaseClass.cpp

//ChildClass.h
class ChildClass:public BaseClass<ChildClass> {
public:
   ChildClass* setWidth(const int width);
   //No redefinition of "setRootPath" and hope not necessary
....
}

//Implementations are in ChildClass.cpp and i included BaseClass.cpp in this file as well

When I compile this code it throws no error. On the other hand during linkage time it says that it cannot find the required symbol for setRootPath with appropriate templating. Why can't it find "ChildClass* setRootPath(std::string&& rootPath)" function?

What I want to ask is that "Should I declare the function with an appropriate return type in ChildClass while I want to use the very same definition from the BaseClass"?

The error thrown by MSVC is :

error LNK2001: unresolved external symbol "public: class ChildClass * __cdecl BaseClass<class ChildClass>::setRootPath(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (?setRootPath@?$BaseClass@VChildClass@3@$$QEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

Thanks for your help in advance.

iliTheFallen
  • 466
  • 7
  • 16
  • no it has nothing to do with the question i have asked. The one you are referring to is about why compiler cannot find the definition of a template function. I have already solved this issue by including BaseClass's .cpp file in the ChildClass's .cpp file. This is not the problem I have stated... – iliTheFallen May 28 '16 at 15:02
  • You said in your comments that for BaseClass that the ***Implementations are in BaseClass.cpp*** which is the reason for marking it a duplicate. Although from your second comment ***Implementations are in ChildClass.cpp and i included BaseClass.cpp in this file as well*** you appear to be using `BaseClass.cpp` like some use an `.inl` file. http://stackoverflow.com/a/1208062/487892 – drescherjm May 28 '16 at 15:15
  • The truth is that all header(.h) and implementation(.cpp) files are separate. The only thing I did is to include BaseClass.cpp in ChildClass.cpp for compiler can find necessary function definitions(implementations) from the BaseClass. And it does. However linker says that "Oh I am sorry; but you dont have the correct function definiton for setRootPath". Hope this time it is clear. – iliTheFallen May 28 '16 at 15:23
  • I never ever include a `.cpp` file. These always are part of the project and never contain implementations of templates. Although with that said the `c++` language does not specify file extensions. You are free to use whatever convention you want. – drescherjm May 28 '16 at 15:30
  • It is something perfectly all right [inclusion model](https://books.google.com.tr/books?id=EotSAwuBkJoC&pg=PA61&lpg=PA61&dq=Inclusion+model+in+templates&source=bl&ots=iwO-H5_f8b&sig=_lQy9I36pfxWuU5iIw1IDCw-uK0&hl=en&ei=guCOTMbqGJHyvQPzs9DUCw&sa=X&oi=book_result&ct=result&redir_esc=y#v=onepage&q=Inclusion%20model%20in%20templates&f=false) – iliTheFallen May 28 '16 at 15:35
  • The confusion wasn't that you included it. It was that you chose the `.cpp` extension for the file you included. With that said I voted to reopen your question (4 more people need to agree with me). However I can't answer it. I am not sure if more information is needed about your implementation or not. – drescherjm May 28 '16 at 15:39

0 Answers0