3

this is an excerpt of a program I've been working on. I suspect that the problem may be shomehow caused by this member of the class Player:

std::vector<std::list<Bid> > lBidding

The code results in the follow 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 feel pretty confident that this error has something to do with the static vector since that was the last piece of code i added. My research has shown plenty of different problems resulting in similar errors but none of them seem related to the same sort of code that I have.

Code:(I've trimmed away several hudred lines of code that don't seem to apply. This should be the bare minimum.)

#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 could someone plese tell me, what is wrong with my code and why. Btw, if anyone's wondering this constitutes the bulk of a sort of homemade Bridge simulator.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What do you have inside `/Users/andvik/MockUp/fudge/fudge.txt` ? It contains the arguments to `g++` probably in the wrong order, or missing. And adding `-L.` after it is wrong, you should put `-L.` before `@/Users/andvik/MockUp/fudge/fudge.txt` – Basile Starynkevitch Mar 14 '12 at 18:23
  • Is this in MacOSX? (It looks like, and it is important as you might be building universal binaries, which would explain an error from the linker stating that some symbols are not defined for some particular architectures) – David Rodríguez - dribeas Mar 14 '12 at 18:24
  • The IDE (CodeLite) which I'm running on OS X Lion did that call to the compiler. The content of fudge.txt is just this: ./Debug/MockUp_main.o. –  Mar 14 '12 at 18:27

1 Answers1

7

The reason is that lBidding is static, but you're declaring and not implementing it. If you just add;

std::vector<std::list<Bid> > Player::lBidding;

to the end of the file, everything compiles happily.

Since static variables aren't part of an object but instead of the class, you'll need to implement them once in a cpp file, usually by convention named <classname>.cpp. It's not a good idea to put it in a .h file since that will implement it in every file that includes it and lead to (justified) linker errors.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Oh, that's right. I remember reading something about the difference between declaring and implementing static variables way back when. Never would have got that one on my own though. Thanks a bunch. –  Mar 14 '12 at 18:39