简体   繁体   中英

Where to define/declare overload of comparison operators as non-member functions in a library?

Let's imagine that I have a class named "Myclass" for which it makes sense to overload the comparison operators. I need to put this class into a library named "libmyclass", against which I want to compile/link a program named "myprog".

Following advice , I chose to overload the comparison operators as non-member functions. I chose to declare them in the header file and define them in the implementation file, but they are not found at the linking step: "undefined reference to `operator<(...)'" (see below). What should I do to fix this?

Here is the file "myclass.h":

#ifndef MYCLASS_H
#define MYCLASS_H

#include <cstdlib>

#include <string>
using namespace std;

class Myclass {
private:
  size_t x_;
public:
  Myclass(void);
  Myclass(const size_t & x);
  const size_t & GetValue(void) const { return x_; };
};

bool operator==(const Myclass& lhs, const Myclass& rhs);
bool operator!=(const Myclass& lhs, const Myclass& rhs);
bool operator< (const Myclass& lhs, const Myclass& rhs);
bool operator> (const Myclass& lhs, const Myclass& rhs);
bool operator<=(const Myclass& lhs, const Myclass& rhs);
bool operator>=(const Myclass& lhs, const Myclass& rhs);

#endif //MYCLASS_H

Here is the file "myclass.cc":

#include "myclass.h"

Myclass::Myclass(void)
{
}

Myclass::Myclass(const size_t & x)
{
  x_ = x;
}

inline bool operator==(const Myclass& lhs, const Myclass& rhs)
{
  return(lhs.GetValue() == rhs.GetValue());
}

inline bool operator< (const Myclass& lhs, const Myclass& rhs)
{
  return(lhs.GetValue() < rhs.GetValue());
}

inline bool operator!=(const Myclass& lhs, const Myclass& rhs){return !operator==(lhs,rhs);};
inline bool operator> (const Myclass& lhs, const Myclass& rhs){return  operator< (rhs,lhs);};
inline bool operator<=(const Myclass& lhs, const Myclass& rhs){return !operator> (lhs,rhs);};
inline bool operator>=(const Myclass& lhs, const Myclass& rhs){return !operator< (lhs,rhs);};

Here is the file "myprog.c":

#include <cstdlib>

#include <iostream>
using namespace std;

#include "myclass.h"

int main (int argc, char ** argv)
{
  Myclass * pt_obj1 = new Myclass(1);
  Myclass * pt_obj2 = new Myclass(2);
  if (*pt_obj1 < *pt_obj2)
    cout << "obj1 < obj2" << endl;
  else
    cout << "obj1 >= obj2" << endl;
  delete pt_obj1;
  delete pt_obj2;
  return EXIT_SUCCESS;
}

Here is the file "Makefile_lib":

PROJECT=libmyclass.a
SOURCES=myclass.cc
CC=g++
CFLAGS=-Wall -Wextra -g

OBJECTS=$(SOURCES:.cc=.o)

all: $(PROJECT)

$(PROJECT): $(OBJECTS)
            ar -cvq $(PROJECT) $(OBJECTS)

.cc.o:
        $(CC) $(CFLAGS) -c $< -o $@

clean:
        rm -f $(OBJECTS) $(PROJECT)

Here is the file "Makefile_exe":

PROJECT=myprog
SOURCES=myprog.cc
LIB=libmyclass.a
CC=g++
CFLAGS=-Wall -Wextra -g

OBJECTS=$(SOURCES:.cc=.o)

all: $(PROJECT)

$(PROJECT): $(OBJECTS) $(LIB)
            $(CC) $(OBJECTS) -L. -lmyclass

.cc.o:
        $(CC) $(CFLAGS) -c $< -o $@

clean:
        rm -f $(OBJECTS) $(PROJECT)

Finally, here are the commands I used and error I got:

$ make -f Makefile_lib clean
rm -f myclass.o libmyclass.a
$ make -f Makefile_lib
g++ -Wall -Wextra -g -c myclass.cc -o myclass.o
ar -cvq libmyclass.a myclass.o
a - myclass.o
$ make -f Makefile_exe clean
rm -f myprog.o myprog
$ make -f Makefile_exe
g++ -Wall -Wextra -g -c myprog.cc -o myprog.o
myprog.cc:8: warning: unused parameter ‘argc’
myprog.cc:8: warning: unused parameter ‘argv’
g++ myprog.o -L. -lmyclass
myprog.o: In function `main':
/home/me/src/myprog.cc:12: undefined reference to `operator<(Myclass const&, Myclass const&)'
collect2: ld returned 1 exit status
make: *** [myprog] Error 1

您应该将所有inline定义移动到声明下面的头文件中

Your code contradicts itself.

When myprog.c is compiled, operator<(Myclass const&, Myclass const&) can't be inlined . The definition is nowhere to be seen.

When myclass.cc is compiled, operator<(Myclass const&, Myclass const&) isn't generated for the linker because you're promising that it will be inlined.

The solution is to either remove the inline for those functions, or move the definitions to a header so they may truly be inlined.

The inline are to be put in the .h file

or remove the inline in the .cpp file

and probably

inline bool operator!=(const Myclass& lhs, const Myclass& rhs){return !(lhs==rhs);}

is simpler than the operator form.

and no need to put an ; after a function definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM