   Arg!  How to pass a string to a function?
  I have searched.  I promise.  Yes it's basic, but for some reason I cannot get it to work.I first was just trying to use a char*(And I'm sure I did several things wrong but I just wanted something that worked before making it right).Anyway, I didn't have string included so I wasn't using that class for awhile.So here is the function declarationvoid createProjectile(GLfloat positionX, GLfloat positionY, GLfloat positionZ, GLfloat angle = 0.0f, GLfloat velocity = 1.0f, string Type="default")I started with that as a char* Type isnteadBut anyway, this is the call to the functionHeroProjectileBase.createProjectile(curPosition.x+curX, curPosition.y+curY, myPosition.z, orientation,(string)"Hero");(Yeah I know, it's not the best way to cast but it should work, actually string("Hero") is probably the right way to do it but... shouldn't matter here)Anyway.. what the heck is going on with this error?Quote:/home/xanas/Projects/KDevelop/nextstep/src/GunPort.h:15: error: no matching function for call to `HeroProjectiles::createProjectile(GLfloat, GLfloat, GLfloat&, GLfloat&, std::string)'/home/xanas/Projects/KDevelop/nextstep/src/Projectile.h:59: note: candidates are: void HeroProjectiles::createProjectile(GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, std::string)It only occurs when I add the string at the end.  When I leave out that parameter it works just fine.So how do I pass a parameter?  It's such a "simple" problem but C++ can be so incredibly frustrating sometimes.I don't know how the stupid compiler is deciding that the 3rd & 4th params are references instead of Glfloats, which is simply not the case..LOL, more playing with it got an even more stupid error (as in, utterly incomprehensible)I believe, if I remember correctly, that you are supposed to use references whenever possible to minimize copying of variables.  In practice I don't do it much (wasn't considering optimization top priority at this time), but anyway.  I went with it here due to the problem and made it take references (since the values aren't being modified).  I think I'm supposed to make it const also (so I'm not capable of modifying anything I don't want to) but meh.Anyway, here's my new lovely declaration and error..void createProjectile(GLfloat& positionX, GLfloat& positionY, GLfloat& positionZ, GLfloat& angle = 0.0f, GLfloat& velocity = 1.0f, string Type="default")And the error isQuote:void createProjectile(GLfloat& positionX, GLfloat& positionY, GLfloat& positionZ, GLfloat& angle = 0.0f, GLfloat& velocity = 1.0f, string Type="default")I stop passing that string("Hero") and it all goes away, I put it back in and compiler error.
  Try not casting to a string. The string type should take a plain series of characters. Do this:HeroProjectileBase.createProjectile(curPosition.x+curX, curPosition.y+curY, myPosition.z, orientation,"Hero");;Quote:/home/xanas/Projects/KDevelop/nextstep/src/GunPort.h:16: error: no matching function for call to `HeroProjectiles::createProjectile(GLfloat&, GLfloat&, GLfloat&, GLfloat&, const char[5])'/home/xanas/Projects/KDevelop/nextstep/src/Projectile.h:59: note: candidates are: void HeroProjectiles::createProjectile(const GLfloat&, const GLfloat&, const GLfloat&, const GLfloat&, const GLfloat&, std::string)I get this error now.I did update the function to use const& since I don't plan on changing the values.  I also had to move those values out that I was adding (since you can't add values in the function call if you are going to use references).So here is the function that the main call is in modified, with the problem when I try to pass a string, and without a problem when I don't....class HeroGunPort{position myPosition; // offset position from center of the ship quadint repeat_rate; // repeat rate of the gun port, amount of time it waits in ticks (higher = longer wait)int Ticks;public:HeroGunPort(position offSetPosition, int fire_rate){ Ticks = 0; myPosition = offSetPosition; repeat_rate = fire_rate;}// we need to get the current orientation (angle) to precisely determine the place to fire from and direction // the projectile will travelvoid attemptToFire(GLfloat orientation, position curPosition){// Has it been enough time since we last fired?if(Time - Ticks > repeat_rate){// Do we have ammo?  Code will go here to check amount of ammunitionGLfloat curX = cos(orientation/180*PI) * myPosition.x + -sin(orientation/180*PI) *myPosition.y;GLfloat curY = cos(orientation/180*PI) * myPosition.y + sin(orientation/180*PI) *myPosition.x; curPosition.x+=curX; curPosition.y+=curY; HeroProjectileBase.createProjectile(curPosition.x, curPosition.y, myPosition.z, orientation,"Hero");Ticks = Time;}}};Feel free to make commentary about unrelated improvements I can make btw.Also, the reason I'm trying to pass the string is to define "types" of projectiles being fired.  I just want to color the projectile differently depending on the type.  I know, I could do this with an integer or a number of other types of parameters, but I wanted to use a string here and.. it really should work.. I can't for the life of me figure out why not.For fun here is the version string of my compilerQuote:gcc (GCC) 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9)Copyright (C) 2004 Free Software Foundation, Inc.This is free software; see the source for copying conditions.  There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.Edit....Ugh, I'm an idiot lol. Count parameters closely after posting.. I haven't tested this yet and it still may not work but my other function has 5 glfloats (one called velocity I haven't been using yet) and not 4.. that's the problem.  I was only defining 4 of them.Sorry..;  1) Try this:void createProjectile(GLfloat positionX, GLfloat positionY, GLfloat positionZ, GLfloat angle = 0.0f, GLfloat velocity = 1.0f, const string& Type="default")Note that you don't need to make the other parameters references, as passing it by reference (essentially passing a pointer to them) is 4 bytes, which is the same size of floats (and unsigned longs, etc). However, string can be rather large, so we pass that by const reference.2) Make sure UNICODE isn't defined, although that would give you a different sort of error. If it is, you need to pass as L"default".Hope that helps.   ;  Oh ok, that makes sense (about the pointers vs. the other types).Anyway, I do have it working just fine now.  The problem I was having is that my paramaters have defaults, and I had forgotten about this velocity parameter I wasn't using yet.  Since it also had a default, but I was skipping that when I was calling the function it was throwing this error.I'm sure it would have worked a long time ago had I noticed this.  But I did not...(That's the problem with doing this at 1 AM, lol)Thanks!   ;  It'd most annoying catagorizing things based on strings: takes more memory, longer to evaluate, might be a null pointer,  but of course these are minor errors.   ;  Perhaps, but it's more readable like this, which is more important at this point (at least to me) than it being optimal.  I can always update it later to be a char datatype or something else appropriately small, and then just enumerate a list.  But why do I need to do that at this point?  That's my current opinion on the topic anyway.;  Well, if you feel more confortable with strings, than by all means, do so, i mean, it's not devastatingly bad, infact only inconvient using them, so yeah, it's cool using them, i used them for aquirring the pointer to bones in my figure hiarchy.   ;Quote:Original post by XanasPerhaps, but it's more readable like this, which is more important at this point (at least to me) than it being optimal.  I can always update it later to be a char datatype or something else appropriately small, and then just enumerate a list.  But why do I need to do that at this point?  That's my current opinion on the topic anyway.In my opinion, it's quite a bit less readable.  It's not being used in the way strings normally are (read from files, written to files, read from standard input, written to standard output, sent or received to/from another computer through a network, etc), which causes confusion.  Just use something like this:enum ProjectileType {    DEFAULT,    HERO,    // ...};;  Well, I didn't mean easily readable by others, just by me (though I get your point).I already changed it before re-reading the thread to an enum just because I changed my mind abou it.  At the time I hadn't used one before.