这是我在eclipse IDE C / C ++中的类 IRSender.h:
#ifndef IRSender_h
#define IRSender_h
#include <stdint.h>
class IRSender
{
public:
IRSender(uint8_t pin); // Cannot create generic IRSender instances
virtual ~IRSender(){};
public:
virtual void setFrequency(int frequency);
void sendIRbyte(uint8_t sendByte, int bitMarkLength, int zeroSpaceLength, int oneSpaceLength);
uint8_t bitReverse(uint8_t x);
virtual void space(int spaceLength);
virtual void mark(int markLength);
public:
uint8_t _pin;
uint8_t pin1;
};
class IRSenderPWM : public IRSender
{
public:
IRSenderPWM(uint8_t pin);
void setFrequency(int frequency);
void space(int spaceLength);
void mark(int markLength);
};
IRSenderPWM(uint8_t pin);没有身体定义 IRSender.cpp:
#include "IRSender.h"
// The generic functions of the abstract IRSender class
IRSender::IRSender(uint8_t pin)
{
_pin = pin;
}
// Send a uint8_t (8 bits) over IR
void IRSender::sendIRbyte(uint8_t sendByte, int bitMarkLength, int zeroSpaceLength, int oneSpaceLength)
{
for (int i=0; i<8 ; i++)
{
if (sendByte & 0x01)
{
mark(bitMarkLength);
space(oneSpaceLength);
}
else
{
mark(bitMarkLength);
space(zeroSpaceLength);
}
sendByte >>= 1;
}
}
// The Carrier IR protocol has the bits in a reverse order (compared to the other heatpumps)
// See http://www.nrtm.org/index.php/2013/07/25/reverse-bits-in-a-uint8_t/
uint8_t IRSender::bitReverse(uint8_t x)
{
// 01010101 | 10101010
x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
// 00110011 | 11001100
x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
// 00001111 | 11110000
x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
return x;
}
// Definitions of virtual functions
void IRSender::setFrequency(int) {};
void IRSender::space(int) {};
void IRSender::mark(int) {};
当我想在main中调用构造函数IRSenderPWM时,我收到此错误: make:*** [main]错误1
主:
#include "IRSender.h"
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int main(int argv, char** argc) {
uint8_t pin =9;
//IRSenderPWM irSender=IRSenderPWM(9);
IRSenderPWM irSender(pin); //this line make an error
return 0;
}
为什么我不能像这样定义和使用构造函数,我还在属性中添加-std = c ++ 11&gt; c / c ++ build&gt; setting&gt;工具设置&gt; GCC c ++编译器&gt;杂项但不做不同,我该怎么做才能解决这个错误?
答案 0 :(得分:0)
在IRSender.cpp中的代码下方添加,然后您可以传递构建。
IRSenderPWM::IRSenderPWM(uint8_t pin)
:IRSender(pin)
{
}
void IRSenderPWM::setFrequency(int frequency)
{
}
void IRSenderPWM::space(int spaceLength)
{
}
void IRSenderPWM::mark(int markLength)
{
}
因为你在main中声明了IRSenderPWM对象,你必须实现它的构造函数,否则它将无法工作,它来自c ++标准