4
votes

Ok, I'm new to c++ so I'm trying to understand what information I can get from the error message.

Here is the error message

Undefined symbols for architecture x86_64:
  "PieceClothing::PieceClothing(int)", referenced from:
      ClothesInventory::getPieceOfClothing(long)   in ClothesInventory.o
      ClothesInventory::insertIntocloset(std::basic_string, std::allocator >)in ClothesInventory.o
  "PieceClothing::PieceClothing()", referenced from:
      ClothesInventory::ClothesInventory()in ClothesInventory.o
      ClothesInventory::ClothesInventory(std::basic_string, std::allocator >)in ClothesInventory.o
      std::map, std::allocator > >::operator[](long const&)in ClothesInventory.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Here is what I understand:
- There are two errors;
- One that has to do with getPieceOfClothing and insertIntocloset;
- Other in the the constructors maybe about a map and/or iterator I have there.

Just to clarify, I'm not attaching the code because the point of the question is to understand all the information I can get just from the message.

Thanks for any help.

2
'for architecture' - are you working with a toolchain that might link more than one architecture at once, e.g. Mac linking x64 and PPC? I think that's a red herring - it's just telling you that it can't find the implementation of the PieceClothing constructors - have you definitely implemented those and are you definitely linking those in?Rup

2 Answers

5
votes

The errors are actually about the constructors:

PieceClothing::PieceClothing(int)
PieceClothing::PieceClothing()

and they're saying no symbols were found for them. This is usually a sign of either:

  • they weren't implemented
  • they're implemented but the file in which the implementation lies is not compiled
  • you're referencing them from a different module that doesn't link with the module that defines them

The other details in the error list just state where the constructors are called. For example, if you have:

ClothesInventory::getPieceOfClothing(long)
{
   PieceClothing p;
}

you're referencing the constructor because you attempt to create an object of that type.

How this works can be broken down in 2 parts:

1) The compiler checks the header file that define the class and sees whether a default constructor is available. It find the constructor so it gives its ok.

2) The linker comes into action. It looks for symbols that match your calls in object files and referenced libraries. This is where it goes wrong for you.

0
votes

The message is telling you that it cannot find a definition for the declared constructors PieceClothing::PieceClothing(int) and PieceClothing::PieceClothing() so you need to check whether you've written them and, if so, whether the object file that contains them forms part of the link.

If your linker output is verbose, it should show you which object files are being linked.