params by ref

passing float or double paramaters by reference

Page 1 of 1

8 Replies - 1874 Views - Last Post: 06 July 2009 - 08:02 PM Rate Topic: -----

#1 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

params by ref

Posted 18 June 2009 - 04:30 PM

alright i am trying to pass some float vars by reference but i am having some trouble. I dont know what is going on but here is the code that i have got so far.

my function declearation
void ChangeColor(double *, double *, double *);



the vars i am wanted to be edited
double red=0.0;
double green=0.0;
double blue=1.0;



the function call
ChangeColor(&red, &green, &blue);



and now the usage of the function
void ChangeColor(double *red, double *blue, double *green)
{
		if(blue==1.0)
		{
			red=blue*.1;
			blue-=red;
		}
		else
		{
			red=1-blue;
			blue-=.1;
		}
		
		if (red==1.0)
		{
			green=red*.1; 
			red-=green;
		}
		else
		{
			green=1-red;
			red-=.1;
		}
		
		if(green==1.0)
		{
			blue=green*.1;
			green-=blue;
		}
		else
		{
			blue=1-green;
			green-=.1;
		} 
	}



here is a list of my errors
Error	10	error C2601: 'ChangeColor' : local function definitions are illegal	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	110
Error	11	error C2446: '==' : no conversion from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	111
Error	12	error C2440: '==' : cannot convert from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	111
Error	13	error C2296: '*' : illegal, left operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	113
Error	14	error C2297: '-=' : illegal, right operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	114
Error	15	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	114
Error	16	error C2113: '-' : pointer can only be subtracted from another pointer	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	118
Error	17	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	119
Error	18	error C2446: '==' : no conversion from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	122
Error	19	error C2440: '==' : cannot convert from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	122
Error	20	error C2296: '*' : illegal, left operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	124
Error	21	error C2297: '-=' : illegal, right operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	125
Error	22	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	125
Error	23	error C2113: '-' : pointer can only be subtracted from another pointer	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	129
Error	24	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	130
Error	25	error C2446: '==' : no conversion from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	133
Error	26	error C2440: '==' : cannot convert from 'double' to 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	133
Error	27	error C2296: '*' : illegal, left operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	135
Error	28	error C2297: '-=' : illegal, right operand has type 'double *'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	136
Error	29	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	136
Error	30	error C2113: '-' : pointer can only be subtracted from another pointer	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	140
Error	31	error C2114: '-=' : pointer on left; needs integral value on right	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	141



Is This A Good Question/Topic? 0
  • +

Replies To: params by ref

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: params by ref

Posted 18 June 2009 - 04:38 PM

Here, should be understandable and easy to follow: http://www.learncpp....s-by-reference/

However, in short;

void ChangeColor(double *, double *, double *);
should be
void ChangeColor(double &, double &, double &);

ChangeColor(&red, &green, &blue);
should be
ChangeColor(red, green, blue);

void ChangeColor(double *red, double *blue, double *green)
{
		if(blue==1.0)
		{
			red=blue*.1;
			blue-=red;
		}
		else
		{
			red=1-blue;
			blue-=.1;
		}
		
		if (red==1.0)
		{
			green=red*.1; 
			red-=green;
		}
		else
		{
			green=1-red;
			red-=.1;
		}
		
		if(green==1.0)
		{
			blue=green*.1;
			green-=blue;
		}
		else
		{
			blue=1-green;
			green-=.1;
		} 
	}


should be
void ChangeColor(double & red, double & blue, double & green){...}

This post has been edited by Lillefix: 18 June 2009 - 04:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: params by ref

Posted 18 June 2009 - 04:56 PM

You were mixing the * and & together improperly with parameters...
Was This Post Helpful? 0
  • +
  • -

#4 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: params by ref

Posted 18 June 2009 - 05:04 PM

thx a lot. I changed a few other things around but you helped out a lot. can you help me with the math of changing the colors. i am using values 0-1. the colors i am getting are purple and green. I want red green blue and then they fade into one another.
Was This Post Helpful? 0
  • +
  • -

#5 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: params by ref

Posted 18 June 2009 - 05:31 PM

here is the math that is used to change the color
void ChangeColor(double &red, double &green, double &blue)
	{
		int count=0;
		bool sub=true;
		if (sub)
		{
			if(blue==1.0)
			{
				red=blue *.1;
				blue-=red;
			}
			else
			{
				red=1-blue;
				blue-=.1;
			}
			
			if (red==1.0)
			{
				green=red *.1; 
				red-=green;
			}
			else
			{
				green=1-red;
				red-=.1;
			}
			
			if(green==1.0)
			{
				blue=green *.1;
				green-=blue;
			}
			else
			{
				blue=1-green;
				green-=.1;
			}
		}
		count++;
		
		if(count==1000)
		{
			if(sub)
				sub=false;
			else
				sub=true;
			count=0;
		}

		if(!sub)
		{
			if (blue==0.0)
			{
				red=1.0;
				blue=0.1;
			}
			else
			{
				red=1-blue;
				blue+=red;
			}
	
			if(red==0.0)
			{
				green=1.0;
				red=0.1;
			}
			else
			{
				green=1-red;
				red+=green;
			}
			if(green==0.0)
			{
				blue=1.0;
				green=0.1;
			}
			else
			{
				blue=1-green;
				green+=blue;
			}
		}	
		count++;
 }


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: params by ref

Posted 18 June 2009 - 06:27 PM

Even with passing the values correctly, there are some severe logic errors going on here:

void ChangeColor(double &a, double &B)/> {
	if(a==1.0) {
		b = a *0.1;
		a -= b;
	} else {
		b = 1-a;
		a -= 0.1;
	}
}


void ChangeColor(double &red, double &green, double &blue) {
	int count=0;
	bool sub=true;
	// always runs
	// if (sub) {
	ChangeColor(blue, red);
	ChangeColor(red, green);
	ChangeColor(green, blue);
	// }
	count++;
	
	// count = 1, this never gets here
	/*
	if(count==1000) {
		if(sub)
			sub=false;
		else
			sub=true;
		count=0;
	}
	*/

	// sub is always true, this never is run
	/*
	if(!sub)
	{
		if (blue==0.0)
		{
			red=1.0;
			blue=0.1;
		}
		else
		{
			red=1-blue;
			blue+=red;
		}

		if(red==0.0)
		{
			green=1.0;
			red=0.1;
		}
		else
		{
			green=1-red;
			red+=green;
		}
		if(green==0.0)
		{
			blue=1.0;
			green=0.1;
		}
		else
		{
			blue=1-green;
			green+=blue;
		}
	}	
	*/
	count++;
	
	// count == 2
	// and the value is lost
}


Was This Post Helpful? 0
  • +
  • -

#7 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: params by ref

Posted 18 June 2009 - 06:42 PM

I knew that it was some messed up and slopy code. I had just typed it out bout 30 min ago. But I am still not getting the result i want. I am getting a purple and green shown as my background. I want purple and green. but also red and blue. I am going to try somthing else in my code that i didn't post. I'll reply later. thanks for helping me make my code simpler and more easy to manage. I didn't even think about overloading the function.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: params by ref

Posted 18 June 2009 - 07:29 PM

One of your problems is that you're not passing the sub or count.

One of the things you might consider doing is bundling up all the element of your iterator in a struct or class.

struct ColorState {
	double red, green, blue;
	int count;
	bool sub;
};

void ChangeColor(ColorState &cs);



Or, even better, do it with class:
class ColorState {
private:
	int count;
	bool sub;
	double red, green, blue;
public:
	ColorState() : count(0), sub(bool) {}; // I don't know your color defaults
	ColorState(double r, double g, double b );
	void Change();
	double getRed() const { return red; }
	double getGreen() const { return green; }
	double getBlue() const { return blue; }
};


This post has been edited by baavgai: 18 June 2009 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: params by ref

Posted 06 July 2009 - 08:02 PM

ok i took about a month break on this trying to go back to college. I decided to use a sine wave, but i got some syntax errors that are getting really anoying. I've had them b4 but can't seem to fix them. here is my code:

#include <math.h>
#define PI = 3.141592654
void ChangeColor(double &, double &, double &);
//void ChangeColor(double &, double &); isn't used
void ChangeColor_Sine(double &, double &);
...
void ChangeColor(double &red, double &green, double &blue)
{
	ChangeColor_Sine(blue, red);
	ChangeColor_Sine(red, green);
	ChangeColor_Sine(green,blue);
}

void ChangeColor_Sine(double & a, double & B)
{
	a=sin(PI/4); //all of my errors except my last one are here
	b=sin((PI/4)-PI);
}



my errors are this:

Error	10	error C2143: syntax error : missing ')' before '='	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	146
Error	11	error C2661: 'sin' : no overloaded function takes 0 arguments	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	146
Error	12	error C2059: syntax error : ')'	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	146
Error	13	error C2059: syntax error : '='	d:\LeadWerks\Projects\Leason 5\Leason 5\Leason 5\Leason 5.cpp	147



This was a side project but have turned into such a hassel that i am determined to figure it out

~Xtreampb~

This post has been edited by xtreampb: 06 July 2009 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1