-2

I have this piece of code:

std::map< int, std::pair<int, int> > m;
for ( std::vector<Pass*>::const_iterator passIt = it->GetPasses().begin(); passIt != it->GetPasses().end(); ++passIt )
{
    m.insert( std::make_pair((*passIt)->GetType(), (*passIt)->GetAgeRange()) );
}
--> ages.push_back( new std::istringstream(SerializeAges(m)) ); <--

The marked line yields this error:

undefined reference to `SerializeAges(std::map<int, std::pair<int, int>, std::less<int>, 
 std::allocator<std::pair<int const, std::pair<int, int> > > > const&)'

Declaration:

 static std::string SerializeAges(const std::map< int, std::pair<int, int> > &ageMap);

Definition:

inline std::string SerializeAges(const std::map< int, std::pair<int, int> > &ageMap)
{       
}

Signatures of other called functions:

std::pair<int, int> GetAgeRange() const;

I have no idea what goes wrong here.

EDIT: sorry, Type is an enum, so what GetType() returns is implicitly converted to an int.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Innkeeper
  • 663
  • 1
  • 10
  • 22
  • "Undefined reference" implies that the function is missing its definition. Have you defined it? Are you linking with whichever file contains the definition? – Mike Seymour Feb 17 '14 at 13:37
  • @MikeSeymour yes, it is defined, and I'm linking it. – Innkeeper Feb 17 '14 at 13:38
  • 1
    Well the compiler disagrees. Perhaps you could show us the definition (leaving out the function body, since that shouldn't matter), so we can check that it matches the declaration? Also, is it a member function? – Mike Seymour Feb 17 '14 at 13:41
  • @MikeSeymour I added the definition without the body. It's a medthod of a "static class". – Innkeeper Feb 17 '14 at 13:46
  • @Simple May I ask why? I have ~20 more methods declared and defined similarly in this class, and everything else works great. Same error after removing, by the way. – Innkeeper Feb 17 '14 at 13:48
  • Is the static member function declared in a header and the definition in a .cpp file? In that case you need to remove the `inline`. If this is the case for other functions as well I assume that they are only being used in a single .cpp file. – Simple Feb 17 '14 at 13:48
  • Inline functions must be defined in every file that calls them. If you just define it in one source file, then it can only be used in that file. So you can either remove `inline` and leave a single definition in a source file, or move the `inline` definition into a header included wherever the function is called. – Mike Seymour Feb 17 '14 at 13:50
  • If it's a member (static or otherwise), then you'll have to qualify its name (e.g. `ClassName::SerializeAges) in the definition. – Mike Seymour Feb 17 '14 at 13:51
  • @MikeSeymour The latter is the case. The header in which the function is declared / defined inline, is indeed included at the place of calling. – Innkeeper Feb 17 '14 at 13:51
  • The declaration and definition are both in the header file? – Simple Feb 17 '14 at 13:53
  • @MikeSeymour Yes. I knew this was something really stupid. Thanks. `Serializer::` is missing. – Innkeeper Feb 17 '14 at 13:53

1 Answers1

2

From the comments, it seems that it's a class member. In that case, the definition will need to use the qualified name:

inline std::string WhateverTheClassIsCalled::SerializeAges(const std::map< int, std::pair<int, int> > &ageMap)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^

Without that, the definition declares a separate non-member function with the same name, and the member remains undefined.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644