Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在headerfile Arduno ethernet中声明服务器对象_C++_Arduino - Fatal编程技术网

C++ 在headerfile Arduno ethernet中声明服务器对象

C++ 在headerfile Arduno ethernet中声明服务器对象,c++,arduino,C++,Arduino,我在和Arduino一起工作,有一个ethernetshield。我已经创建了一个通信类,其中声明了一个服务器对象(来自ethernet库)。现在我想在类构造函数中初始化这个对象。对象的构造函数接受一个参数(int-portnumber) 我们所做的工作如下: CommunicationModuleTCPIP:: CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPAdress, int ServPort, bool Ser

我在和Arduino一起工作,有一个ethernetshield。我已经创建了一个通信类,其中声明了一个服务器对象(来自ethernet库)。现在我想在类构造函数中初始化这个对象。对象的构造函数接受一个参数(int-portnumber)

我们所做的工作如下:

CommunicationModuleTCPIP::  CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPAdress, int ServPort, bool Server)
:ServerObject(5000)
 {
   // Code
 }
在headerfile中,将服务器对象声明为指针(*server)

在communicator类构造函数中,我初始化对象如下:

  *ServerObject = EthernetServer(5000);
问题是ServerObject的构造函数总是需要参数,所以我不能简单地在headerfile中声明它

欢迎任何提示和帮助!提前谢谢

通信类的头文件如下所示:

class CommunicationModuleTCPIP : public CommunicationModule
{
public:

CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPServerIPAdress,     int             ServPort,     bool Server);
CommunicationModuleTCPIP();

int Connect();
void Send();
void Recieve();
void SendBuffer(char Buffer[], int Size);
void RecieveBuffer(char Buffer[], int Size);

byte Mac[];
IPAddress ServerIP;
int ServerPort;

EthernetClient Client;
EthernetServer *ServerObject;


    private:
};
    Serial.begin(9600);
delay(1000);
char Buffer[10];

Serial.println("Setting up server");

// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

*ServerObject = EthernetServer(5000);

//Mac = byte[] { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 };   //physical mac address
for (int Index = 0; Index < 6; Index++) {
    Mac[Index] = MacAdress[Index];
};

ServerIP = ServerIPAdress; // IP Adress to our Server
ServerPort = ServPort;

// initialize the ethernet device
Ethernet.begin(Mac, ServerIP, gateway, subnet);

// start listening for clients
this.ServerObject.begin();

Serial.println("Server setup");

// if an incoming client connects, there will be bytes available to read:
Client = ServerObject.available();
if (Client.connected()) {
    Serial.println("client connected");
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:

    RecieveBuffer(Buffer, 10);

    Serial.print(Buffer);
}
类构造函数如下所示:

class CommunicationModuleTCPIP : public CommunicationModule
{
public:

CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPServerIPAdress,     int             ServPort,     bool Server);
CommunicationModuleTCPIP();

int Connect();
void Send();
void Recieve();
void SendBuffer(char Buffer[], int Size);
void RecieveBuffer(char Buffer[], int Size);

byte Mac[];
IPAddress ServerIP;
int ServerPort;

EthernetClient Client;
EthernetServer *ServerObject;


    private:
};
    Serial.begin(9600);
delay(1000);
char Buffer[10];

Serial.println("Setting up server");

// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

*ServerObject = EthernetServer(5000);

//Mac = byte[] { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 };   //physical mac address
for (int Index = 0; Index < 6; Index++) {
    Mac[Index] = MacAdress[Index];
};

ServerIP = ServerIPAdress; // IP Adress to our Server
ServerPort = ServPort;

// initialize the ethernet device
Ethernet.begin(Mac, ServerIP, gateway, subnet);

// start listening for clients
this.ServerObject.begin();

Serial.println("Server setup");

// if an incoming client connects, there will be bytes available to read:
Client = ServerObject.available();
if (Client.connected()) {
    Serial.println("client connected");
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:

    RecieveBuffer(Buffer, 10);

    Serial.print(Buffer);
}

有人有想法吗?

不要将
ServerObject
声明为指针,然后使用初始值设定项列表创建实例

这是您的构造函数的外观:

CommunicationModuleTCPIP::CommunicationModuleTCPIP(/*...*/)
    :ServerObject(5000)
{
   /* code */
}
有一些关于初始化者的信息


请注意,在ARDUNO上C++是有限的。特别是您没有STL和
new
delete

为什么ServerObject是指针?您好,谢谢您的评论!它是一个指针,因为我不能在headerfile中声明ServerObject,(我想),因为这是ServerObject的构造函数,需要一个参数(portNumber)。还有别的方法吗?不要把它变成指针,而要在构造函数中使用初始化列表来创建它。@Craig谢谢你的评论!您能解释如何创建初始化列表吗?感谢您的建议,我将ServerObject声明放在headerfile(Ethernetserver ServerObject)中,并在初始化器列表中初始化ServerObject。但是现在我得到了关于eTernetServer构造函数的编译器错误。我已经把它们放在更新中了。谢谢当我重新启动我的笔记本电脑时你的建议起作用了,编译过程中没有错误。现在不知道为什么它在重新启动后而不是之前工作?