Linking error in C++ - implementing a indexList - c++

Linking...
Directory.obj : error LNK2019: unresolved external symbol "public: void __thiscall indexList<class entry,100>::read(class std::basic_istream<char,struct std::char_traits<char> > &)" (?read#?$indexList#Ventry##$0GE###QAEXAAV?$basic_istream#DU?$char_traits#D#std###std###Z) referenced in function _main
Getting this error and others associated with indexList implementation. I have included all the right files, not sure what this means?
indexList.h
indexList.cpp
Also, using VS .NET 2003 - They are under the "Source Files" and "Header Files" However, I tested with deleting the indexLish.h and the error doesn't change?

What you have is a class template. This means when the compiler needs to call a function, it will look at your template definition and generate the corresponding code as needed.
For example, the following probably has a compile-time error in it if tried to call it:
template <typename T>
void doSomething(const T& x) {
x->_this_function_does_not_exist_ever_();
}
But as long as you don't call doSomething you won't get errors.
The problem you have is that your header file tells the compiler "hey, these functions exist", but when the compiler tries to generate them it cannot find any definitions. (You cannot "compile" definitions in a source file and link them in, they must be visible to the caller.)
The most common solution is to simply define the entire class template in the .h or .hpp file.

Are you using visual studio then include both the files into the solution and then run.

Since you are using templates, the best way is to include the definition in .H file.
I read something from this book . And here is something it may help you too.

Related

LNK2019 unresolved external symbol after moving functions to another header file

I had two array templates in one of my header files, everything worked well. Then I thought, I'd better get some more pedantism into my code, so I moved them all into another header file, mostly just for them (and for another function, that wants to use one of them). And then I got LNK2019 error every time I used functions from these templates in other header files.
Since everything was good before I pasted my code elsewhere, I assume the code is okay, it's just my lack of understanding. Basically, the question is: why do I get a linker error when I moved my function to another header file?
Here is an example of one of my errors:
Error LNK2019 unresolved external symbol "public: char __thiscall C2DArray::Get(int,int)" (?Get#?$C2DArray#D##QAEDHH#Z) referenced in function "public: char __thiscall SGame::GetRecordOutput(int,int)" (?GetRecordOutput#SGame##QAEDHH#Z) Mastermind C:\Users\Master\Documents\Visual Studio 2015\Projects\Mastermind\Mastermind\Menu.obj 1
I'm using Visual Studio 2015 if it matters.
Thank you for the reply. As it seems, I have resolved my problem. The cause was the class templats, which were not specialized (and had to be). All I had to do is include what types of these templates I wanted. Sorry for posting before doing more extensive research.

static library linkage failure in visual c

In visual c++, I created a static library with two files, myLib.h and myLib.cpp. I also have a console application project with the file testSequence.cpp that references this library.
Within myLib.h I have defined a class template<class prec> class sequence which has the function declaration prec *getPrimes(int numToGet) this function is then defined in myLib.cpp. However, when I build testSequence, there is a linking error, and it says error LNK2019: unresolved external symbol "public: int * __thiscall mathLib::sequence<int>::getPrimes(int)" (?getPrimes#?$sequence#H#mathLib##QAEPAHH#Z) referenced in function "char * __cdecl codeString(char *,char *,bool)" (?codeString##YAPADPAD0_N#Z)
So, yeah, help would be nice.
Read this for an explanation of the error.
Basically, what you're trying to do cannot be done. The compiler must be able to see the implementation of the class template when it tries to instantiate it for a given template type parameter. You need to move the implementations for all the member functions to the header file.

C++ linkage error

I compile in Visual studio 2008 and get this error. I have researched linkage error but am still uncertain to what it is. This is the finished code to a poker game so I would rather not post the code. Can someone translate this error message for me?
error LNK2019: unresolved external symbol "void __cdecl betFold(double)" (?betFold##YAXN#Z) referenced in function "void __cdecl flopAction(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?flopAction##YAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) OH-DLL.obj
Your function void flopAction(std::string arg) uses a function betFold(double) that gets referenced and declared in some header, but is not implemented so that the linker is able to find it.
It means that you have declared this method but not defined it. Or at least the linker cannot find the definition, either because it’s in a library that you didn’t reference or else because it’s in an object file (source file) that is not part of your build process.
Sound like you forgot to specify the *.lib file that belongs to the *.dll. You can edit the list under your Project Property Pages -> Configuration properties -> Linker -> Input, remember to do this for the Debug and Release configuration.
And please try to refrain from phrases like wtf etc :)
Also, you could check your signature(function declaration), so that it contains only the type in it's parameter lists, while inside the definition(.cpp file), it contains both the type and parameter names. For eg,
in the .h file where the declaration sits:
void myfunc(int, char*);
and in the .cpp file where the definition sits:
void myfunc(int num, char* name)
{
//
}
I learnt this before in my college, but don't sure if Dev C++ supports it, left this things for a long time ago, just using Borland at that time.
hope this helps.
thanks.

Linker error 'unresolved external symbol' : working with templates

I have a template based class [Allotter.h & Allotter.cpp]:
template <typename allotType> class Allotter {
public:
Allotter();
quint32 getAllotment(allotType*);
bool removeAllotment(quint32, int auto_destruct = 0);
private:
QVector<QPair<quint32, allotType*>> indexReg;
int init_topIndex;
};
and it's usage is shown as [ActiveListener.h & ActiveListener.cpp]:
class ActiveListener: public QObject {
Q_OBJECT
public:
ActiveListener();
private slots:
void processConnections();
void readFromSocket(int);
private:
QTcpServer* rootServer;
QSignalMapper* signalGate;
Allotter<QTcpSocket> TcpAllotter;
};
I am not showing the complete definitions, since it doesn't really matter. The problem is when I compile, all files compile properly. The files are in a VC++ project. Earlier when I did not use a template-based approach for Allotter, everything was compiling and linking fine. But now, I get this error:
1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: __thiscall Allotter<class QTcpSocket>::Allotter<class QTcpSocket>(void)" (??0?$Allotter#VQTcpSocket####QAE#XZ) referenced in function "public: __thiscall ActiveListener::ActiveListener(void)" (??0ActiveListener##QAE#XZ)
1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall Allotter<class QTcpSocket>::getAllotment(class QTcpSocket *)" (?getAllotment#?$Allotter#VQTcpSocket####QAEIPAVQTcpSocket###Z) referenced in function "private: void __thiscall ActiveListener::processConnections(void)" (?processConnections#ActiveListener##AAEXXZ)
The surprising thing is, that the constructor, ActiveListener::ActiveListener() does not make any reference at all Allotter<QTcpSocket>::Allotter(). The second reference however does exist. But I don't understand why the linker isn't able to resolve this external symbol.
The build output just before the errors appear is:
1>Moc'ing ActiveListener.h...
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>ActiveListener.cpp
1>Allotter.cpp
1>moc_ActiveListener.cpp
1>main.cpp
1>Generating Code...
1>Linking...
I don't understand if any of this is relevant, mostly because all this used to work perfectly before. It's just that after I use templates a problem is caused.
Any help will be appreciated. Thanks a lot.
You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
Generally speaking it is considered best practice to write your template code entirely inside header files. There is an important technical reason for this: when you instantiate a template, the C++ compiler needs to generate code from that template that is specific to the template parameters that you have specified. If your template code is placed entirely in your headers, this is done for you automatically.
It is definitely possible to write template code the way that you have, with the implementation placed in cpp files. If you do this, however, you are required to explicitly instantiate the template instance that you intend to use.
In your case, you need to add the following line to a .cpp file in your project:
template class Allotter<QTcpSocket>;
Since you can't place template implementation in .cpp files, it is considered a good practice to use .inl files for the template implementation and include them from the template headers.

Error in Visual Studio

I get this error in visual studio and I don't know the reason. It doesn't even show the line number. Any clue?
Error 1 error LNK2028: unresolved
token (0A000041) "void __cdecl
free_img(struct Image *)"
(?free_img##$$FYAXPAUImage###Z)
referenced in function "double *
__cdecl calc_zernike_moments(struct Image const *,int,struct ZernikeBasis
const *)"
(?calc_zernike_moments##$$FYAPANPBUImage##HPBUZernikeBasis###Z) zernike_moments.obj TestLibrary
You have a routine
double * __cdecl calc_zernike_moments(struct Image const *foo,
int baz,
struct ZernikeBasis const *bar)
that calls a routine
void __cdecl free_img(struct Image *foo)
and you didn't supply the free_img() routine that matched to the linker.
free_img() is a function that is either defined in a .cpp file that you haven't included in the project, or it is in a DLL or static library that you haven't linked against. If it is the former, you need to search for the function in your source files and then add that .cpp file to the project. If it is the latter, then you need to identify which library provides free_img() and then locate the .lib file for that library. Then you can do this:
To add .lib files as linker input in the development environment
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Additional Dependencies property.
(from http://msdn.microsoft.com/en-us/library/ba1z7822(VS.80).aspx)
You need to link the library. Where is the definition of free_img()?
You are just including the .h and not linking lib
The error is a linker error, not a compiler error, so there won't be a line number associated with it. Rather the error is telling you that your function calc_zernike_moments is calling another routine, free_img, that isn't defined in any of TestLibrary's compiled sources, so you need to provide it by other means. Typically what's missing here is the third-party library need be included in the project so the linker can bring in free_img's implementation.