将比较操作符的重载定义/声明为库中的非成员函数

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

本文关键字:成员 函数 操作符 比较 重载 定义 声明      更新时间:2023-10-16

假设我有一个名为"Myclass"的类,对它重载比较操作符是有意义的。我需要把这个类放到一个名为"libmyclass"的库中,我想编译/链接一个名为"myprog"的程序。

根据建议,我选择将比较操作符重载为非成员函数。我选择在头文件中声明它们,并在实现文件中定义它们,但在链接步骤中找不到它们:"对' operator<(…)'的未定义引用"(见下文)。我该怎么做才能解决这个问题?

下面是文件"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

这是文件"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);};

这里是文件"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;
}

下面是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)

这是"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)

最后,这里是我使用的命令和我得到的错误:

$ 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

你的代码自相矛盾。

编译myprog.c时,operator<(Myclass const&, Myclass const&) 不能内联

myclass.cc编译时,operator<(Myclass const&, Myclass const&) 不会为链接器生成,因为您承诺它将被内联。

解决方案是删除这些函数的inline,或者将定义移动到标题中,以便它们可以真正内联。

您应该将所有inline定义移动到声明

下面的头文件中。

内联要放在。h文件

或删除.cpp文件

中的内联可能

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

比运算符形式简单。

,不需要加;