This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 5 years ago.
I'm trying to create a generic button and I want to be able to pass a function to call when I click it.
main.cpp:
#include "Window.h"
int testfunction() {
print("Test");
return 1;
}
...other stuff...
window->SetButtonFunction_NoArgs<int>(" *unrelated stuff* ", &testfunction);
window is a WindowMain class:
window.h:
class WindowMain {
*Stuff*
template <class R1> void SetButtonFunction_NoArgs(string id, R1(*func)());
*Other Stuff*
};
window.cpp:
///Find the object of that id and set the fuction to it
template <class R1> void WindowMain::SetButtonFunction_NoArgs(string id, R1(*func)()) {
for (int i = 0; i < objects_.size(); i++) {
if (objects_[i].getId() == id) {
objects_[i]->AddButton_NoArgs<R1>(func);
}
}
}
The problem is here somewhere I think but I can't find it... When I try to compile it gives me this:
1>main.obj : error LNK2019: unresolved external symbol "public: void __cdecl WindowMain::SetButtonFunction_NoArgs<int>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int (__cdecl*)(void))" (??$SetButtonFunction_NoArgs#H#WindowMain##QEAAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##P6AHXZ#Z) referenced in function "void __cdecl Create(void)" (?Create##YAXXZ)
Thanks for help!
Template methods must be defined in header file. If it wasn't template you could move definition to .cpp file (you would want to include window.h though)
Related
I am trying to create a class library dll that exports a template class. I have a local version of the same class and it must be coincidental, but it works correctly. I know this doesn't really mean much, clearly something is wrong.
The exported template class in the library dll is:
template <class CcmBlock>
class CCMSHARED_EXPORT CcmFilter
{
public:
CcmFilter()
{
mBlock = nullptr;
////mBlockList = new std::list<CcmBlock*>();
}
void add(CcmFrame* frame)
{
if (frame == nullptr)
return;
mBlock = new CcmBlock(
frame->getFrameData(),
frame->getSampleRate(),
0xFFFFFFFF,
frame->getBlockSize(),
frame->getDomain()
);
mBlockList->push_back(aBlock);
}
CcmBlock* get()
{
return mBlock;
}
private:
CcmBlock* mBlock;
////std::list<CcmBlock*>* mBlockList;
};
On the application side:
CcmFilter<FooBlock>* oneFilter = new CcmFilter<FooBlock>();
//Filter<Block>* filter = new Filter<Block>();
CcmFrame* frame = new CcmFrame(0, 50000, 40, 1024, Domain::Time);
oneFilter->add(frame);
CcmBlock* block = oneFilter->get();
FooBlock* fooBlock = dynamic_cast<FooBlock*>(block);
if (fooBlock == nullptr)
{ //Report Error }
else
{ // Do the work}
and the FooBlock class is derived from CcmBlock as follows:
class FooBlock : public CcmBlock
{
public:
FooBlock(int* ipblock, DWORD dwSampleRate, DWORD dwMicrophoneIndex, DWORD dwBlockSize, Domain domain);
void process();
};
The library compiles and builds the dll. When I attempt to build the application I get messages:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl CcmFilter::CcmFilter(void)" (__imp_??0?$CcmFilter#VFooBlock####QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl CcmFilter::add(class CcmFrame *)" (__imp_?add#?$CcmFilter#VFooBlock####QEAAXPEAVCcmFrame###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
DLL-s are loaded at runtime, while templates are instantiated at compile time.
A work-around which is not ideal but might work for a very limited use case is to explicitly instantiate a template somewhere after their declaration. That is, add something like:
template class CcmFilter<FooBlock>;
For details, see https://msdn.microsoft.com/en-us/library/by56e477(VS.80).aspx
Just do not use declspec. But in your case template will be compiled each time when you include it in *.cpp file. It wont be exported.
In order to export some well-known implementations of your template you probably should use template explicit instantiation definitions in tandem with __declspec(export).
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Here's a simple example of using class templates in C++. This code works.
#include <iostream>
using namespace std;
template <class T>
class Test {
public:
Test();
void print();
private:
int i;
};
template <class T>
Test<T>::Test() {
i=1;
cout<<"New instance"<<endl;
}
template <class T>
void Test<T>::print() {
cout<<"Test"<<endl;
}
int main() {
Test<int> i;
i.print();
return 0;
}
So when I separate this code in 3 files: main.cpp, Test.h, Test.cpp:
//Test.h
#include <iostream>
using namespace std;
template <class T>
class Test {
public:
Test();
void print();
private:
int i;
};
//Test.cpp
#include "Test.h"
template <class T>
Test<T>::Test() {
i=1;
cout<<"New instance"<<endl;
}
template <class T>
void Test<T>::print() {
cout<<"Test"<<endl;
}
//main.cpp
#include "Test.h"
using namespace std;
int main() {
Test<int> i;
i.print();
return 0;
}
I get an errors:
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Test<int>::print(void)" (?print#?$Test#H##QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Test<int>::Test<int>(void)" (??0?$Test#H##QAE#XZ) referenced in function _mai
1>C:\Programming\C++\stuff\templass\Debug\templass.exe : fatal error LNK1120: 2 unresolved externals
I use Microsoft Visual C++ 2010 Express. So I searched a lot about unresolved external symbol but didn't find anymore for this case. So what's my mistake?
Templates cannot be compiled like any other source file. Both interface and implementation should live in the header file (although some split them in .hpp files for interface and .ipp files for implementation, and then include the .ipp file at the end of the .hpp file).
How would the compiler know what classes to generate when the template class is compiled?
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I read that its possible to create template method. I have something like this in my code
File : Student.h
class Student
{
public:
template<class typeB>
void PrintGrades();
};
File: Student.cpp
#include "Student.h"
#include <iostream>
template<class typeB>
void Student::PrintGrades()
{
typeB s= "This is string";
std::cout << s;
}
Now in main.cpp
Student st;
st.PrintGrades<std::string>();
Now I get a linker Error:
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Student::PrintGrades<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??$PrintGrades#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Student##QAEXXZ) referenced in function _main
Any suggestion on what I might be doing wrong ?
The template is not instantiated anywhere, causing a linker error.
For templates defined in the header, the compiler will generate the instantiation by itself, because it has access to its definition. However, for templates defined in the .cpp file, you need to instantiate them yourself.
Try adding this line to the end of your .cpp file:
template void Student::printGrades<std::string>();
I am trying to implement a PointArray class derived from a template. Here is what my hpp file for PointArray looks like:
#ifndef POINTARRAY_H
#define POINTARRAY_H
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "Array.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
class PointArray : public Array<Point>
{
public:
PointArray();//Default constrcutor.
PointArray(int size);//Constructor with size argument.
PointArray(const PointArray& arr);//Copy constructor.
~PointArray();//Destructor.
double Length() const;//Length function.
};
}
}
#endif
My cpp looks like this :
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "PointArray.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
//Default constructor.
PointArray::PointArray(): Array<Point>()
{
cout<<"Point arr default cons"<<endl;
}
//Constructor with size argument.
PointArray::PointArray(int size) : Array<Point>(size)
{
}
//Copy constructor.
PointArray::PointArray(const PointArray& arr) : Array<Point>(arr)
{
}
//destrcutor.
PointArray::~PointArray()
{
}
}
}
I get the LNK error :
error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(void)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(void)" (??0PointArray#CONTAINERS#Abhishek##QAE#XZ)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(int)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#H#Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(int)" (??0PointArray#CONTAINERS#Abhishek##QAE#H#Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(class Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point> const &)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#ABV012##Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(class Abhishek::CONTAINERS::PointArray const &)" (??0PointArray#CONTAINERS#Abhishek##QAE#ABV012##Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::~Array<class Abhishek::CAD::Point>(void)" (??1?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function __unwindfunclet$??0PointArray#CONTAINERS#Abhishek##QAE#XZ$0
1>C:\Users\Rambo\Documents\Level 6\Section 4.2b\Exercise 3\Debug\Exercise 3.exe : fatal error LNK1120: 4 unresolved externals
I dont understand why this could be happening. I included all the relevant header files and CPP files. If anyone can help I will really appreciate it.
You forgot to post the most relevant header: the one that declares the class template that causes the error. Almost certainly, that header declares the default constructor of the Array template:
Array();
but doesn't define it; either there is no definition, or you have a definition in a source file.
In either case, you'll get an error since templates must be defined in any translation unit that uses them. This means that you'll need to define the constructor (and any other member functions) in the header, to include them wherever they are used.
If that's not the problem, then please post the header so we can investigate further.
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::getfirst(int &)" (?getfirst#?$LinkedSortedList#H##UAE_NAAH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::clear(void)" (?clear#?$LinkedSortedList#H##UAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::print(void)const " (?print#?$LinkedSortedList#H##UBEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::insert(int)" (?insert#?$LinkedSortedList#H##UAE_NH#Z) referenced in function _main
1>main.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::find(int)const " (?find#?$LinkedSortedList#H##UBE_NH#Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall LinkedSortedList<int>::size(void)const " (?size#?$LinkedSortedList#H##UBEHXZ)
1>c:\users\chris\documents\visual studio 2010\Projects\lab0\Debug\lab0.exe : fatal error LNK1120: 6 unresolved externals
This is what I recieve when trying to compile my code. I've narrowed it down to (i believe) this section of code here:
#ifndef _LinkedSortedListClass_
#define _LinkedSortedListClass_
#include "LinkedNode.h"
#include "SortedList.h"
template <class Elm>
class LinkedSortedList: public SortedList<int> {
public:
void clear();
bool insert(Elm newvalue);
bool getfirst(Elm &returnvalue);
void print() const;
bool find(Elm searchvalue) const;
int size() const;
private:
LinkedNode<Elm>* head;
};
#endif
This is the child class of the SortedList, which is this, in case it's needed..
#ifndef _SortedListClass_
#define _SortedListClass_
template <class Elm> class SortedList {
public:
// -------------------------------------------------------------------
// Pure virtual functions -- you must implement each of the following
// functions in your implementation:
// -------------------------------------------------------------------
// Clear the list. Free any dynamic storage.
virtual void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
virtual bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
virtual void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
virtual bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
virtual int size() const = 0;
};
#endif
Thanks so much for any help; our last class taught us nothing of inheritance, but this is project #1 for this class, without being taught inheritance here either, so this is all touch and go for me, despite what I managed to look up on Google.
Your methods aren't defined. So the linker is complaining because it can't link to their definitions.
Maybe it helps if you placed the definitions of your functions in your header file. This makes it easier for the compiler to resolve these external symbols.
I hope this will help.
Regards,
Philinator