リンク中に次のようになります。
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) {
}
本「Safe C++」(Kushnir, V.,O'Reilly Press, 2012) の p.58 には、特に記述子のコーディングに関して、不適切なクラス作成の例があります。ただし、修正された実装は、私の問題のいくつかを解決するように見えます。
私が疑問に思っていることの 1 つは、これらの問題が発生しないように、C++ でクラスを実装する際の問題を回避する方法です。問題から人を遠ざけることができる一般的な言葉ですべきこととすべきでないことのリストがなければなりません。