恼人的错误:'{'标记之前的预期类名

时间:2013-01-27 22:30:30

标签: c++ oop compiler-errors arduino

我一直在尝试使用arduino库编译基本程序,并且在'{'标记错误之前我一直得到这个预期的类名。我是c ++的新手,所以非常感谢详细的解释/插入解决方案。

以下是代码:

SerialGSM.h

#ifndef _SerialGSM_H
#define _SerialGSM_H
#include "Arduino.h"
#include <SoftwareSerial.h>

#define SERIALTIMEOUT 2000
#define PHONESIZE 13
#define MAXMSGLEN 160

class SerialGSM : public SoftwareSerial {
public:
  SerialGSM(int rxpin,int txpin);
  void FwdSMS2Serial();
  void SendSMS();
  void SendSMS(char * cellnumber,char * outmsg);
  void DeleteAllSMS();
  void Reset();
  void EndSMS();
  void StartSMS();
  int ReadLine();
  int ReceiveSMS();
  void Verbose(boolean var1);
  boolean Verbose();
  void Sender(char * var1);
  char * Sender();
  void Rcpt(char * var1);
  char * Rcpt();
  void Message(char * var1);
  char * Message();
  void Boot();

  boolean verbose;
  char sendernumber[PHONESIZE + 1];
  char rcpt[PHONESIZE + 1];
  char outmessage[160];
  char inmessage[160];

protected:
  unsigned long lastrec;

};

#endif /* not defined _SerialGSM_H */

SerialGSM.cpp

// SerialGSM version 1.1
// by Meir Michanie
// meirm@riunx.com





// error codes
// http://www.developershome.com/sms/resultCodes2.asp
#include <SerialGSM.h>

SerialGSM::SerialGSM(int rxpin,int txpin):
SoftwareSerial(rxpin,txpin)
{
 verbose=false;
}

void SerialGSM::FwdSMS2Serial(){
  Serial.println("AT+CMGF=1"); // set SMS mode to text
  this->println("AT+CMGF=1"); // set SMS mode to text
  delay(200);
  this->ReadLine();
  Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
  this->println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
  delay(200);
  this->ReadLine();
}

void SerialGSM::SendSMS(char * cellnumber,char * outmsg){
  this->Rcpt(cellnumber);
  if (verbose) Serial.println(rcpt);
  this->StartSMS();
  this->Message(outmsg);
  Serial.print(outmessage);
  this->print(outmessage);
  this->EndSMS();
  delay(500);
  this->ReadLine();
}

void SerialGSM::SendSMS(){
  if (verbose) Serial.println(rcpt);
  if (verbose) Serial.println(outmessage);
  this->StartSMS();
  Serial.print(outmessage);
  this->print(outmessage);
  this->EndSMS();
  delay(500);
  this->ReadLine();
}

void SerialGSM::DeleteAllSMS(){
  Serial.println("AT+CMGD=1,4"); // delete all SMS
  this->println("AT+CMGD=1,4"); // delete all SMS
  delay(200);
  this->ReadLine();
}

void SerialGSM::Reset(){
  Serial.println("AT+CFUN=1,1"); // Reset Modem
  this->println("AT+CFUN=1,1"); // Reset Modem
  delay(200);
  this->ReadLine();
}


void SerialGSM::EndSMS(){
  this->print(char(26));  // ASCII equivalent of Ctrl-Z
  Serial.println();

  //delay(5 * 1000); // the SMS module needs time to return to OK status
}

void SerialGSM::StartSMS(){

  Serial.println("AT+CMGF=1"); // set SMS mode to text
  this->println("AT+CMGF=1"); // set SMS mode to text
  delay(200);
  this->ReadLine();

  Serial.print("AT+CMGS=");
  this->print("AT+CMGS=");

  this->print(char(34)); // ASCII equivalent of "

  Serial.print(rcpt);
  this->print(rcpt);

  this->println(char(34));  // ASCII equivalent of "

  delay(500); // give the module some thinking time
  this->ReadLine();

}

int SerialGSM::ReadLine(){
  static int pos=0;
  char nc;
  while (this->available()){
    nc=this->read();
    if (nc == '\n' or (pos > MAXMSGLEN) or ((millis()> lastrec + SERIALTIMEOUT)and (pos > 0)) ){
      nc='\0';
      lastrec=millis();
      inmessage[pos]=nc;
     pos=0;
     if (verbose) Serial.println(inmessage);
      return 1;
    }
    else if (nc=='\r') {
    }
    else{
      inmessage[pos++]=nc;
      lastrec=millis();
    }
  }
  return 0;
}


int SerialGSM::ReceiveSMS(){
  static boolean insms=0;
  if (this->ReadLine()){
  // Get the number of the sms sender in order to be able to reply
    if ( strstr(inmessage, "CMT: ") != NULL ){
        insms=1;
        int sf=6;
        if(strstr(inmessage, "+CMT:")) sf++; 
            for (int i=0;i < PHONESIZE;i++){
              sendernumber[i]=inmessage[sf+i];
            }
        sendernumber[PHONESIZE]='\0';
        return 0;
     }else{ 
        if(insms) {
            insms=0;
            return 1;
        }
    }
  }
  return 0;
}


boolean SerialGSM::Verbose(){
    return verbose;
}

void SerialGSM::Verbose(boolean var1){
    verbose=var1;
}

char * SerialGSM::Sender(){
    return sendernumber;
}


char * SerialGSM::Rcpt(){
    return rcpt;
}

char * SerialGSM::Message(){
    return inmessage;
}


void SerialGSM::Sender(char * var1){
    sprintf(sendernumber,"%s",var1);
}


void SerialGSM::Rcpt(char * var1){
    sprintf(rcpt,"%s",var1);
}

void SerialGSM::Message(char * var1){
    sprintf(outmessage,"%s",var1);
}

void SerialGSM::Boot(){
  int counter=0;
  while(counter++ < 15){
    if (verbose) Serial.print(".");
    delay(1000);
  }
  if (verbose) Serial.println();

}

Arduino草图

#include <SerialGSM.h>
SerialGSM cell(2,3);

boolean sendonce=true;
void setup(){
  Serial.begin(9600);
  // cell.begin(9600);
  cell.Verbose(true);
  cell.Boot();
  cell.DeleteAllSMS();
  cell.FwdSMS2Serial();
 }


void loop(){
  if (cell.ReceiveSMS()){
     Serial.print("Sender: ");
     Serial.println(cell.Sender());
     Serial.print("message: ");
     Serial.println(cell.Message());
     cell.DeleteAllSMS();
  }
}

编辑:

以下是完整的错误消息: /home/agt/arduino/libraries/SerialGSM.h:10:错误:'{'令牌之前的预期类名 感谢

1 个答案:

答案 0 :(得分:2)

您正在设置头文件“arduino.h”和“serialgsm.h”之间的相互包含。通过使用前向声明来解决这个问题,错误很可能会消失(或者你至少会得到一个不同的错误)。