Welcome, Guest. Please Login or Register
Arduino IDE for Visual Studio
 
 
When Logging a Support Issue in the Forum, please ensure you have also:-
  • Enabled vMicro > Compiler > Show Build Properties and Verbose
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
Support requests without the output above may be impossible to answer, so please help us to help you
 
  HomeSearch Tutorials & Wiki visualmicro.com LoginHelpRegister  
 
Page Index Toggle Pages: 1
Send Topic Print
Compile Error (Read 155 times)
sima
Newbies
*
Offline



Posts: 7
Compile Error
Sep 26th, 2019 at 8:06pm
 
Hi Guys

If I try to compile this code

Code:
template<typename T> uint8_t HC_12CopyAnything(T& value, uint16_t anzahl) {
  uint8_t * p = (uint8_t*) &value;
  uint16_t i;
  for (i = 0; i < anzahl; i++) {
    *p++ = packetBufferHC_12[i];
  } return i;
}
 



I am always getting an error, compiles fine in Arduino IDE

Here the error message:

LOLIN32_HC12_WebServer_SendStructure.ino: 41:27: error: 'T' was not declared in this scope

LOLIN32_HC12_WebServer_SendStructure.ino: 41:30: error: 'value' was not declared in this scope

LOLIN32_HC12_WebServer_SendStructure.ino: 41:46: error: expected primary-expression before 'anzahl

LOLIN32_HC12_WebServer_SendStructure.ino: 41:52: error: expression list treated as compound expression in initializer [-fpermissive]

LOLIN32_HC12_WebServer_SendStructure.ino: 219:73: error: 'template<class T> uint8_t HC_12CopyAnything(T&, uint16_t)' redeclared as different kind of symbol
   template<typename T> uint8_t HC_12CopyAnything(T& value, uint16_t anzahl) {
LOLIN32_HC12_WebServer_SendStructure.ino:41: note  previous declaration uint8_t HC_12CopyAnything
   uint8_t  Datensatz = 1

Back to top
 
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10565
United Kingdom
Re: Compile Error
Reply #1 - Sep 26th, 2019 at 8:26pm
 
One change in arduino in the past year or two is that is better places the automatic prototypes that are generated for code in .ino files.

You can place the prototypes yourself. If you can give some simple .ino code that builds in arduino but fails in visul micro we can show you the solution.

Another alternative is to put the user types in .h files and then call from the ino or cpp code

Back to top
 
WWW  
IP Logged
 
sima
Newbies
*
Offline



Posts: 7
Re: Compile Error
Reply #2 - Sep 26th, 2019 at 8:55pm
 
Here we go

Code:
#include <SoftwareSerial.h>
#include <CRC32.h>
SoftwareSerial mySerial(2, 3);  // RX, TX
CRC32 crc;

float h1, t1;        // Temperatur und Luftfeuchte Sensor1
float rvcc;          // Batteriespannung
uint32_t zk = 0;     // Zähler kurze Zyklen in Folge
uint32_t zl = 0;     // Zähler lange Zyklen in Folge
uint32_t zg = 0;     // Zyklen seit Start

#pragma pack (push, 1)
struct {
  uint8_t  Datensatz = 1;
  uint8_t  telegrammlaenge;
  uint8_t  x02 = 0x02;
  uint8_t  x03 = 0x03;
  uint8_t  x04 = 0x04;
  float    x05 = 0x06;
  uint32_t crc;
} Daten1;
#pragma pack (pop)

template <typename T> uint32_t calcCRC32  (const T& value) {
  const uint8_t * p = (const uint8_t*) &value;
  uint32_t mycrc = 0xFFFF;
  for (unsigned int i = 0; i < sizeof value - 2; i++) {
    Serial.print(*p, HEX);
    Serial.print(" ");
    crc.update(*p++);
  }
  mycrc = crc.finalize();
 
  Serial.print("CRC32 = "); Serial.println(mycrc, HEX); //Serial.println();
  return mycrc;
}

//frei nach Nick Gammon's I2C_Anything.h --> http://www.gammon.com.au/forum/?id=10896&reply=8#reply8
template <typename T> uint8_t softSerialWriteAnything (const T& value) {
  const uint8_t * p = (const uint8_t*) &value;
  uint8_t i;
  Serial.println("---> softSerialWriteAnything() ");
  Serial.print("Size: "); Serial.print(sizeof value); Serial.print(" Byte - Data = ");
  //*p = sizeof value;
  for (i = 0; i < sizeof value; i++) {
    mySerial.write(*p);
    Serial.print("0x");
    if (*p <= 15) Serial.print("0");
    Serial.print(*p++, HEX);
    Serial.print(" ");
  }
  Serial.println();
  return i;
}

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
  Daten1.telegrammlaenge = (sizeof Daten1);
}

