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 9 months ago.
I am facing the error as in below,
Undefined first referenced
symbol in file
void bcMwMrsh::cppListFromWire<CSSCODERec*>(void*&, RWTValSlist<CSSCODERec*, std::allocator<CSSCODERec*> >&, AISObject* (*)()) /app/sdasup/home/mhp/source/develop/sd/lib/libsdHostSupport.so
ld: fatal: symbol referencing error
Under bc/include/bcMwMrsh.h cppListFromWire defined as below, Please note I have declared and defined the cppListFromWire in .h
typedef AbcObject *(*factory)()
namespace bcMwMrsh
{
template<typename listT>
void cppListFromWire(void *&src,RWTValSlist<listT> &dest, factory);
}
template<typename listT>
void bcMwMrsh::cppListFromWire(void *&src, RWTValSlist<listT> &list, factory factoryFunc)
{LOG (LM_MARSHAL_COM, (" bcMwMrsh::cppAbcListFromWire()...\n"));
int nItems = 0;
ptrdiff_t* pstart = (ptrdiff_t*) src;
bcMwMrsh::cppIntFromWire(src, &nItems);
for (int i = 0; i < nItems; i++)
{
AbcObject *elem = factoryFunc();
assert (elem); // improper assert; should cause exception - KD
elem->fromWire(src);
list.append(elem);
}
LOG (LM_MARSHAL_COM, (" bcMwMrsh::cppAbcListFromWire(): length %i\n", ptrdiff_t((ptrdiff_t*)src - pstart)));
}
and under "wv/include/cppmarsh.h" cppListFromWire been defined as in below,
typedef AISObject *(*factorywv)();
namespace bcMwMrsh
{
template<typename listT>
void cppListFromWire(void *&src,RWTValSlist<listT> &dest, factorywv);
}
template<typename listT>
void cppListFromWire(void *&src,RWTValSlist<listT> &list, factorywv factoryFunc)
{
LOG (LM_MARSHAL_COM, (" cppmarsh::cppListFromWire()...\n"));
ptrdiff_t* pstart = (ptrdiff_t*) src;
short nItems=0;
bcMwMrsh::cppShortFromWire(src, &nItems);
for (int i=0; i < nItems; i++)
{
AISObject *elem = factoryFunc();
assert(elem); // improper assert; should cause exception - KD
elem->fromWire(src, 0);
list.append(elem);
}
LOG (LM_MARSHAL_COM, (" cppmarsh::cppListFromWire(): length %i\n",
ptrdiff_t((ptrdiff_t*)src - pstart)));
}
And the .so (/app/sdasup/home/mhp/source/develop/sd/lib/libsdHostSupport.so) path where the error is pointing out has cppListFromWire used at two files as in below,
under sd/sdHostSupport/csscode.cpp
#include "csscode.h"
#include "cppmarsh.h"
int CSSCODERecList::fromWire(void *&buf, long bufLen)
{
bcMwMrsh::cppListFromWire(buf, contents, CSSCODERec::AISFactory);
return 0;
}
and under sd/sdHostSupport/__cppmarsh.cpp
#include "cppmarsh.h"
void cppIntFromWire(void *&src, int *dest)
{
assert(sizeof(int)==4);
*dest=0;
#ifndef LITTLE_ENDIAN //NT Port
*((char*)dest+3)=*((char*)src);
*((char*)dest+2)=*((char*)src+1);
#else
*((char*)dest)=*((char*)src);
*((char*)dest+1)=*((char*)src+1);
#endif
(char*&)src+=2;
}
template<typename listT>
void cppListFromWire(void *&src,RWTValSlist<listT> &list, factorywv factoryFunc)
{
int nItems = 0;
cppIntFromWire(src, &nItems);
for (int i = 0; i < nItems; i++)
{
AISObject *elem = factoryFunc();
assert(elem); // improper assert; should cause exception - KD
elem->fromWire(src, 0);
list.append(elem);
}
}
Is this something related to function-overloading, as I can see cppListFromWire defined has one parameter with different return type? However I see that is falling under overloading rules. Anything that differs here please let me know.
I tried including signature in both the files didn't work. Also tried including absolute header paths didn't work. One thing that worked was when I commented out below line form the file sd/sdHostSupport/csscode.cpp but that vague thing one can do.
bcMwMrsh::cppListFromWire(buf, contents, CSSCODERec::AISFactory);
I am unable to figure out what's the problem and how to resolve that. Been stuck for about 2 weeks now. Any kind of help/suggestions/inputs are greatly appreciated.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
This code is meant to help in profiling game code - and I can't seem to get my head around why it isn't working. I'm getting the following error message: undefined reference to `ProfileSample::profileSamples', and another for each of the points at which I've used its members.
#ifndef PROFILER_HPP_
#define PROFILER_HPP_
#define MAXIMUM_PROFILE_SAMPLES 16
class ProfileSample {
private:
int sampleIndex, parentIndex;
static int previousSampleIndex;
inline float getTime(void) {
return ((float)SDL_GetTicks()) / 1000.0f;
}
static struct profileSamples {
float start, duration;
char *name;
} profileSamples[MAXIMUM_PROFILE_SAMPLES];
public:
ProfileSample(const char *name) {
sampleIndex = 0;
while(profileSamples[sampleIndex].name != NULL)
sampleIndex ++;
parentIndex = (sampleIndex > 1) ? previousSampleIndex : -1;
previousSampleIndex = sampleIndex;
profileSamples[sampleIndex].name = (char *)name;
profileSamples[sampleIndex].start = getTime();
}
~ProfileSample(void) {
float end = getTime();
profileSamples[sampleIndex].duration = (end - profileSamples[sampleIndex].start);
if(parentIndex >= 0)
profileSamples[parentIndex].start -= profileSamples[sampleIndex].duration;
if(sampleIndex == 0)
output();
}
static void output(void) {
for(int i = 1; i < MAXIMUM_PROFILE_SAMPLES; i ++) {
printf("\nName: %s"
"\nDuration: %f"
"\nOverall percentage: %f",
profileSamples[i].name,
profileSamples[i].duration,
(profileSamples[0].duration / 100) * profileSamples[i].duration);
}
}
};
#endif /* PROFILER_HPP_ */
Can anybody explain what I'm missing here? Go easy, I've only just left C for C++
static struct profileSamples {
float start, duration;
char *name;
} profileSamples[MAXIMUM_PROFILE_SAMPLES];
You have just declared this static structure.
You haven't actually initialised it.
If you add
struct ProfileSample::profileSamples ProfileSample::profileSamples[16] = {};
int ProfileSample::previousSampleIndex = 1;
After the class declaration it will actually fix the issue.
You can think about it as a 2nd step, which is filling the memory with actual data. The first is just describing that some memory will be used and what interpretation data will have.
Hope that this, combined with issue marked in comments will help you understand what is going on.
This question already has answers here:
How to initialize private static members in C++?
(18 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I know that unresolved external symbol means that compiler can't find definition.
I got this error
Severity Code Description Project File Line
Error LNK2001 unresolved external symbol "private: static class std::set,class std::allocator > VirtualWorld::vWords" (?vWords#VirtualWorld##0V?$set#HU?$less#H#std##V?$allocator#H#2##std##A) ConsoleApplication11 c:\Users\Laptop\documents\visual studio 2015\Projects\ConsoleApplication11\ConsoleApplication11\Source.obj 1
with this code below (this code is fragments, but it's still generates this error)
#include <set>
#ifndef _TYPE_ID
#define _TYPE_ID
typedef unsigned int Id;
#endif
#ifndef _CLASS_VIRTUALWORLD
#define _CLASS_VIRTUALWORLD
class VirtualWorld
{
private:
static Id freeCounter;
static std::set<Id> vWorlds;
bool holding;
Id virtualWorldId;
static Id hold();
static void release(const Id & i);
public:
VirtualWorld();
~VirtualWorld();
Id getInstance();
void forceRelease();
};
#endif
int main() {
VirtualWorld virWorld;
return 0;
}
Id VirtualWorld::freeCounter = 1;
Id VirtualWorld::hold() {
std::set<Id>::iterator iter = vWorlds.lower_bound(freeCounter);
while (iter != vWorlds.end() && *iter == freeCounter)
{
++iter;
++freeCounter;
}
return freeCounter++;
}
void VirtualWorld::release(const Id & i) {
vWorlds.erase(i);
freeCounter = 1;
}
VirtualWorld::VirtualWorld() : holding(false) { }
VirtualWorld::~VirtualWorld()
{
if (holding) {
release(virtualWorldId);
}
}
Id VirtualWorld::getInstance() {
if (!holding) {
virtualWorldId = hold();
holding = true;
}
return virtualWorldId;
}
void VirtualWorld::forceRelease() {
if (holding) {
release(virtualWorldId);
holding = false;
}
}
I researched that I'm getting this error then trying to access vWorlds in hold() and release(int) functions. Why I'm getting this error and what should I change ?
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I start learning C++ in school and this error appear.
1>Bettle_Dice.obj : error LNK2019: unresolved external symbol "public: int __thiscall Beetle::checkcom(void)" (?checkcom#Beetle##QAEHXZ) referenced in function _main
I have include other header files and cpp files, I don understand why only this file have problem please help
Below is my code
main.cpp
#include "stdafx.h"
#include "beetle.h"
#include "dice.h"
#include "player.h"
#include <iostream>
using namespace std;
int main()
{
Player p; //declare Class to a variable
Dice d;
Beetle btle;
int temp;
cout << "Number of players?" << endl;
cin >> temp;
p.Num(temp); //store the number of player into class
//cout << p.getNumPlayers() <<endl;
cout << "Start game!!" <<endl;
temp = btle.checkcom();
while(temp != 1)
{
for(int i=0;i<p.getNumPlayers();i++)
{
temp = d.roll();
cout <<"Your roll number:"<< temp;
}
}
return 0;
}
beetle.h
class Beetle
{
private:
int body,head,ante,leg,eye,tail;
public:
int completion();
int checkcom();
int getBody() { return body;};
int getHead() { return head;};
int getAnte() { return ante;};
int getLeg() { return leg;};
int getEye() { return eye;};
int getTail() { return tail;};
};
Beetle.cpp
#include "stdafx.h"
#include "beetle.h"
int completion()
{
return 0;
}
int checkcom()
{
Beetle btle;
int flag = 0;
if(btle.getBody() == 1 && btle.getHead() == 1 && btle.getAnte() == 2 && btle.getEye() == 2 && btle.getLeg() ==6 && btle.getTail() == 1)
flag = 1;
return flag;
}
I checked some solution on the internet, some are saying is the library problem, but this file is not a built-in function. I guess it is not the problem of the library. I tried to include the beetle.obj file to it and the debugger said it is included already and duplicate definition.
In the other file, i do not have "bettle" this word. It should not be the problem of double declaration or duplicate class.
I have no idea what the problem is. Please help.
You need to prefix the signature of your class functions with the class name Beetle::
Otherwise the compiler just thinks those functions are global functions, not member functions.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I create this file over and over and cant seem to see why I'm getting this error. I tried going to the line where the code is but the format seem correct I may just need another set of eyes .
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void readString(char*, int);
void changeToUppercase(char*, int);
void displayStringInUppercase(char*, int);
int main()
{
int arraySize;
char* characterArray;
cout << "Enter the size of dynamic array: ";
cin >> arraySize;
characterArray = new char[arraySize];
readString(characterArray, arraySize);
changeToUppercase(characterArray, arraySize);
displayStringInUppercase(characterArray, arraySize);
delete [] characterArray;
system ("pause");
return 0;
}
void changeToUppercase(char* characterArray, int arraySize)
{
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
void displayStringInUppercase(char* characterArray, int arraySize)
{
cout << "\nThestring inupper case letters: ";
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
This is the error codes that keep popping up:
error LNK2019: unresolved external symbol "void __cdecl readString(char *,int)" (?readString##YAXPADH#Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals
You use a forward declaration: void readString(char*, int); but then never actually define this function.
Define your readString function later in your code like...
void readString(char* str, int a)
{
// do stuff
}
You are missing the readString function. You have a forward declaration that satisfies the compiler here
void readString(char*, int);
But no actual implementation of the function to satisfy the linker when it tries to put your program together. You need something along the lines of
void readString(char* characterArray, int arraySize)
{
// do stuff here
}
I'm trying to include a simple hash table class in some files with a header class. But whenever I try to compile I get several errors like this:
LNK2019: unresolved external symbol " public: __thiscall HashTable::~HashTable(void)" (??1HashTable##QAE#XZ) referenced in function _main "
I'm using Visual Studio 2010. I am aware that this means it can't find the function definition in any of the source files. But I have defined them, in a file in the same directory as the file it's called in. Perhaps Visual Studio doesn't look in the current directory unless you set some linker option?
Here is the source code:
//HashTable.h
#ifndef HASH_H
#define HASH_H
class HashTable {
public:
HashTable();
~HashTable();
void AddPair(char* address, int value);
//Self explanatory
int GetValue(char* address);
//Also self-explanatory. If the value doesn't exist it throws "No such address"
};
#endif
//HashTable.cpp
class HashTable {
protected:
int HighValue;
char** AddressTable;
int* Table;
public:
HashTable(){
HighValue = 0;
}
~HashTable(){
delete AddressTable;
delete Table;
}
void AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};
};
No you have not. You created a new class.
The proper way to define the methods is:
//HashTable.cpp
#include "HashTable.h"
HashTable::HashTable(){
HighValue = 0;
}
HashTable::~HashTable(){
delete AddressTable;
delete Table;
}
void HashTable::AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int HashTable::GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};