错误:“ const char * HTTP REQUESTS :: certificate”不是“类HTTP REQUESTS”的静态成员

Here's a general question I hope you can explain me. In case why you're wondering why I use cstring it's because the Arduino doesn't support for exampe std by default. However, my problem is the error

错误:“ const char * HTTP REQUESTS :: certificate”不是“类HTTP REQUESTS”的静态成员
#include <cstring>

char root_ca_sherz[128];
unsigned long now();


class HTTPREQUESTS {
  public:
    HTTPREQUESTS(char const strAddress[64] = "defaultAddress", char const strParameters [256] = "defaultParameters", bool bSendImmediately = false, char const *cert = root_ca_sherz);

    ~HTTPREQUESTS();

    bool sendRequest();

    char const *certificate;
    char parameters[256];
    char device[30];
    char address[64];
    unsigned long timestamp;
    int sendAttempts;
    bool sendImmediately;
    unsigned long lastSendAttempt;
};




HTTPREQUESTS::HTTPREQUESTS(char const strAddress[64], char const strParameters [256], bool bSendImmediately, char const *cert) {
  if(strcmp(address, "defaultAddress") == 0) {
    strcpy(address, strAddress);
    strcpy(parameters, strParameters);
    timestamp = now();
    certificate = cert;
  }
}

char const *HTTPREQUESTS::certificate="";

char HTTPREQUESTS::parameters[256]="defaultParameters";
char HTTPREQUESTS::device[30];
char HTTPREQUESTS::address[64] = "defaultAddress";
unsigned long HTTPREQUESTS::timestamp=0;
int HTTPREQUESTS::sendAttempts=0;
bool HTTPREQUESTS::sendImmediately = false;
unsigned long HTTPREQUESTS::lastSendAttempt=0;


int main()
{

}

实际上,内容分为两个文件(.h,.cpp),但是出于测试目的,我将它们合并在main.cpp文件中。

I don't understand why I should declare the member variables as static because I basicly just don't want it. First because the variables should not be available without initialization and second they will be different for every object after initialization. In my understanding these two functionalities result from setting a member as static - please correct me if I'm wrong, I'm very new to C++.

The objects from this class are instantiated temporary (by new) and deleted after usage.

So what do I need to do to use the code as intended without the static keyword?

评论
  • 笑伴孤单
    笑伴孤单 回复

    这些类成员已在类中声明。

    您可能想要这样:

    class HTTPREQUESTS {
    public:
      HTTPREQUESTS(char const strAddress[64] = "defaultAddress", char const strParameters[256] = "defaultParameters", bool bSendImmediately = false, char const* cert = root_ca_sherz);
    
      ~HTTPREQUESTS();
    
      bool sendRequest();
    
      char const* certificate = "";
    
      char parameters[256] = "defaultParameters";
      char device[30];
      char address[64] = "defaultAddress";
      unsigned long timestamp = 0;
      int sendAttempts = 0;
      bool sendImmediately = false;
      unsigned long lastSendAttempt = 0;
    };
    

    并将其全部放在一起:

    char const *HTTPREQUESTS::certificate="";
    
    char HTTPREQUESTS::parameters[256]="defaultParameters";
    char HTTPREQUESTS::device[30];
    char HTTPREQUESTS::address[64] = "defaultAddress";
    unsigned long HTTPREQUESTS::timestamp=0;
    int HTTPREQUESTS::sendAttempts=0;
    bool HTTPREQUESTS::sendImmediately = false;
    unsigned long HTTPREQUESTS::lastSendAttempt=0;
    

    Latter would only be necessary if all those class members were static, which is not the case here and which is not your intention as far as I understood.