C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 23rd, 2002, 05:44 PM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
  #2  
Old November 24th, 2002, 04:39 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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.

Reply With Quote
  #3  
Old November 24th, 2002, 10:17 AM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
  #4  
Old November 24th, 2002, 10:24 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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.

Reply With Quote
  #5  
Old November 24th, 2002, 12:00 PM
Marky_Mark Marky_Mark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: On a screen near you
Posts: 498 Marky_Mark User rank is Private First Class (20 - 50 Reputation Level)Marky_Mark User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Class and string problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway