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 ?
Related
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.
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.
I'm working on a MP3 player for an assignment. I keep getting the following errors:
1>A4_main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Song::ClearListFile(void)" (?ClearListFile#Song##SAXXZ) referenced in function "void __cdecl DeleteSong(void)" (?DeleteSong##YAXXZ)
1>A4_main.obj : error LNK2001: unresolved external symbol "public: static char * Song::songListFile_" (?songListFile_#Song##2PADA)
1>A4_Song.obj : error LNK2001: unresolved external symbol "public: static char * Song::songListFile_" (?songListFile_#Song##2PADA)
1>A4_Song.obj : error LNK2019: unresolved external symbol __imp__mciSendStringA#16 referenced in function "public: void __thiscall Song::PlaySong(void)" (?PlaySong#Song##QAEXXZ)
1>A4_Song.obj : error LNK2001: unresolved external symbol "public: static char * Song::currentMP3_" (?currentMP3_#Song##2PADA)
From what I understand, these sort of errors stem from not including function declarations, declaring but not implementing them, misspelling, etc. What have I missed here? Since this is an assignment, I'll post the bare minimum of the code I think is causing the problem.
A4_main.cpp
#include "A4_LinkedList.h"
#include "A4_Song.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
using namespace std;
LinkedList g_list;
void FreeLinkedList();
char DisplayMenu();
void LoadSongFile();
void AddNewSong();
void DeleteSong();
void PlaySong();
void PrintAllSongs();
//stuff
void LoadSongFile()
{
const int SZ = 256;
int songCnt = 0;
ifstream inData;
char buff[SZ];
Song* newSong;
_flushall();
cout << "\nEnter the full file path: ";
cin.getline(Song::songListFile_, SZ);
// Open the file
inData.open(Song::songListFile_);
// Free any memory currently allocated for the word array
FreeLinkedList();
// Loop through file again and allocate memory
while (!inData.eof())
{
// Each time through loop read all 5 entries in each line.
// Songt with the Song name
inData.getline(buff, SZ);
if (buff[0] == 0)
{
// No more words
break;
}
// Create a new Song object
newSong = new Song(buff);
if (newSong == 0)
{
cout << "\nDynamic memory allocation failed.";
break;
}
// Add this Song object to the linked list
g_list.AddLinkToBack(newSong);
songCnt++;
}
inData.close();
cout << "\nLoaded file and read " << songCnt << " Song objects.";
}
void DeleteSong()
{
const int SZ = 256;
bool foundSong = false;
Node* node = g_list.GetFirstNode();
Song* song = 0;
char songFileName[SZ];
_flushall();
// Prompt the user for the name of a song
cout << "\nEnter the song name with extension: ";
cin.getline(songFileName, SZ);
// Loop through the linked list for that song and delete it if it is found.
// If not, print error to console.
while (node != 0)
{
// Cast the void ptr to a song object ptr
song = (Song*)(node->data_);
// Call on the Song class to print the objects contents
if (strcmp(song->GetSongName(), songFileName) == 0)
{
// Set flag and get out of loop
g_list.RemoveThisLink(node);
foundSong = true;
break;
}
// Go to the next node
node = node->next_;
}
if (!foundSong)
{
cout << "\nCould not find that song in list!\n";
}
else
{
// Now that the linked list has been updated need to persist the new
// list to the song file, replacing previous contents.
Song::ClearListFile();
// Now loop through the linked list again, appending the song
// file name to the song list file.
node = g_list.GetFirstNode();
while (node != 0)
{
// Cast the void ptr to a song object ptr then add name to file
song = (Song*)(node->data_);
song->AppendToListFile();
// Go to the next node
node = node->next_;
}
}
}
A4_Song.cpp
#include "A4_Song.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
// Store the path name of the song list file
static char songListFile_[ENTRY_SZ] = "";
static char currentMP3_[ENTRY_SZ] = "";
// Static method to empty the song list file
static void ClearListFile()
{
ofstream outFile;
if (songListFile_[0] != 0)
{
// Open for truncate then close again
outFile.open(songListFile_, ios_base::ate);
outFile.close();
}
else
cout << "\nNothing to clear!";
}
void Song::PlaySong()
{
const int BUFF_SZ = 512;
char fullStr[BUFF_SZ];
MCIERROR err;
StopSong();
// Set global variable so we know this file is playing.
// Sandwich the file name in escaped double quotes so
// spaces can be included and don't need to double up
// on the backslashes.
sprintf_s(currentMP3_, ENTRY_SZ, "\"%s\"", songPath_);
sprintf_s(fullStr, BUFF_SZ, "open %s type mpegvideo alias myFile", currentMP3_);
err = mciSendString(fullStr, NULL, 0, 0);
err = mciSendString("play myFile", NULL, 0, 0);
}
Let me know if I've omitted too much.
Your problem is that you have variables and functions declared inside Song, but are defined at namespace level. This makes them different entities, and so definitions for the declarations in Song are never found.
static char songListFile_[ENTRY_SZ] = "";
static char currentMP3_[ENTRY_SZ] = "";
static void ClearListFile() {/*...*/}
These should be changed. Removing static and prefixing the surrounding class Song should fix it.
char Song::songListFile_[ENTRY_SZ] = "";
char Song::currentMP3_[ENTRY_SZ] = "";
void Song::ClearListFile() {/*...*/}
You only need the static inside the class definition. Outside it, you're using a different meaning of the static keyword. I think you have more instances like this outside the code you posted, but it shouldn't be difficult to find them.
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;
};