将比较操作符的重载定义/声明为库中的非成员函数
Where to define/declare overload of comparison operators as non-member functions in a library?
假设我有一个名为"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);}
比运算符形式简单。
,不需要加;
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法