Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Weird compiler error on simple program (Read 2441 times)
mgarber
Newbies
*
Offline


Posts: 7
Joined: Mar 2nd, 2017
Weird compiler error on simple program
Apr 3rd, 2018 at 7:20pm
Print Post  
This doesn't happen in the "real" arduino IDE but this simple program ....

Code
Select All
#include <Arduino.h>

class Color {
public:
	Color() {}
	Color(int ri) { setColors(ri); }
	Color *  setColors(int ri) {
		r = ri;
		return this;
	}
	int r = 0;
};

void dosomething(Color * c) {
	Color * d = c;
	int r = d->r;
	Serial.println(r);
}

Color c(666);

void setup() { 	Serial.begin(115200); }

void loop() {
	dosomething(c.setColors(999));
	delay(1000);
} 



.... complains ("...not declared in this scope") about the Color references.

Extra weirdlyer, if I simply move the Color class definition to a header file and include it, it compiles & runs fine.

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


Posts: 12144
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Weird compiler error on simple program
Reply #1 - Apr 3rd, 2018 at 7:36pm
Print Post  
Hi,

Put your custom class in a .h or .cpp and .h

Alternatively add the prototype manually as shown below.

You do not need to #include arduino.h in .ino files in visual micro or arduino ide

Code
Select All
class Color {
public:
	Color() {}
	Color(int ri) { setColors(ri); }
	Color *  setColors(int ri) {
		r = ri;
		return this;
	}
	int r = 0;
};

void dosomething(Color * c);

void dosomething(Color * c) {
	Color * d = c;
	int r = d->r;
	Serial.println(r);
}

Color c(666);

void setup() { 	Serial.begin(115200); }

void loop() {
	dosomething(c.setColors(999));
	delay(1000);
}

 




This is how Arduino worked for a long time. More recently it has worked out a better way to determine prototype location but that hasn't been implemented in visual micro yet.

« Last Edit: Apr 3rd, 2018 at 7:38pm by Tim@Visual Micro »  
Back to top
IP Logged
 
mgarber
Newbies
*
Offline


Posts: 7
Joined: Mar 2nd, 2017
Re: Weird compiler error on simple program
Reply #2 - Apr 3rd, 2018 at 7:50pm
Print Post  
OK!.... Thanks for the fast response!!!

But its still weird.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12144
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Weird compiler error on simple program
Reply #3 - Apr 3rd, 2018 at 7:57pm
Print Post  
Please give the compiler error that you see in the output. Copy and paste it into your response.

Weird isn't very helpful  Smiley

I tried the code that I have posted by copying it into a new project and it build correctly when your code failed.
  
Back to top
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12144
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Weird compiler error on simple program
Reply #4 - Apr 3rd, 2018 at 8:07pm
Print Post  
I looked at your code again and see that Visual Studio gives some false intellisense errors with a class defined along side normal code.

To be honest it is cleaner and better to user .h files.

I moved you Color class to a Color.h file which I added to the project. 

To create the Color.h file, right click the project and select "Add New Item"

In Color.h I have this code:-

Code
Select All
// Color.h

#ifndef _COLOR_h
#define _COLOR_h

#include "arduino.h"

class Color {
public:
	Color() {}
	Color(int ri) { setColors(ri); }
	Color *  setColors(int ri) {
		r = ri;
		return this;
	}
	int r = 0;
};


#endif 



Then your .ino code becomes more manageable and simpler:-

Code
Select All
#include "Color.h"

Color c(666);

void dosomething(Color * c) {
	Color * d = c;
	int r = d->r;
	Serial.println(r);
}


void setup() { Serial.begin(115200); }



void loop() {
	dosomething(c.setColors(999));
	delay(1000);
} 








« Last Edit: Apr 3rd, 2018 at 8:10pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint