0

I see there are a couple of Q&A in this topic but I still can't find the clue to my issue. I must admit I am pretty new to c++, so this might be the root cause.

Ok, so I am trying to declare a new type like :

struct measurement_t
{
  int sensorID;
  int sensorData;
  measurement_t(int ID, int Data)
  {
    sensorID = ID;
    sensorData = Data;
  }
};

and my project contains a method, that should return this type of data:

measurement_t getLightSensorData()
{
    ...
}

when I try to compile the code I got error : error: 'measurement_t' does not name a type

What I also find very strange is that the error line the compiler reports is the line number of the first #define statement at the very beginning of the code :

#define CE_PIN  7

If I simply change the method to void, the the project compiles (useless, though).

I also tried returning pointer by specifying measurement_t * as return type but the result is the same. Tried remove constructor from struct, same result again.

Can someone please help me to understand what the problem is here ?

The minimal version of the code that reproducing the same issue is:

struct measurement_t
{
  int sensorID;
  int sensorData;
  measurement_t(int ID, int Data)
  {
    sensorID = ID;
    sensorData = Data;
  }
};


measurement_t getLightSensorData()
{
  int sensorValue = 1;
  measurement_t m(1, sensorValue);
  return m;
}

enter image description here

For reference, this is intended to be an arduino sketch, and the full code is as below:

#include "RF24.h"
#include <SPI.h>
#include "printf.h"

#define CE_PIN  7
#define CS_PIN  8
#define LIGHTSENSOR_PIN A0

RF24 myRadio(CE_PIN, CS_PIN);
byte rxAddr[6] = { 0x0, 0x0, 0x0, 0x0, 0x0F };

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 3000; // send once per second
unsigned long loopSleepMillis = 200;

struct measurement_t
{
  int sensorID;
  int sensorData;
  measurement_t(int ID, int Data)
  {
    sensorID = ID;
    sensorData = Data;
  }
};

// the setup routine runs once when you press reset:
void setup()
{
  Serial.println(">>> Initializing...");
  Serial.begin(115200);
  printf_begin();
  bool radioOk = myRadio.begin();  // Start up the physical nRF24L01 Radio
  if (radioOk) Serial.println("     Radio initialized");
  else Serial.println("     ERROR initializing radio !");
  myRadio.setChannel(120);  // Above most Wifi Channels
  myRadio.setPALevel(RF24_PA_HIGH);
  myRadio.setRetries(15, 15);
  myRadio.openWritingPipe(rxAddr); // Use the first entry in array 'addresses' (Only 1 right now)
  Serial.println("<<< Done initialization");
}

// the loop routine runs over and over again forever:
void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
    transmitData(getLightSensorData());
    prevMillis = millis();
  }
  delay(loopSleepMillis);
}

measurement_t getLightSensorData()
{
  int sensorValue = analogRead(LIGHTSENSOR_PIN);
  measurement_t m(1, sensorValue);
  return m;
}

