Using a class object as a param in a function for another class

I have the current source that will not compile on Tinkercad circuits (using an old version of the Arduino compiler)...

enum MyEnum
{
	None = 0,
	Choice = 1,
};

class ClassTwo {
public:
	ClassTwo(enum MyEnum e) { }
};

class ClassOne {
public:
	void MyMethod(ClassTwo c) { }
};

void setup() { }

void loop() { }

The line with void MyMethod(ClassTwo c) { } gives me an error. The errors reported are...

  • 2:15: error: variable or field 'MyMethod' declared void
  • 2:15: error: 'ClassTwo' was not declared in this scope

Can someone point out why this does not compile in the Arduino compiler but works fine in other IDEs such as VS c++ and VSCode etc? What I'm I doing wrong?

Thanks!

When I try to compile what you posted I get:

Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

Is this exactly the same code that created the error messages? What version of Arduino?

Delta_G:
When I try to compile what you posted I get:

Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.




Is this exactly the same code that created the error messages? What version of Arduino?

That's the part the kills me Delta_G... On my own machine, I use the latest version of the Arduino IDE and it compiles with no issues (also on VSCode, and VS). So in my environment, everything always gives me a thumbs up. However, in the Tinkercad Circuits environment, I get errors. I contacted them and they told me that their product uses an old version of the Arduino compiler (though they never told me the version). I have just recently asked them to give me the exact version number so I'm hoping to be able to get that soon. For now, all I can tell you is it's an older version.

So even though it's compiling for you in the current IDE the issue (whatever it is) still exists in the old compilers that I have no choice but to use from a third party.

My goal with this thread was to have someone give me a different way to code what I have in my example that might work. I know the code I posted is 100% solid as I said in my OP. But it's not so solid in old versions of Arduino compiler.

Just a guess: might it be more aggressive settings on optimization. ClassTwo doesn't do anything at all; could the compiler have optimized it away?

wildbill:
Just a guess: might it be more aggressive settings on optimization. ClassTwo doesn't do anything at all; could the compiler have optimized it away?

Thanks wildbill, great idea but nah that's not it. It was giving me issues in my actual code (more complex) and I just wanted to simplify the code before posting. Before when testing in Tinkercad I was removing little by little to make it less complex. There were many tests with declared variables, functions, etc in the class.

I do wonder about this line..

ClassTwo(enum MyEnum e)

Shouldn't it be..

ClassTwo(MyEnum e)

?

-jim lee

jimLee:
I do wonder about this line..

ClassTwo(enum MyEnum e)

Shouldn't it be..

ClassTwo(MyEnum e)

?

-jim lee

Normally yes, but apparently adding that has something to do with support the way the code compiles or something. I have tried it without the "enum" keyword to though (that's what I started with anyway).

But to ease your mind, the following compiles with no issues.

enum MyEnum
{
	None = 0,
	Choice = 1,
};

class ClassTwo {
public:
	ClassTwo(enum MyEnum e) { }
};

class ClassOne {
public:
	//void MyMethod(ClassTwo c) { }
};

void setup() { }

void loop() { }

Are there no other errors or warnings that could explain why ClassTwo is "not declared in this scope"?

One thing to try... Earlier versions of Arduino had trouble with the auto-generated prototypes for functions taking struct and enum types as arguments. The prototypes were generated above the global declarations so if you just gave the struct/enum name the name would not yet be defined. Adding the keyword 'struct' or 'enum' was sufficient for the compiler to accept the prototype. Perhaps it is the same with a class type. Try changing your line to:

	void MyMethod(class ClassTwo c) { }

johnwasser:
Are there no other errors or warnings that could explain why ClassTwo is “not declared in this scope”?

One thing to try… Earlier versions of Arduino had trouble with the auto-generated prototypes for functions taking struct and enum types as arguments. The prototypes were generated above the global declarations so if you just gave the struct/enum name the name would not yet be defined. Adding the keyword ‘struct’ or ‘enum’ was sufficient for the compiler to accept the prototype. Perhaps it is the same with a class type. Try changing your line to:

	void MyMethod(class ClassTwo c) { }

I have been reading a bunch of your posts here and there John. The info about the prototypes in the other threads helped me resolve the other class issues I had with… yep you guessed it ENUMS as parameters! Thanks for that info.

In regards to this class issue, once again right on the money! Your fix (adding “class” keyword) worked like a charm.

Thanks!!