我在链接时得到以下信息:
BServer.cpp:(.text+0x58e5): undefined reference to `dbusServer::dbusServer(char*, int)'
BServer.cpp:(.text+0x58f4): undefined reference to `dbusServer::~dbusServer()'
运行 make 时的 g++ 输出为:
g++ dbusServer.o PCounter.o BServer.o -o PCounter -L./BAPI/lib -L./usr/local/lib/libdbus -lBAPIx64 -m64 -lstdc++ -lpthread -lboost_system -lboost_program_options -L./home/ben/Downloads/libdbus-3.0.3/src/ -ldbus -L/home/ben/workspace/IntegratedPCounter/src/PCounter
我/home/ben/workspace/IntegratedPCounter/src/PCounter
包括'#include "dbusServer.h'
来自BServer.cpp
然后我尝试创建一个dbusServer
in的实例BServer
,如下所示:
dbusServer modserver("192.168.0.9",5432);
这是 dbus 的头文件:
// Define the number of devices to be managed
#include <dbus.h>
#define ADDRESS_START 0
#define ADDRESS_END 7
#ifndef dbusSERVER_H_
#define dbusSERVER_H_
class dbusServer {
dbus_t *ctx;
int nb;
typedef struct regHL
{
uint16_t hibits;
uint16_t lowbits;
};
private:
void SplitCount(int); //To-do determine of the value needs to be split
void addDevice(int);
public:
dbusServer(char *, int);
~dbusServer();
int WriteDeviceCounts(int, int);
int AddDevice(int);
int resetDeviceCounts(int);
};
#endif /* dbusSERVER_H_ *
这是dbus代码:
#include <dbus.h>;
#include <errno.h>
#include "dbusServer.h"
//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
#include <syslog.h>
using namespace std;
namespace po = boost::program_options;
//alias program option namespace
int nb = 0;
dbus_t *ctx;
int rc;
int dbusServer::dbusServer(char *IPAddress, int port) {
nb = 0;
// TODO Auto-generated constructor stub
int socket;
ctx = dbus_new_tcp(IPAddress, port);
dbus_mapping_t *mb_mapping;
mb_mapping = dbus_mapping_new(500, 500, 500, 500);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
dbus_strerror(errno));
dbus_free(ctx);
return (-1);
}
socket = dbus_tcp_listen(ctx, 1);
dbus_tcp_accept(ctx, &socket);
return (0);
}
dbusServer::~dbusServer() {
// TODO Auto-generated destructor stub
dbus_mapping_free (mb_mapping);
dbus_close(ctx);
dbus_free(ctx);
}
int writeDeviceCounts(uint32_t counts, int device) {
//split the 32 bit value from the counting device
uint16_t highBits = (counts & 0xFFFFFFFF00000000) >> 16; // get the high bits
uint16_t lowBits = (counts & 0xFFFFFFFF); //get the low bits
return (-1);
}
int addDevice(int device_id) {
//increment the device counter
return(-1);
}
void removeDevice(int device_id) {
}
void resetDeviceCounts(int device_id) {
}
在第 58 页的“Safe C++”一书(Kushnir, V., O'Reilly Press, 2012)中,有一个错误的类创建示例,尤其是在编写析构函数方面。但是,更正后的实现看起来可以解决我的一些问题。
我想知道的一件事是如何避免在 C++ 中实现类时出现问题,这样这些事情就不会出现。必须有一个通用白话中的注意事项列表,可以引导人们远离问题。