What causes the Undefined symbol for x86_64 architecture "Build error?"
This is an excerpt from a program I am working on. I suspect this problem could be caused by this member of the Player class:
std::vector<std::list<Bid> > lBidding
The code results in the following build error:
g++ -o ./Debug/fudge @"/Users/andvik/MockUp/fudge/fudge.txt"-L.
Undefined symbols for architecture x86_64:
"Player::lBidding", referenced from:
Deal::bidding() in MockUp_main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[1]: *** [Debug/fudge] Error 1
make[1]: Leaving directory `/Users/andvik/MockUp/fudge'
make: *** [All] Error 2
make: Leaving directory `/Users/andvik/MockUp'
----------Build Ended----------
0 errors, 0 warnings
I'm sure this error has something to do with the static vector, as this was the last piece of code I added. My research has shown many different problems leading to similar errors, but none of them are related to the same code that I have.
Code: (I've trimmed a few concise lines of code that don't seem to apply. This should be minimal.)
#include <string>
#include <list>
#include <vector>
class Bid
{
public:
short iLevel;
short iSuit;
Bid():iLevel(0),iSuit(0){};
Bid(short l, short s):iLevel(l),iSuit(s){};
bool operator ==(const Bid Other) const
{
if(iLevel==Other.iLevel && iSuit==Other.iSuit)
{return 1;}
else{return 0;}
}
};
class Player
{
public:
short iPos;
static std::vector<std::list<Bid> > lBidding;
Player(short p):iPos(p) {};
bool operator ==(const Player Other) const
{
if(iPos == Other.iPos){return 1;}
else{return 0;
}
}
};
class Deal
{
public:
short iPass;
short iIteration;
Player pWest, pNorth, pEast, pSouth, pError;
Deal(): iPass(0), iIteration(0), pWest(0), pNorth(1), pEast(2), pSouth(3), pError(5){};
void bidding()
{
Player pTemp(5);
Bid bTemp, bPass;
while(iPass < 3)
{
pTemp = setPlayer();
if(pTemp == pError){break;}
if(Player::lBidding.at((pTemp.iPos+2)%4).empty()){}
else
{
if(Player::lBidding.at(pTemp.iPos).empty()){}
}
if(bTemp == bPass){++iPass;}
else{(*Player::lBidding.begin()).push_back(bTemp);}
};
}
Player setPlayer()
{
switch(iIteration % 4)
{
case 0: return pWest; break;
case 1: return pNorth; break;
case 2: return pEast; break;
case 3: return pSouth; break;
}
return pError;
}
};
int main(int argc, char **argv)
{
srand(time(0));
Deal deal;
deal.bidding();
printf("No debugging error.\n");
return 0;
}
So can someone tell me what is wrong with my code and why. By the way, if anyone is wondering what is the main part of their homemade bridge simulator.
source to share
The reason is that lBidding is static, but you are declaring and not implementing it. If you just added:
std::vector<std::list<Bid> > Player::lBidding;
to the end of the file, everything will compile with joy.
Since static variables are not part of the object, but instead of a class, you need to implement them once in the cpp file, usually by the convention name <classname> .cpp. It's not a good idea to put it in a .h file as it will implement it in every file that includes it and will lead to (justifiable) linker errors.
source to share