簡體   English   中英

在庫中使用非成員函數來定義/聲明比較運算符的重載?

[英]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

您應該將所有inline定義移動到聲明下面的頭文件中

你的代碼與自己相矛盾。

編譯myprog.c無法內聯 operator<(Myclass const&, Myclass const&) 這個定義無處可見。

編譯myclass.cc不會為鏈接器生成 operator<(Myclass const&, Myclass const&) 因為您承諾它將被內聯。

解決方案是刪除這些函數的inline ,或將定義移動到標題,以便它們可以真正內聯。

內聯將放在.h文件中

或刪除.cpp文件中的內聯

可能

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

比運算符形式更簡單。

而且不需要放; 在函數定義之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM