我认为这与 C++ 链接器错误有关,而不是与节俭有关。我对 thrift 文件进行了更改并重新生成了 cpp 和 java 类。进行此更改后,我开始在 cpp 中收到链接器错误。这是错误
未定义的符号:
"com::XXXX::thrift::employee::SavingsInfo::operator<(com::XXXX::thrift::employee::SavingsInfo const&) const", referenced from:
std::less::operator()(com::XXXX::thrift::employee::SavingsInfo const&, com::XXXX::thrift::employee::SavingsInfo const&) constin employee_types.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [ThriftCPPSamples] Error 1
我在 thrift 文件中添加了 SavingsInfo 类型,这是我所做的更改。我将文档中提到的所有选项都提供给 g++。我给了-I/usr/local/include/thrift
,
-I/path-to-boost
, -L/path-to-boost-lib
, -lthrift
. 但是在更改之后,我开始收到上述链接器错误。我无法理解这其中的原因。该错误指向thrift 生成的某些内容。错误的原因可能是什么?
这个错误是关于“operator <”的,所以我只发布与之相关的代码。完整的代码可从最后提供的两个链接中获得。
员工类型.h
class SavingsInfo {
public:
std::string name;
double amount;
bool operator == (const SavingsInfo & rhs) const { /*...*/ }
bool operator != (const SavingsInfo &rhs) const { return !(*this == rhs); }
bool operator < (const SavingsInfo & ) const;
uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const;
};
class EmployeeInfo {
public:
int32_t id;
std::string name;
double salary;
bool contract;
std::set<std::string> dependents;
std::set<SavingsInfo> savings;
bool operator == (const EmployeeInfo & rhs) const { /*...*/ }
bool operator != (const EmployeeInfo &rhs) const { return !(*this == rhs); }
bool operator < (const EmployeeInfo & ) const;
uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const;
};
员工类型.cpp
uint32_t EmployeeInfo::read(::apache::thrift::protocol::TProtocol* iprot) {
uint32_t xfer = 0;
std::string fname;
::apache::thrift::protocol::TType ftype;
int16_t fid;
xfer += iprot->readStructBegin(fname);
using ::apache::thrift::protocol::TProtocolException;
while (true)
{
xfer += iprot->readFieldBegin(fname, ftype, fid);
if (ftype == ::apache::thrift::protocol::T_STOP) {
break;
}
switch (fid)
{
// removed the case statements which deal with reading other fields
case 6:
if (ftype == ::apache::thrift::protocol::T_SET) {
{
this->savings.clear();
uint32_t _size6;
::apache::thrift::protocol::TType _etype9;
iprot->readSetBegin(_etype9, _size6);
uint32_t _i10;
for (_i10 = 0; _i10 < _size6; ++_i10)
{
SavingsInfo _elem11;
xfer += _elem11.read(iprot);
this->savings.insert(_elem11);
}
iprot->readSetEnd();
}
this->__isset.savings = true;
} else {
xfer += iprot->skip(ftype);
}
break;
default:
xfer += iprot->skip(ftype);
break;
}
xfer += iprot->readFieldEnd();
}
xfer += iprot->readStructEnd();
return xfer;
}
uint32_t EmployeeInfo::write(::apache::thrift::protocol::TProtocol* oprot) const {
uint32_t xfer = 0;
// removed the write statements for other fields
xfer += oprot->writeStructBegin("EmployeeInfo");
xfer += oprot->writeFieldBegin("savings", ::apache::thrift::protocol::T_SET, 6);
{
xfer += oprot->writeSetBegin(::apache::thrift::protocol::T_STRUCT,
this->savings.size());
std::set<SavingsInfo> ::const_iterator _iter13;
for (_iter13 = this->savings.begin(); _iter13 != this->savings.end(); ++_iter13)
{
xfer += (*_iter13).write(oprot);
}
xfer += oprot->writeSetEnd();
}
xfer += oprot->writeFieldEnd();
xfer += oprot->writeFieldStop();
xfer += oprot->writeStructEnd();
return xfer;
}
我尝试过的其他几件事:
- 如果我使用 SavingsInfo,而不是 set<SavingsInfo>,它可以正常工作。
- 在employee_types.h 中评论了“operator <”,因为我不知道它在哪里被使用。我收到以下构建错误
Building file: ../src/employee_types.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/thrift -I/Users/raghava/Software/Boost_C++_Library/boost_1_43_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/employee_types.d" -MT"src/employee_types.d" -o"src/employee_types.o" "../src/employee_types.cpp"
/usr/include/c++/4.2.1/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = com::xxxx::thrift::employee::SavingsInfo]':
/usr/include/c++/4.2.1/bits/stl_tree.h:982: instantiated from 'std::pair::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = com::xxxx::thrift::employee::SavingsInfo, _Val = com::xxxx::thrift::employee::SavingsInfo, _KeyOfValue = std::_Identity, _Compare = std::less, _Alloc = std::allocator]'
/usr/include/c++/4.2.1/bits/stl_set.h:307: instantiated from 'std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = com::xxxx::thrift::employee::SavingsInfo, _Compare = std::less, _Alloc = std::allocator]'
../src/employee_types.cpp:163: instantiated from here
/usr/include/c++/4.2.1/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y'
make: *** [src/employee_types.o] Error 1
这两个文件的完整源代码在下面的链接中给出。另一个链接在评论中(在我获得 10 分的声誉之前我不能发布它)。
employee_types.cpp -- http://pastebin.com/7dLtstCK
employee_types.h -- http://pastebin.com/JGzE8V6J
谢谢你。
问候,拉格哈瓦。