Go Down

Topic: Compiler Error "... does not name a type" (Read 11568 times) previous topic - next topic

JanUlrich

Hello folks,

i tried to compile this code:
Code: [Select]

struct Packet {
    char header[7];
};

Packet p;


void setup()
{
  p = Packet();
}

void loop()
{
}

Packet recNextPacket(){ 
  return p;
}


but the Compiler tells me at validation: "sketch_nov16a:0: error: 'Packet' does not name a type"

Pls help!

MFG

bubulindo

#1
Nov 24, 2011, 09:45 am Last Edit: Nov 24, 2011, 09:47 am by bubulindo Reason: 1
Code: [Select]

struct Packet {
   char header[7];
};

struct Packet p;




Or:

Code: [Select]

typedef struct  {
   char header[7];
}Packet;

Packet p;


This... is a hobby.

JanUlrich

thank you very much!

Problem solved.

MFG

JanUlrich

ok I got another problem.

i hope this doesnt make too much effort for you:

this is the code:
Code: [Select]
#include "WProgram.h"
#define INT_16 int
#define UINT_16 unsigned int
#define INT long
#define UINT_32 unsigned long

UINT_32 timerInit(){
    return millis();
}

UINT_32 timerDiff(UINT_32 zeitstempel){
    if(millis()<zeitstempel)return (((UINT_32)-1)-zeitstempel)+millis();
    return millis()-zeitstempel;
}

INT_16 readData(char* buffer, INT_16 len)
{
    INT_16 bytesRead=0;
    while(Serial.available() > 0 && bytesRead < len)
        {
            *buffer = Serial.read();
            buffer++;
            bytesRead++;
        }
    return bytesRead;
}

bool sendData(const void * buffer, INT_16 nSize)
{
    Serial.write((const uint8_t*)buffer, nSize);
}

void closeSerialPort()
{
    Serial.end();
}

bool initializeSerialPort(const char* cpDevice)
{
    Serial.begin(9600); //Bei Änderungen auch Baudrate auf Server-Seite ändern!
    return true;
}


#define protName "LBUKAS"


void ntohs(uint16_t* value) {
  uint16_t tmp_a = (*value & 0xff00) >> 8;
  uint16_t tmp_b = (*value & 0x00ff) << 8;
  *value = tmp_b | tmp_a;
}

void ntohl(uint32_t* value) {
  uint32_t tmp_a = (*value & 0xff000000) >> 24;
  uint32_t tmp_b = (*value & 0x00ff0000) >> 8;
  uint32_t tmp_c = (*value & 0x0000ff00) << 8 ;
  uint32_t tmp_d = (*value & 0x000000ff) << 24;
  *value = tmp_d | tmp_c |tmp_b | tmp_a;
}

struct Packet{
    char header[7];
    INT_16 checksum;
    UINT_16 packetNUM;
    bool ack;
    UINT_32 data;
};


class Traffic{
private:
    UINT_16 packetNumber;

    void shiftLeftCharArray(char * p,int size){
        for(int i=0;i<size-1;i++){
            *p = *(p+1);
            p++;
        }
    }

public:
   struct Packet recNextPacket(UINT_32 timeout){
        int bytesRead=0;
        struct Packet p = Packet();
        char * head = (char*)p.header;
        UINT_32 timer = timerInit();
        do{
            do{
                int br = readData(head, sizeof(p.header)-bytesRead);
                head += br;
                bytesRead += br;
            }while(bytesRead<sizeof(p.header)&&(timerDiff(timer)<timeout)); //Header aus Datenstrom ausgelesen.
            if(timerDiff(timer)<timeout)break;
            if(strcmp(protName,p.header))break; // der Paketanfang wurde gefunden
            //ansonsten...
            shiftLeftCharArray((char*)p.header,sizeof(p.header));
            bytesRead=sizeof(p.header)-1;
            head = (char*)p.header+sizeof(p.header)-1;
        }while(timerDiff(timer)<timeout);
        head=(char*)&p;
        head+=sizeof(p.header);
        while(timerDiff(timer)<timeout);{
            int br = readData(head, sizeof(p)-bytesRead-sizeof(p.header));
            head += br;
            bytesRead += br;
        }
        if(timerDiff(timer)<timeout)p.packetNUM=0; // wir setzen packetNUM auf null, falls das Paket nicht gänzlich empfangen wurde!
        return p;
    }

    ~Traffic(){
        closeSerialPort();
    }

    Traffic(){
      initializeSerialPort("\0");
      packetNumber=1;
      //warteschlange = PacketSpeicher();
    }

    bool sendTicks(UINT_16 ticks, UINT_32 timeout){
        struct Packet p = Packet();
        strcpy(p.header, protName);
        p.packetNUM=packetNumber;
        p.checksum=0;
        p.data=ticks;
        p.ack=false;
        UINT_32 timer = timerInit();
        sendData(&p,sizeof(struct Packet));
        do{
            if(packetNumber==recieveACK(timeout-timerDiff(timer))){
                packetNumber++;
                return true;
            }
        }while(timerDiff(timer)<timeout);
        return false;
    }

    INT_16 recieveACK(UINT_32 timeout){
        struct Packet p;
        UINT_32 timer = timerInit();
        do{
            p = recNextPacket(timeout-timerDiff(timer));
        }while(p.ack==false && timerDiff(timer)<timeout);
        if(timerDiff(timer)<timeout)return NULL;
        return p.packetNUM;
    }

    INT_16 recieveTicks(){
        struct Packet p = recNextPacket(1);
        return p.data;
    }
};


Traffic traffic;

void setup()
{
  traffic = Traffic();
}

void loop()
{
  delay(1000);
  Serial.write("test");
}


this code compiles and uploads its binary to the arduino. But if i listen to the serial, nothing gets send!

Pls help again!

It has to be a problem with the initialization of the Traffic class.
If i take the same code, but exchanging the setup function with this:
Code: [Select]
void setup()
{
  Serial.begin(9600);
  //traffic = Traffic();
}
the program works fine!

Best regards!

PaulS

Code: [Select]
Traffic traffic;
This has created an instance of the Traffic class, and stored that in a variable called traffic.

Code: [Select]
traffic = Traffic();
stomps on the definition of traffic assigning it what was returned by the constructor, which is not what you think it is.

In C++, you would new the new operator to assign what the constructor returned to a pointer. The new operator is not implemented for the Arduino, so you can't use the normal create an instance of a class process. Calling the constructor directly is not a good thing.
The art of getting good answers lies in asking good questions.

JanUlrich

#5
Nov 24, 2011, 12:21 pm Last Edit: Nov 24, 2011, 12:25 pm by JanUlrich Reason: 1
ah, allright. Thank you very much. Good answer!

I came from Java programming to C++ and I am still a C newbie. I made the mistake because of the missing "new" Operator.
Im sry for annoying you with such trivial questions.
But now my program works thanks to you and the Community ^^.

Problem solved!

sbr_


I made the mistake because of the missing "new" Operator.


FWIW, the forthcoming 1.0 release of the Arduino IDE provides the 'new' and 'delete' operators, at least they are in rc2.

arvinbold

#define Kpw 3200    //коэффициент пересчета импульсов счетчика
#define KPtim 3600000000/Kpw    //коэфф.для подсчета мощности по
                  //длит.периода импульсов (длительности мерим в мс)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3f,16,2);


unsigned long time;
unsigned long timeM;
unsigned long timeOld;
unsigned long PerImp;
const int pinPw = 8;  //вывод для подключения оптрона счетчика эл.эн.
byte pinPwOld = 1;    //старое значение состояния входа
long datD;
byte i;

//Установка Таймера2.
//Конфигурирует 8-битный Таймер2 ATMega168 для выработки прерывания
//с периодом около 16 мс.
void SetupTimer2()  {
//Установки Таймер2: Делитель частоты /1024, режим 0
//Частота = 16MHz/1024 =  16.384 мс
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;   //на 1024
//Подключение прерывания по переполнению Timer2
TIMSK2 = 1<<TOIE2;
}

//Timer2 указатель вектора прерывания по переполнению
ISR(TIMER2_OVF_vect)  {
//приращение переменной
time ++;
int valpinPw = digitalRead(pinPw);
if (valpinPw == 0) {pinPwOld = 0;}
else {
  if (pinPwOld == 0)
  { pinPwOld = 1;
    timeM = millis();
    PerImp = timeM - timeOld;
    timeOld = timeM;
  } 
}
}

void setup() {
  //для подсчета мощности
  time = 0;
  timeOld = millis();
  PerImp = 1;
  pinMode(pinPw, INPUT);
  digitalWrite(pinPw, 1); // вкл внутр резистор
  Serial.begin(9600);
  SetupTimer2();  //прерывания таймера - подчет мощности
}

void loop() {
 datD = KPtim/PerImp;  //текущая потребляемая мощность
 Serial.print("Power = ");
 Serial.println(datD);
 delay(10000);
}
 lcd.init()             // initialize the lcd
 lcd.print("Power = ");
 lcd.println(datD);      ------------------'lcd' does not name a type ??????
 delay(10000);
}




PaulS

This is ALL of loop():
Code: [Select]
void loop() {
 datD = KPtim/PerImp;  //текущая потребляемая мощность
 Serial.print("Power = ");
 Serial.println(datD);
 delay(10000);
}


This code:
Code: [Select]
lcd.init()             // initialize the lcd
 lcd.print("Power = ");
 lcd.println(datD);      ------------------'lcd' does not name a type ??????
 delay(10000);
}

is NOT in a function. ALL code MUST be in a function.
The art of getting good answers lies in asking good questions.

Go Up