Go Down

Topic: Annoying compliler errors (Read 1 time) previous topic - next topic

Chris Parish

I am getting this error:
Code: [Select]

sketch_mar31a:-1: error: expected ',' or '...' before '&' token
sketch_mar31a:-1: error: ISO C++ forbids declaration of 'T' with no type
sketch_mar31a:-1: error: 'T' was not declared in this scope
sketch_mar31a:-1: error: 'value' was not declared in this scope
sketch_mar31a:9: error: 'template<class T> int file_readAnything(T&)' redeclared as different kind of symbol
sketch_mar31a:-1: error: previous declaration of 'int file_readAnything'


from this code
Code: [Select]
template <class T> int file_writeAnything(const T& value)
{
  const byte* p = (const byte*)(const void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    //file.write(*p++);
  return i;
}

template <class T> int file_readAnything(T& value)
{
  byte* p = (byte*)(void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
   // *p++ = file.read();
  return i;
}

void setup()
{
 
}

void loop()
{
 
}


The above code is a segment from a larger project and it used to work before I upgraded to the latest IDE.
Struggleing here.

I know there is no code in the setup and loop functions, but it should still complie without any problems.
Any ideas?

retrolefty

It's a known bug with version 22. One workaround is putting the templete code into a tabbed window and call it something.h and then include it in your main sketch. Here is the issue entry for the bug:

http://code.google.com/p/arduino/issues/detail?id=472&sort=-id -type

Lefty

Chris Parish

Excellent. Thanks, Lefty.

However now I have a further problem. The code is now in an .h tab (and so are various other bits in the project) but now it is trying to tell me I am redefining things that I am defining for the first time.

errors:
Code: [Select]

In file included from version0_6.cpp:18:
storage.h:5: error: redefinition of 'Sd2Card card'
storage.h:5: error: 'Sd2Card card' previously declared here
storage.h:6: error: redefinition of 'SdVolume volume'
storage.h:6: error: 'SdVolume volume' previously declared here
storage.h:7: error: redefinition of 'SdFile root'
storage.h:7: error: 'SdFile root' previously declared here
storage.h:8: error: redefinition of 'SdFile file'
storage.h:8: error: 'SdFile file' previously declared here
storage.h:10: error: redefinition of 'template<class T> int file_writeAnything(const T&)'
storage.h:10: error: 'template<class T> int file_writeAnything(const T&)' previously declared here
storage.h:19: error: redefinition of 'template<class T> int file_readAnything(T&)'
storage.h:19: error: 'template<class T> int file_readAnything(T&)' previously declared here
storage.h: In function 'boolean setupStorage()':
storage.h:30: error: redefinition of 'boolean setupStorage()'
storage.h:30: error: 'boolean setupStorage()' previously defined here
storage.h: In function 'void getConfigData()':
storage.h:85: error: redefinition of 'void getConfigData()'
storage.h:85: error: 'void getConfigData()' previously defined here
storage.h: In function 'void updateConfigData()':
storage.h:102: error: redefinition of 'void updateConfigData()'
storage.h:102: error: 'void updateConfigData()' previously defined here
storage.h: In function 'void storeData()':
storage.h:115: error: redefinition of 'void storeData()'
storage.h:115: error: 'void storeData()' previously defined here


and the code:
Code: [Select]
#define CD_PIN 3
#define CARDSPEED SPI_HALF_SPEED    //this can be set to SPI_FULL_SPEED if you want

//SD Card
Sd2Card   card;
SdVolume  volume;
SdFile    root;
SdFile    file;

template <class T> int file_writeAnything(const T& value)
{
  const byte* p = (const byte*)(const void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    file.write(*p++);
  return i;
}

template <class T> int file_readAnything(T& value)
{
  byte* p = (byte*)(void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    *p++ = file.read();
  return i;
}



boolean setupStorage()
{
  if (!enable_storage) {
    messageln ("Logger Disabled");
    return false;
  }
  pinMode(CD_PIN, INPUT);
  digitalWrite(CD_PIN, HIGH);
  //look for a card
  if (digitalRead(CD_PIN)) {
    error ("SD Card: There is no SD card installed");
    enable_storage = false;
    return false;
  }
  //try and initalise the card
  if (!card.init(CARDSPEED,9)) {
    error("SD Card: Failed to initalise card");
    enable_storage = false;
    return false;
  }
  else {
    messageln ("SD Card: SD Card Initalised");
  }
  if (!volume.init(&card)) {
    error("SD Card: Failed to initalise volume");
    enable_storage = false;
    return false;
  }
  else {
    messageln ("SD Card: Volume initalised");
  }
  if (!root.openRoot(&volume)) {
    error ("SD Card: Failed to initalise Root");
    enable_storage = false;
    return false;
  }
  else {
    messageln ("SD Card: Root initalised");
  }
  message("SD Card: Card Type - ");
  switch(card.type()) {
  case SD_CARD_TYPE_SD1:
    messageln("SD1");
    break;
  case SD_CARD_TYPE_SD2:
    messageln("SD2");
    break;
  case SD_CARD_TYPE_SDHC:
    messageln("SDHC");
    break;
  default:
    messageln("Unknown");
  }
}

void getConfigData()
{
  if (!enable_storage) return;
  if (!file.open(&root, "config.alt", O_READ)) { //file doesn't exists
    if (file.open(&root, "config.alt", O_CREAT | O_APPEND | O_WRITE)) {
       file_writeAnything(configData);
       file.close();
       messageln("Created new config data file");
    }
   } else {
     file_readAnything(configData);
     file.close();
     messageln("Loaded config data");
   }
 
}

void updateConfigData()
{
  if (!enable_storage) return;
 
  SdFile::remove(&root, "config.alt");
      if (file.open(&root, "config.alt", O_CREAT | O_APPEND | O_WRITE)) {
       file_writeAnything(configData);
       file.close();
       messageln("Updated config data");
    }
 
}

void storeData()
{
  if (!enable_storage) return;
  char name[12];
  sprintf(name, "flt%05d.csv", flight_number);
  if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE)) {
    error("Unable to open file");
  }
  file.print(millis());
  file.print(",");
  file.print(altitude);
  file.print(",");
  file.print(filter.x);
  file.println();
  if (file.writeError) {
    error("File write failed");
    enable_storage = false;
  }
  if (!file.close()) {
    error("File close failed");
    enable_storage = false;
  }
}




Chris Parish

ignore that last post.

Fixed it. :smiley-red:

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy