Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++]Functions

Started by Killinger May 9, 2005 at 7:01 PM 4 replies 1.3k views
Original Post
Killinger
Killinger
Hey all. I started some experiments with pointers, but I just can't get this to work. The code is pretty obvious, so I didn't comment it. It doesn't seem to like my functions much. Here are the compiling errors I am getting: (39) : error C2144: syntax error : missing ')' before type 'int' (39) : error C2660: 'destroy' : function does not take 0 parameters (39) : error C2059: syntax error : ')' Error executing cl.exe.

#include <iostream>

using namespace std;


class rpg
{
public:
	int damage();
	int destroy(int sword, int *p_sword);
};

int rpg::damage()
{
	int sword = 50;
	int *p_sword = &sword
	cout << "In rpg::damage();...\n";
	cout << "Sword Damage: " << sword << endl;
	cout << "*p_sword Damage: " << *p_sword << endl;
	return sword;
	return *p_sword;
}
int rpg::destroy(int sword, int *p_sword)
{
	cout << "\nIn rpg::destroy();...\n";
	cout << "Sword Damage: " << sword << endl;
	cout << "*p_sword Damage: " << *p_sword << endl;
	cout << "Changing sword damage to 100...";
	sword = 100;
	cout << "\nSword Damage: " << sword << endl;
	cout << "*p_sword Damage: " << *p_sword << endl;
	return 0;
}

int main()
{
	rpg stuff;
	stuff.damage();
	stuff.destroy(int sword, int *p_sword);
	return 0;
}

Thanks alot!
Drakkcon
Drakkcon
When you call a function, don't use the variable type. You're re declaring the function. Instead, pass an actual value or variable to it.

int main(){  int Number = 75;  rpg Stuff;  Stuff.damage();  Stuff.destroy(Number, &Number);  return 0;}
Evil Steve
Evil Steve
I assume line 39 is this line: stuff.destroy(int sword, int *p_sword); (I didn't actually check).
You can't call a function like that, you need to pass it an actual parameter. For example:
int main(){   rpg stuff;   int sword = 42;   stuff.damage();   stuff.destroy(sword,&sword);   return 0;}
chad_420
chad_420
also you have a function with two return statement next to eachother. They are also of different types thats a big nono.
//return sword;//can only be ruturned by functions that return int's//like int somefunc(){    int a;    a = 1;    return a;}//return *p_sword;//can only be used with a function that returns an int pointerint *somefunc(){    int *ptr = NULL;    return ptr;}//Atleast Im pretty sure about that. also//by having two return statements together like you do:	return sword;	return *p_sword;the second will never be executed.however there are situations where you may want/need more than one return statement. probably more in a context like this:if(something) return a;else return b;
SumDude
SumDude
if you want to return 2 items from a function you can use.

int function (int *data)
{
*data = 56;
return 99;
}

and use it like

main()
{
int val1, val2;

val1 = function(&val2);
}

or you can use

int function( int &data)
{
data = 56;
return 67;
}

and use it like

main()
{
int val1,val2;

val1 = function(val2);
}


EDIT: Chad_420: you can return a pointer as a value by dereferencing it.
example:

int function(int *data)
{
return *data;
}


[Edited by - SumDude on May 9, 2005 8:43:19 PM]
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are inte
chad_420
chad_420
hey SumDude what do ya get when you derefernce a pointer? (hint: its not a pointer(in this case atleast)) So you're not returning a pointer at all, but the value it points when your dereference it. Pointers and the values they point at are very different things.

[Edited by - chad_420 on May 10, 2005 12:43:55 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.