FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


call by reference (C++)





elcyron
hi everyone,

i just stumbled into my next c++ problem.
only this i think i misunderstood a basic concept.
Code:

class player
{
...
char* name;
...
}
player::player name("newname"),...{ cin >> name; }


compiled perfectly but crashed, the integers worked perfectly. so i thought i'd go another way


Code:

void foo()
{
char* Tchar;
player pl;
...
cout <<"Name of Player: "; cin >> Tchar; pl.s_name(Tchar) ;
...
}



produces errors:
no matching function for call to `player::s_name(char*&)'
candidates are: void player::s_name(const char*&)

i always used "const *& " to make sure the submitted value remains unchanged.
of course i could do a player::s_name(char*&) thereby disable my failsafe
or overload player::s_name but actually i'd like to know where my mistake is.
kv
In your method declaration, change


s_name(const char*&)

to

s_name(const char*)

It works.
bgbg
elcyron wrote:
hi everyone,

i just stumbled into my next c++ problem.
only this i think i misunderstood a basic concept.
Code:

class player
{
...
char* name;
...
}
player::player name("newname"),...{ cin >> name; }


compiled perfectly but crashed, the integers worked perfectly. so i thought i'd go another way

Code:

void foo()
{
char* Tchar;
player pl;
...
cout <<"Name of Player: "; cin >> Tchar; pl.s_name(Tchar) ;
...
}



produces errors:
no matching function for call to `player::s_name(char*&)'
candidates are: void player::s_name(const char*&)

i always used "const *& " to make sure the submitted value remains unchanged.
of course i could do a player::s_name(char*&) thereby disable my failsafe
or overload player::s_name but actually i'd like to know where my mistake is.




kv gave you an answer. Let me just suggest you an excellent book "Thinking in C++" by Bruce Eckel. It is a two volume book that contains EXCELLENT explanations on how passing values and/or objects to/from functions work. The book is available in print or freely downloadable from author's site in various formats: http://www.mindview.net/ .
The book contains lot's of information that will help any beginner programer to become a pretty good intermediate one. To be a really advanced, professional you have to have education much wider than any single book can give you
BruceTheDauber
Code:

class player
{
...
char* name;
...
}
player::player name("newname"),...{ cin >> name; }


You seem to be confusing char*s and strings. A char* is just a pointer, so doing cin >> x, where x is a char* is not going to give you anything like the behaviour you expect if you're looking for a string. In the above, cin will try to overwrite the location in memory where the string literal "newname" is stored, which is probably illegal, depending on your compiler settings relating to how constant strings are stored, and likely to cause buffer overrun if it is not illegal. If it doesn't crash immediately, it will crash later when the nonsense value of "name" is read.

You can use std::string instead, viz.:

Code:

using namespace std;

class player {
   string name;
   public player() {cin >> name; }
};
elcyron
BruceTheDauber wrote:


You can use std::string instead, viz.:


thanks for your explainingeffort, but i figured that out by myself. however the question was not how to do it but how to do it with char*. i needed a char* for another function which wouldn't accept the const char* returned by c_str.
BruceTheDauber
elcyron wrote:
BruceTheDauber wrote:


You can use std::string instead, viz.:


thanks for your explainingeffort, but i figured that out by myself. however the question was not how to do it but how to do it with char*. i needed a char* for another function which wouldn't accept the const char* returned by c_str.


Assuming you know that the function you're trying to use doesn't modify the char*, the best way is to still use std::string, but convert the string to a char* when you call the function that wants a char*.

Supposing the function you want to pass your string to is called "foo", and takes a single char* argument, you could use

Code:

foo(reinterpret_cast<char*>(s.c_str()));


If the function does modify the char*, you will have to create a char* buffer using new or malloc, and either input it using more low-level input methods (such as sscanf), or copy it from your std::string using, e.g., strcpy. After using such a buffer, don't forget to delete it using delete or free.
Related topics
Tutorial: PHP Installed Modules Dynamic Reference Tool
NY Times: A perfect example of lieberals spreading...
Fortran Tutorials(77)
writing COM components in Perl
[c++]random number generator doesnt work
Why do you all hate God?
PHP VS ASP
Male Pregnancy!!!
Is philosophy a science?
COM port programming using PHP
what is 0 divided by 0?
White Privilege, the Palins, US politics
Another “Occupier” makes terroristic threats
Arduino Mega: Programming
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.