0

我是 C++ 新手,但不会编程,最近开始为 AVR 微控制器编写一个库。我的库的头文件(Codex.h)看起来像这样..

#ifndef Codex_h
#define Codex_h
#include "Arduino.h"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

class Codex
{
public:
    void hostIP(int a ,int b ,int c,int d);
    void start(String ID);
    void digitalReadOSC();
    void analogReadOSC();
    void digitalRead();
    void analogRead();
    void receive();
private:
    EthernetUDP _Udp;
    int _pin,_a,_b;
    int _sensorData[52];
    String _nID,_sID,_pID,_snID,_lID,_payloadlen,_payload,_packet;
    char _packetBuffer[25];
    IPAddress _coreIP(000,000,0,00);
};
#endif

现在我再说一遍,我是 C++ 新手,所以我认为我犯了一个简单的错误,但我的编译器在 IPAddress 类型和处理 EthernetUDP 实例创建方面存在问题。IPAddress 是一个来自 Ethernet.h 库的函数。当我尝试将我的库包含在项目中时,这是我的编译器吐出的内容。

In file included from sketch_aug17b.ino:1:
C:\Program Files (x86)\Arduino\libraries\Codex/Codex.h:19: error: 'EthernetUDP' 
does not name a type
C:\Program Files (x86)\Arduino\libraries\Codex/Codex.h:24: error: 'IPAddress' 
does not name a type

提前感谢您提供的任何帮助,即使只是告诉我去阅读 C++ 书 :)。

4

2 回答 2

0

As has been mentioned by @WhozCraig there is no problem with the classes names, nor the classes are inside a scope. So my best guess is that you are trying to instantiate a member variable (_coreIP) outside a method. You should fist declare the member variable on the class declaration, and then instantiate it on the constructor of the class.

So change this.

class Codex
{
...
private:
    ...
    IPAddress _coreIP(000,000,0,00);
};

for this

class Codex
{
public:
    ...
    Codex();
private:
    ...
    IPAddress _coreIP;
};

Codex::Codex() :
  _coreIP(000,000,0,00)
{
    ...
}   

You can instantiate member variables directly on the constructor of the class, using the : operator as is showed here.

By the way, this is my first answer, so I hope I had done it well :)

Greetings

于 2013-08-17T02:30:53.647 回答
0

我的代码现在正在运行,我不确定这是一个真正的错误还是发生了其他事情,但是在使用我的类的工作程序中包含我的库中所需的外部头文件后,我不再遇到任何问题,如果有人可以向我解释为什么会这样,那就太好了。我的新标头代码和程序代码。

//(Codex.h)
#ifndef Codex_h
#define Codex_h
#include "Arduino.h"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

class Codex
{
public:
    void hostIP(int a ,int b ,int c,int d);
    void start(String ID);
    void digitalReadOSC();
    void analogReadOSC();
    void digitalRead();
    void analogRead();
    void receive();
private:
    int _pin,_a,_b;
    int _sensorData[52];
    String _nID,_sID,_ndID,_pID,_snID,_lID,_payloadlen,_payload,_packet;
    char _packetBuffer[25];
    EthernetUDP _Udp;
    IPAddress _coreIP;
};
#endif

这是我的程序,尽管如果它们已经在我的 .h 和 .cpp 文件中声明,我将不得不在我的程序中第二次声明我需要的库,这似乎是不寻常的。

#include <Codex.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

Codex codex;
void setup()
{
codex.start("01");  
}
void loop()
{ 
}
于 2013-08-17T14:12:11.960 回答