我有三个类Node,Pattern和Motor,它们都在同一个目录中 在Pattern类中我定义了一个Node类型的函数,但程序没有编译,请你帮忙吗?
以下是所有文件:
Node.h:
#ifndef Node_h
#define Node_h
#include "Arduino.h"
#include "Motor.h"
#include "Pattern.h"
#include "QueueArray.h"
class Node
{
public:
Node();
~Node();
int amplitude;
int duration;
void setPatternNode(int amplitude,int duration);
};
#endif
节点.cpp:
#include "Arduino.h"
#include "Node.h"
#include "Motor.h"
#include "Pattern.h"
#include "QueueArray.h"
Node::Node(){}
Node::~Node(){}
void Node::setPatternNode(int amplitude,int duration){
Node.amplitude=amplitude;
Node.duration=duration;
}
Pattern.h
#ifndef Pattern_h
#define Pattern_h
#include "Arduino.h"
#include "Node.h"
#include "QueueArray.h"
#include "Motor.h"
class Pattern
{
public:
Pattern();
~Pattern();
Node setPattern(int amplitude[3],int duration[3]);
};
#endif
Pattern.cpp:
#include "Arduino.h"
#include "Pattern.h"
#include "Node.h"
#include "QueueArray.h"
#include "Motor.h"
Pattern::Pattern(){}
Pattern::~Pattern(){}
Node Pattern::setPattern(int amplitude[3],int duration[3]){
QueueArray <Node> pattern;
for (int i=0;i<3;i++){
Node node;
node.setPatternNode(amplitude[i],duration[i]);
pattern.enqueue(pattern);
}
return pattern;
}
Motor.h:
#ifndef Motor_h
#define Motor_h
#include "Arduino.h"
#include "QueueArray.h"
#include "Node.h"
#include "Pattern.h"
class Motor
{
public:
Motor(int pin);
~Motor();
void runMotor();
int _pin;
bool isMotorActive;
void motorSetPattern(Node p);
Node currentPattern;
};
#endif
Motor.cpp:
#include "Arduino.h"
#include "Motor.h"
#include "QueueArray.h"
#include "Node.h"
#include "Pattern.h"
Motor::Motor(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
Motor::~Motor(){}
void Motor::runMotor(){
if(isMotorActive){
pattern.dequeue();
if(patternStartTime < currentPattern.duration){
analogWrite(_pin,currentPattern.amplitute);
} else{
pattern.dequeue();
if(pattern.isEmpty()){
isMotorActive = false;
currentPattern = null;
}
}
}
}
void Motor::motorSetPattern(Node p){
currentPattern = p;
isMotorActive = true;
unsigned long patternStartTime = millis();
}
草图:
#include <Motor.h>
#include <Node.h>
#include <Pattern.h>
#include <QueueArray.h>
Motor vibmotor(13);
Node node;
Pattern pattern;
int amplitude[3] = {5,2,0};
int duration[3] = {1000,2000,1000};
void setup()
{
}
void loop()
{
pattern.setPattern(amplitude,duration);
vibmotor.runMotor();
}
错误:
In file included from C:\Users\Yacine\Documents\Arduino\libraries\Motor/Node.h:6,
from C:\Users\Yacine\Documents\Arduino\libraries\Motor/Motor.h:6,
from mc_ino.ino:1:
C:\Users\Yacine\Documents\Arduino\libraries\Motor/Pattern.h:14: error: 'Node' does not name a type
mc_ino.ino: In function 'void loop()':
mc_ino:21: error: 'class Pattern' has no member named 'setPattern'
答案 0 :(得分:3)
您的#includes中至少有一个循环依赖项。
您的 Node.h 文件包含 Motor.h ,它指的是Node
。当Node.h包含Motor.h时,Motor.h在定义之前引用Node。
如果您来自Java,您可能会想到#include
就像Java导入一样。但是,#include不会导入类型名称。在预处理期间,#include将替换为#include&#39; d文件的内容。圆形#include需要更多关注。
你可以做的一件事是在motor.h中的类的前向声明:
class Node;
但是,这仅适用于您使用指针或类的引用,因此您需要更改此方法的签名:
#include "Node.h"
class Node;
class Motor
{
public:
...
void motorSetPattern( const Node & p);
...
};