Code Question
Posted in Code
First watch, 4 bells (10:21 pm)

Anybody know why I can't do this?


class Room {
  typedef boost::shared_ptr<Room> RoomPointer;
  typedef std::map<std::string, RoomPointer> RoomList;

And in another object, reference it like this:

RoomList mRoomList;
if((Room::RoomList::iterator it = mRoomList.find("name")) != mRoomList.end()) {
   return it->second;

I always get error: ‘::iterator’ has not been declared back from the compiler.

If I do this, it works fine:

Room::RoomList::iterator it = mRoomList.find("name");
if(it != mRoomList.end()) {
   return it->second;

I ask because Nicolai Josuttis (in The C++ Standard Library) recommends comparing the iterator against the end() condition on the same line because of type conversion of various unsigned types that may be used.

1 Comment »
Rookie Mistake
Posted in Code
Last dog watch, 1 bell (6:56 pm)

Today I found I made a rookie mistake in my code project at work. I've been tracking down an error for an hour or so when I finally realized it was due to an uninitialized pointer. I feel so stupid. It shouldn't have happened.

Leave a Comment »
Linking Error
Posted in Code
First dog watch, 3 bells (5:31 pm)

I just solved an annoying linking error in Linux. I have a program I'm writing that has two subdirectories in its source directory, and inside those two directories, two static libraries are built. In the parent directory, all objects are built fine, and everything's shiny until it tries to link the final binary, when I get all these undefined reference to `class::class()`, etc. The constructor, destructor, and other functions seem to have disappeared when I link the library to the main application!

Using nm I can see that the library has those symbols defined! What's going on?

Turns out it was a problem with my Makefile where I set up the linking. I was adding the link path and libraries before the object files of the parent directory. I didn't think there was a problem with this as long as everything was on the same command line. Well it looks like I was wrong. When I changed my linking from

g++ -o program_name -Lpath1 -Lpath2 -lstaticlib1 -lstaticlib2 object1.o object2.o ...


g++ -o program_name object1.o object2.o ... -Lpath1 -Lpath2 -lstaticlib1 -lstaticlib2

everything worked perfectly! Sheesh, picky linker!

PS Technically I'm using Makefile variables, so it's easier to read. The actual line looks like this:
$(CC) -o program_name $(OBJ) $(LINK)
but I expanded them for clarity.

Leave a Comment »