背景信息:我正在为嵌入式应用程序开发通信协议 该协议基本上需要是一个包装器,允许它移植到不同的通信方法(串行,WiFi,蓝牙等),并能够支持与不同外围设备(DAC,加速度计等)的通信和控制。
我们的想法是每个通信方法(在我的示例中为A_base
)都有自己的实现,同时具有相同的外部函数,这样,如果(例如)外围设备的代码不需要重写我们从字节串口转移到json-over-wifi
每个外设都实现了基类的派生(在我的例子中为B_base
)来处理特定于该外设的数据传输,因此每个B
必须能够从{{1}的导数调用函数不必知道使用了哪种衍生物。
当我编译以与以下相同的方式编写的代码时,我会收到错误,例如:
A_base
derivedB.cpp: In member function 'virtual void derivedB::setupData()':
然后
derivedB.cpp:xx: error: no matching function for call to 'A_base::callFromB(const char*, int&)'
baseClasses.h:xx:yy: note: candidate: virtual void A_base::callFromB()
virtual void callFromB() =0;
我是否实现了我描述错误的功能,或者根本不可能,或是否需要特定版本的C ++? 我不是一位经验丰富的C ++程序员,所以非常感谢任何帮助。
示例代码:
基础课程
candidate expects 0 arguments, 2 provided
衍生物A.
/* ==== baseClasses.h ==== */
#ifndef BASECLASSES_H
#define BASECLASSES_H
#include <list>
// forward declarations
class A_base;
class B_base;
class A_base {
protected:
std::list<B_base*> listOfBs;
public:
A_base();
void addB(B_base*);
virtual void callFromB() =0;
virtual void alsoCallFromB(short*, int) =0;
virtual void alsoCallFromB(int*, int) =0;
virtual void alsoCallFromB(float*, int) =0; // overloaded
};
class B_base {
protected:
int someCommonInt;
A_base* A;
public:
B_base(int, A_base&);
virtual void setupData() =0;
};
#endif
/* ==== baseClasses.cpp ==== */
#include "baseClasses.h"
A_base::A_base() {}
void A_base::addB(B_base* b { listOfBs.push_back(b); }
B_base::B_base(int i, A_base& ref_A) {
someCommonInt = i;
A = &ref_A;
A->addB(this);
}
B的衍生物
/* ==== A_derived.h ==== */
#include "baseClasses.h"
class derivedA : public A_base {
public:
virtual void callFromB(const char*, int);
virtual void alsoCallFromB(short*, int);
virtual void alsoCallFromB(int*, int);
virtual void alsoCallFromB(float*, int); // overloaded
};
/* ==== A_derived.cpp ==== */
#include "A_derived.h"
void derivedA::callFromB(const char* msg, int foo) {
// do something with msg and foo
}
void derivedA::alsoCallFromB(short* data, int len=1) { // overloaded derived function with default value
// do something with short int data
}
void derivedA::alsoCallFromB(int* data, int len=1) { // overloaded derived function with default value
// do something with int data
}
void derivedA::alsoCallFromB(float* data, int len=1) { // overloaded derived function with default value
// do something with float data
}
主要计划
/* ==== B_derived.h ==== */
#include "baseClasses.h"
class derivedB : public B_base {
private:
int* intData;
float* floatData;
int arraySize;
public:
virtual void setupData(int*, float*, int);
void callAWithDataArrays();
void callAWithSingleValue(int);
};
/* ==== B_derived.cpp ==== */
#include "B_derived.h"
void derivedB::setupData(int* iPtr, float* fPtr, int size) {
intData = iPtr;
floatData = fPtr;
arraySize = size;
A->callFromB("B setup done.\n", 0);
}
void derivedB::callAWithDataArrays() {
A->alsoCallFromB(intData, arraySize);
A->alsoCallFromB(floatData, arraySize);
}
void derivedB::callAWithSingleValue(int idx) {
A->alsoCallFromB(intData[idx]);
A->alsoCallFromB(floatData[idx]);
}
答案 0 :(得分:2)
你有一个纯粹的虚函数virtual void callFromB() =0;
,它在派生类中没有被覆盖,即:
virtual void callFromB(const char*, int); != virtual void callFromB()
您可以更改A_base::callFromB() to A_base::callFromB(const char*, int)