void loop() {
  Daten1.x03++;
  Daten1.x05 += 1.2345678;
 
  // Senden von Daten1
  Daten1.crc = calcCRC32(Daten1);    // vor senden CRC fuer Daten1 berechnen und in Struct eintragen
  softSerialWriteAnything(Daten1);   // Daten1 senden

  Serial.println();
  delay(2000);
}

 

Back to top
 
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10565
United Kingdom
Re: Compile Error
Reply #3 - Sep 26th, 2019 at 9:24pm
 
Thanks you can see below how I have added two function prototypes at the desired location which is after the user type has been defined.

The same would also be required in both the Arduino IDE and Visual Micro if the code is moved to cpp in the future.

Arduino are still perfecting the new way to determine prototype locations for .ino code. It was improved in 1.8.8 and we might do similar if it is stable. Previously it could cause unpredictable errors.

The prototypes have exact same syntax as the method signature but with semi-colon; on the end. The following code has been reduced to make this example easier to read.

The protypes are:-
template <typename T> uint32_t calcCRC32(const T& value);
template <typename T> uint8_t softSerialWriteAnything(const T& value);


Code (C++):
float h1, t1;        // Temperatur und Luftfeuchte Sensor1
float rvcc;          // Batteriespannung
uint32_t zk = 0;     // Zähler kurze Zyklen in Folge
uint32_t zl = 0;     // Zähler lange Zyklen in Folge
uint32_t zg = 0;     // Zyklen seit Start

#pragma pack (push, 1)
struct {
	uint8_t  Datensatz = 1;
	uint8_t  telegrammlaenge;
	uint8_t  x02 = 0x02;
	uint8_t  x03 = 0x03;
	uint8_t  x04 = 0x04;
	float    x05 = 0x06;
	uint32_t crc;
} Daten1;
#pragma pack (pop)

template <typename T> uint32_t calcCRC32(const T& value);
template <typename T> uint32_t calcCRC32(const T& value) {
	const uint8_t * p = (const uint8_t*)&value;
	uint32_t mycrc = 0xFFFF;
	return mycrc;
}

template <typename T> uint8_t softSerialWriteAnything(const T& value);
template <typename T> uint8_t softSerialWriteAnything(const T& value) {
	const uint8_t * p = (const uint8_t*)&value;
	uint8_t i;
	return i;
}

void setup() {
	Daten1.telegrammlaenge = (sizeof Daten1);
}

void loop() {
	Daten1.x03++;
	Daten1.x05 += 1.2345678;

	softSerialWriteAnything(Daten1);   // Daten1 senden

	Serial.println();
	delay(2000);
} 

Back to top
« Last Edit: Sep 26th, 2019 at 9:26pm by Visual Micro »  
WWW  
IP Logged
 
sima
Newbies
*
Offline



Posts: 7
Re: Compile Error
Reply #4 - Sep 27th, 2019 at 1:00pm
 
Thanks a lot

works like a charmnow.
Back to top
 
 
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print