Undefined reference to public class method?

  • Follow


For the following classes, just assume I have the appropriate
constructors and destructors, please...
In my Data.h file I have:

class NumData
{
    ...
    ...
};

Class NumList
{
    ...
    ...
    public:
       NumData FindByName(const string &name) const;
};

In my Data.c file I have:

#include "Data.h"

NumData NumList::FindByName(const string &name) const
{
    return NumData(0);
}

In my Muvie.c file I have:

#include "Data.h"

int main(int argc, char **argv)
{
    NumList A;

    A.FindByName("Test");

    return 0;
}

When I compile everything goes fine, but when the linker tries to do
its thing, I get the following error:

Linking MUVIE
../objs/Muvie.o(.text+0x5cc): In function `main':
/home/muvie/muvie/src/Muvie.c:32: undefined reference to
`NumList::FindByName(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&) const'
collect2: ld returned 1 exit status

What the heck am I doing wrong? I have the header file #included in my
source .c file. I've even tried make clean and make depend. I also
tried using NumList A(); and NumList A("Test"); but I get the same
result.

The method I'm trying to call is public, so it shouldn't give me any
problems.

Any suggestions would help.

Sincerely,

Gordon E.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply gordone (2) 2/14/2005 11:08:52 AM

>From the little code you give I see twoissues:
1 - ' class'  should have a lowercase 'c'
2 - you're using an object of type string without defining it, add '
#include <string>'  and ' using namespace std;'  to your header abd it
should comile


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply wittempj 2/14/2005 8:47:35 PM


I think you better post your make script also here.

But, I guess you did NOT link Muvie.o with (possibly) Data.o.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Joseph 2/14/2005 8:48:59 PM

OK, forget all that, I was able to figure out why it wasn't working.
Now I'm having a different problem.

../objs/Data.o(.text+0x0): In function `operator+(double const&, NumData
const&)':
/home/muvie/muvie/src/Data.h:257: multiple definition of
`operator+(double const&, NumData const&)'
../objs/Muvie.o(.text+0x0):/home/muvie/muvie/src/Data.h:257: first
defined here
collect2: ld returned 1 exit status
make: *** [Muvie] Error 1

I've double and triple checked. I only have one definition of this
method. I defined this method so that I could get a NumData object by
adding a double and another NumData object (3.215 + NumData(53.2221))

In my .h file I have:

#ifndef Data_h
#define Data_h

NumData operator+(const double &lhs, const NumData &rhs)
{
   return NumData(lhs + rhs.GetValue());
}

#endif

This should prevent the method from being redefiend, but it doesn't for
some reason.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Gordon 2/14/2005 8:50:59 PM

> Linking MUVIE
> ./objs/Muvie.o(.text+0x5cc): In function `main':
> /home/muvie/muvie/src/Muvie.c:32: undefined reference to
> `NumList::FindByName(std::basic_string<char, std::char_traits<char>,
> std::allocator<char> > const&) const'
> collect2: ld returned 1 exit status
>
> What the heck am I doing wrong?

The code compiles successfully because you have declared 
NumList::FindByName. But the linker cannot find the  implementation of 
NumList::FindByName.  You may have forgotten to include the c/cpp file in 
your build, or the declaration and definition are not identical. 



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Kurt 2/14/2005 9:12:50 PM

> This should prevent the method from being redefiend, but it doesn't
for
some reason.

You must understand how preprocessor works. '#include' directive just
embed your header file (Data.h) into source code (Movie.c/Data.c).
Therefore, you actually DID redefine NumData::operator+() both Movie.o
and Data.o.

#ifndef/#define protection only prevents the method from being
redefined in one compilation unit. (Say, in Movie.o)


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Joseph 2/15/2005 9:59:38 AM

Gordon Ellsworth wrote:
> ./objs/Data.o(.text+0x0): In function `operator+(double const&,
NumData
> const&)':
> /home/muvie/muvie/src/Data.h:257: multiple definition of
> `operator+(double const&, NumData const&)'
> ./objs/Muvie.o(.text+0x0):/home/muvie/muvie/src/Data.h:257: first
> defined here
> collect2: ld returned 1 exit status
> make: *** [Muvie] Error 1
>
> In my .h file I have:
>
> #ifndef Data_h
> #define Data_h
>
> NumData operator+(const double &lhs, const NumData &rhs)
> {
>    return NumData(lhs + rhs.GetValue());
> }
>
> #endif

Short answer: Use the keyword inline:

inline NumData operator+(const double &lhs, const NumData &rhs)
{
   return NumData(lhs + rhs.GetValue());
}

Long answer: Since you defined this function in a header file, it
gets defined in every module that #includes it directly or indirectly.
This is required for inline functions, but prohibited for functions
that are not inline.

Fortunately, your operator+ function is so short and trivial (return
with a relatively simple expression) that it is worthy of an exception
to the "don't use inline unless profiling says you should" rule. So
go ahead and make the function inline, and you can leave it where it
is.

If you had the same problem with a longer function, you should declare
it in the .h file:

    inline NumData operator+(const double &lhs, const NumData &rhs);

And then actually define it in a module (probably Data.c, or
Data.cpp, or whatever).


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Allan 2/16/2005 11:01:05 PM

6 Replies
765 Views

(page loaded in 1.267 seconds)

Similiar Articles:













7/24/2012 3:20:27 PM


Reply: