• Advertisement

Archived

This topic is now archived and is closed to further replies.

HELP.

This topic is 5101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have pretty basic source here...and i get a pretty basic error..i have no idea whats wrong..can someone help me?

#include <iostream>

 int playerhealth,playerdamage,playerexp,playergold,monsterhealth,monsterdamage; // globals

 char name,pclass;
 char  prace;
 int race,nclass; // mmm more globals


char  SetRace(char crace[20])
{
	return crace;
}

 int main()

 {
	 
	 std::cout<<"Battle Magi\n";
	 std::cout<<"By TF \n";
	 std::cout<<"By what are you called?\n";
	 std::cin>>name;
	 std::cout<<"Race:\n";
	 std::cout<<"[1] - Human\n";
	 std::cout<<"[2] - Halfling\n";
	 std::cout<<"[3] - Oger\n";
	 std::cout<<"[4] - Dwarf\n";
	 std::cin>>race;
	 if(race == 1)
	 {
		 SetRace("Human");
	 }

	 return 0;
 }
and heres the error..
: error C2440: 'return' : cannot convert from 'char []' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
[edited by - Xabbu on March 5, 2004 11:38:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
Notice the return type of SetRace(), and the type of what your trying to return?

Edit:
SetRace returns a char, and crace is a char[]

[edited by - Lord Hen on March 5, 2004 11:42:37 PM]

Share this post


Link to post
Share on other sites
Ok, i changed that..stil got a error :-(


SetRace' : cannot convert parameter 1 from 'char [6]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.


[edited by - Xabbu on March 5, 2004 11:42:46 PM]

Share this post


Link to post
Share on other sites
With that, you have the line

SetRace("Human");

your SetRace function accepts only one char now, not an array.

im thinking you should use pointers

char* SetRace(char* crace)
{
return crace;
}

Share this post


Link to post
Share on other sites
Oh that worked, thanks. Now is there anything i should know about preventing a memory leak, or when to delete the pointer?( i have never used pointers). And is there a way to access crace from within main?

[edited by - Xabbu on March 5, 2004 11:52:35 PM]

Share this post


Link to post
Share on other sites
As for accessing crace...you should be storing it somewhere, not just returning it.

I would have put all your global variables in a class, you should learn how to use those too, if you havent.
And the variable that contains the race would be a char*, just set it to the pointer passed into SetRace.



Lord Hen

Share this post


Link to post
Share on other sites

  • Advertisement