#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14

    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?
    Last edited by Marky_Mark; November 23rd, 2002 at 05:47 PM.
    100 trillion calculations per nanosecond
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14
    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.
    100 trillion calculations per nanosecond
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    yes. it is correct.
    // 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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    14
    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.
    100 trillion calculations per nanosecond

IMN logo majestic logo threadwatch logo seochat tools logo