|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Class and string problems
Can anyone help i have a problem with passing a string from one class to another
Code:
//Ok im in my first class and want to pass a string array to my printer class
// string array
char aMsg[] = "My string";
// call my second class
MysecondClass second;
second.print_msg(&aMsg);
// function in second class
void MysecondClass::print_msg(char *ptr){
// do whatever
}
It's not working but i cant see anything wrong Any ideas?
__________________
100 trillion calculations per nanosecond Last edited by Marky_Mark : November 23rd, 2002 at 05:47 PM. |
|
#2
|
|||
|
|||
|
first, "itīs not working" is very vague...
second, itīs a char-array, not a string array. did you try "second.print_msg(aMsg);" without the ampersand? afaik arrays are always passed by reference, never on the stack, this makes an implicit "&" even if you donīt supply it. and: why donīt you make it a char* (the commonly used type for strings in C unless you have a "real" String class) - this will save you from most type-juggling fun ![]() char *aMsg = "My string";
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. Last edited by M.Hirsch : November 24th, 2002 at 04:41 AM. |
|
#3
|
|||
|
|||
|
Tried passing the string array without ampersand and i still keep getting this error
error C2143: syntax error : missing ';' before '.' Code:
// call my second class
MysecondClass second;
// error line
second.print_msg(aMsg);
// this is the print function in second class
void MysecondClass::print_msg(char *str){
}
Is char *str correct to recieve the address of the incomming array Last edited by Marky_Mark : November 24th, 2002 at 10:20 AM. |
|
#4
|
|||
|
|||
|
yes. it is correct.
Quote:
there is no ";" missing. your compiler is broken or you didnīt supply enough code. (probably the second option, but please donīt post the whole code. check for missing brackets.) on the other hand, i am no c++ pro, but i only know of this syntax to instanciate and access classes: MysecondClass *second=new MysecondClass(); second->print_msg(aMsg); but your syntax should be correct too imho. |
|
#5
|
|||
|
|||
|
Thanks for your help
I located the problem to a naming error I was using Code:
Interface GUI; GUI.Display(string); That looks fine but MSVC++ throws an for using GUI as a reference Last edited by Marky_Mark : November 24th, 2002 at 12:03 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Class and string problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|