void transmitData(measurement_t data)
{
  myRadio.stopListening();
  Serial.print("Transmitting data...");
  bool writeOK = myRadio.write(&data, sizeof(data));
  if (writeOK)
  {
    Serial.println("OK");
  }
  else
  {
    Serial.println(F("no response"));
    //myRadio.printDetails();
  }
}
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
  • 1
    Circular inclusion problem? – πάντα ῥεῖ Mar 12 '17 at 13:02
  • Thanks, but I do not think this issue has anything to do with inclusion –  Mar 12 '17 at 13:06
  • What's the full error message? – melpomene Mar 12 '17 at 13:07
  • 1
    @LászlóFrank Well, read [here](http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies) to be sure that isn't your problem. – πάντα ῥεῖ Mar 12 '17 at 13:08
  • @melpomene : 6:1: error: 'measuremant_t' does not name a type –  Mar 12 '17 at 13:08
  • That's not the full message. Line 6 of what? – melpomene Mar 12 '17 at 13:10
  • @πάνταῥεῖ : but the error line is not the #include, but the #define. How is it related to include ? Moreover, the included headers a standard libraries, surely not including my simple, first and new code. –  Mar 12 '17 at 13:11
  • 1
    @LászlóFrank Create a [MCVE] that reproduces your problem, and post that one in your question please. – πάντα ῥεῖ Mar 12 '17 at 13:13
  • Are you absolutely *sure* you're looking at the correct file? The error message doesn't relate to a header that's being pulled in? – G.M. Mar 12 '17 at 13:14
  • @melpomene : yes, it is the full message indeed. Line 6 of the full code I inlcuded with the post. But more precisely, there re other errors, you probably mean that :-) 6:1: error: 'measurement_t' does not name a type 7:19: error: 'measurement_t' was not declared in this scope 7:19: error: variable or field 'transmitData' declared void 51:34: error: 'transmitData' was not declared in this scope 51:33: error: 'getLightSensorData' was not declared in this scope –  Mar 12 '17 at 13:14
  • No, I mean the filenames that should be part of those messages. What happened to them? – melpomene Mar 12 '17 at 13:15
  • You've changed your story again. It was "measuremant_t" before; now it's "measurement_t". Don't retype (parts of) the compiler output. Copy/paste it all (in your question, not a comment). – melpomene Mar 12 '17 at 13:16
  • Just compiled the code, Compiled successfully!I think you are compiling wrong file. – Sniper Mar 12 '17 at 13:17
  • @πάνταῥεῖ : I have edited the post and included the minimal version –  Mar 12 '17 at 13:19
  • @LászlóFrank How are code lines like `bool radioOk = myRadio.begin();` relevant for the problem? Explain in depth please, or narrow further. – πάντα ῥεῖ Mar 12 '17 at 13:21
  • @melpomene : I removed the filenames, they are the same:. C:\My Projects\Visual Studio Projects\IOT\WirelessLightSensor\WirelessLightSensor.ino 6 I can't post the full output here... –  Mar 12 '17 at 13:21
  • Yes, you can and should post the full output. – melpomene Mar 12 '17 at 13:22
  • @πάνταῥεῖ : the code you quoted is not relevant at all, please could you look at the modified post and the minimal version included. –  Mar 12 '17 at 13:23
  • @melpomene : could we please focus on the minimal code version ? –  Mar 12 '17 at 13:24
  • 1
    Are you perhaps trying to compile this as `C` rather than `C++`? That might in some way account for those error messages. – G.M. Mar 12 '17 at 13:25
  • 1
    @LászlóFrank I am. Where are the error messages? – melpomene Mar 12 '17 at 13:25
  • @melpomene : Error compiling project sources Debug build failed for project 'test' test.ino: 2:1: error: 'measurement_t' does not name a type measurement_t getLightSensorData() Severity Code Description Project File Line Suppression State Error 2:1: error: 'measurement_t' does not name a type C:\Users\Lacko\Documents\Arduino\test\test.ino 2 –  Mar 12 '17 at 13:26
  • The code compiles, Check this https://ibb.co/hibPtv – Sniper Mar 12 '17 at 13:27
  • 2
    @LászlóFrank Well, your [_"minimal example"_](http://coliru.stacked-crooked.com/a/d7eb835894729c65) doesn't reproduce the problem, that's why I'm complaining. – πάντα ῥεῖ Mar 12 '17 at 13:28
  • 1
    @LászlóFrank Dude. Relevant information needs to go into the question itself, not down here hidden in the comments. – melpomene Mar 12 '17 at 13:29
  • 1
    Use 'struct measurement_t getLightSensorData()' – Sniper Mar 12 '17 at 13:35
  • @πάνταῥεῖ, user64322 : I updated the post and inserted a picture of Visual Studio exhibiting the isue (link right below the minimal code version) –  Mar 12 '17 at 13:49
  • 1
    @LászlóFrank What's actually so hard to understand about a _[MCVE]_? – πάντα ῥεῖ Mar 12 '17 at 13:51
  • @user64322 : your're right, I had to use struct measurement_t getLightSensorDate() ! At last, problem resolved for now. Many thanks ! Could you also explain what is the difference ? –  Mar 12 '17 at 13:52
  • @user64322 You suspect a c compiler is used here? I doubt so. Is that special for arduino? – πάντα ῥεῖ Mar 12 '17 at 13:52
  • @LászlóFrank Your screenshot shows the file from line 3 onwards. The error message refers to line 2. – melpomene Mar 12 '17 at 13:54
  • @πάνταῥεῖ : look at the picture in the post if you don't believe me. My example was Minimal, Complete and Verifiable. Why us that so difficult to understand ? By the way, issue has been resolved. –  Mar 12 '17 at 13:54
  • @melpomene : line 1-2 were empty, it just was scrolled down a bit, sorry for that –  Mar 12 '17 at 13:57
  • @LászlóFrank It's not a question if I _believe_ you or not. You're in charge to pinpoint something reproducible for us. – πάντα ῥεῖ Mar 12 '17 at 13:57
  • Thanks you all guys for your instant and precise help ! –  Mar 12 '17 at 14:02

1 Answers1

-1

You had to use

struct measurement_t getLightSensorData()
{
  int sensorValue = analogRead(LIGHTSENSOR_PIN);
  measurement_t m(1, sensorValue);
  return m;
}

You might have outdated version of Arduino IDE because on my IDE it compiled without the use of struct keyword.

Sniper
  • 1,428
  • 1
  • 12
  • 28
  • I have Arduino IDE 1.8.1 installed and as far as I can see this is the latest. On the other hand I am using vMicro from VS2015, maybe it has something to do with this strange behavior. –  Mar 12 '17 at 14:05