The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Class and string problems
Discuss Class and string problems in the C Programming forum on Dev Shed. Class and string problems C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 23rd, 2002, 05:44 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
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.
|

November 24th, 2002, 04:39 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
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";
Last edited by M.Hirsch : November 24th, 2002 at 04:41 AM.
|

November 24th, 2002, 10:17 AM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

Time spent in forums: < 1 sec
Reputation Power: 12
|
|
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.
|

November 24th, 2002, 10:24 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
yes. it is correct.
Quote:
// call my second class
MysecondClass second;
// error line
second.print_msg(aMsg); |
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.
|

November 24th, 2002, 12:00 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: On a screen near you
Posts: 498

Time spent in forums: < 1 sec
Reputation Power: 12
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|