We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Duplicate Method Declaration
Page Index Toggle Pages: 1
Duplicate Method Declaration (Read 1109 times)
Duplicate Method Declaration
Mar 9th, 2008, 3:58am
 
I have overloaded versions of my grid() method.
They have different parameter signatures.
Why do I get: Semantic Error: Duplicate declaration of method "grid" ??
Code:
void setup() {
size(200,200);
background(153);
noLoop();
}//setup()

void draw() {
}//draw()

void grid(int x0, int y0, int w, int h, int d, color c) {
//.TODO.
}//grid()

void grid(int x0, int y0, int w, int h, int d) {
grid(x0,y0,w,h,d,color(20,100,100));
}//grid()

void grid(int w, int h, int d, color c) {
grid(0,0,w,h,d,c);
}//grid()

void grid(int w, int h, int d) {
grid(0,0,w,h,d,color(20,100,100));
}//grid()

void grid(int x0, int y0, int w, int h) { //<< ERROR HERE
grid(x0,y0,w,h,10,color(20,100,100));
}//grid()

void grid(int w, int h) {
grid(0,0,w,h,10,color(20,100,100));
}//grid()
Re: Duplicate Method Declaration
Reply #1 - Mar 9th, 2008, 5:19am
 
I don't think you can just overload methods like that.

I suggest you create a class and overload there.

You just need like 5 more lines of code for the class declaration, constructor and closing brackets. Smiley
Re: Duplicate Method Declaration
Reply #2 - Mar 9th, 2008, 5:32am
 
you get the duplicated declaration error because

Code:

void grid(int w, int h, int d, color c) {

and
Code:

void grid(int x0, int y0, int w, int h) { //<< ERROR HERE

have the same types of parameters (4 ints) since color is seen and interpreted as an int.

@acapulco
to overload methods they dont necessarily have to be in their own class, works fine just within your sketch (which is a class as well).

best,
andi
Re: Duplicate Method Declaration
Reply #3 - Mar 9th, 2008, 5:46am
 
So "color is seen and interpreted as an int".

That's not good for me in this case.
I will have to rethink my methods.
Re: Duplicate Method Declaration
Reply #4 - Mar 9th, 2008, 6:39pm
 
I'm not sure if that is the problem, although it seems like it is.

Why won't you make a simple class and overload there? Try it and see if it works. If it doesn't then we can ask fry if the type "color" is treated as an int, although it shouldn't be like so. Even when color might be an int, the type is named different.
Re: Duplicate Method Declaration
Reply #5 - Mar 9th, 2008, 7:46pm
 
a follow up on my previous post. an explanation regarding color and int can be found in the PdePreprocessor
Quote:
compiler.color_datatype 'color' is aliased to 'int' as a datatype to represent ARGB packed into a single int, commonly used in p5 for pixels[] and other color operations. this is just a search/replace type thing, and it can be used interchangeably with int.

when your sketch gets compiled, the PdeEmitter will replace the color datatype with int.
Re: Duplicate Method Declaration
Reply #6 - Mar 16th, 2008, 2:42am
 
So to reduce the overloading, I now use "grid()" and "gridC()".
It's not a perfect solution, but it's not too bad either.
Code:
void setup() { 
size(200,200);
background(153);
noLoop();
}//setup()

void draw() {
}//draw()

void gridC(int x0, int y0, int w, int h, int d, color c) {
//.TODO.
}

void grid(int x0, int y0, int w, int h, int d) {
gridC(x0,y0,w,h,d,color(20,100,100));
}

void gridC(int w, int h, int d, color c) {
gridC(0,0,w,h,d,c);
}

void grid(int w, int h, int d) {
gridC(0,0,w,h,d,color(20,100,100));
}

void grid(int x0, int y0, int w, int h) {
gridC(x0,y0,w,h,10,color(20,100,100));
}

void grid(int w, int h) {
gridC(0,0,w,h,10,color(20,100,100));
}
Page Index Toggle Pages: 1