std::string problem

This is a discussion on std::string problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a problem with an std::string that is a member of a class. It is simply declared in ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    std::string problem

    Hello,

    I have a problem with an std::string that is a member of a class.

    It is simply declared in the public section of my class like this: "string s;". The class is does not have a cpp file because it only has a bunch of data members. When I try to use it somewhere else like: "cout << myclassinstance->s;" my program crashes. myclassinstance is not null, and I am able to use other members of it of type bool.

    What could be the problem?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Is myclassinstance a pointer to an instance?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Was searching for the problem for hours, and then minutes after I posted I found the problem. Sorry about that.


    The problem was that I had a subclass pointer pointing to a base class instance. I was able to use the members of the baseclass, but not those of the subclass.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by C+/- View Post
    The problem was that I had a subclass pointer pointing to a base class instance. I was able to use the members of the baseclass, but not those of the subclass.
    And you achieved that with what sort of cast?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C+/- View Post
    Was searching for the problem for hours, and then minutes after I posted I found the problem. Sorry about that.
    Yeah, that happens to me all the time. As soon as you ask someone to look at your code, you find the problem yourself.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by cpjust View Post
    Yeah, that happens to me all the time. As soon as you ask someone to look at your code, you find the problem yourself.
    That's why I like to recommend what laserlight did. It often lead the person to the solution.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by medievalelks View Post
    That's why I like to recommend what laserlight did. It often lead the person to the solution.
    Try explaining your problem to the nearest wall. I usually end up solving it in the process of putting it into words.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    Try explaining your problem to the nearest wall. I usually end up solving it in the process of putting it into words.
    LOL. I can't count the number of times I was *convinced* there was a compiler bug, only to sheepishly discover that the problem, as always, was in my code.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by brewbuck View Post
    Try explaining your problem to the nearest wall. I usually end up solving it in the process of putting it into words.
    I SO do this. I'll say it out loud to myself a few times, slower each time. I usually answer myself on the 3rd or 4th iteration. It works! The last word out of my mouth is usually "duh!"

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    6
    Code:
    explain in details
    
    
    #include<iostream.h>
    #include <math.h>
    #include<process.h>
    #include<conio.h>
    // prototype for the the functions
    void getVect(int &s, int &e );
    void addition();
    void subtraction();
    void multiplication();
    void length();
    int main() // function main begins program execution
    {
    // let the user know about the program
    clrscr();
    cout << "\n\n\t program that describes something about vectors.";
    char choice;
    do{
    cout << "\n\n \t\t\t 'M E N U' ";
    cout << "\n\n\t\tAddition : A";
    cout << "\n\t\tSubtraction : S";
    cout << "\n\t\tMultiplication : M";
    cout << "\n\t\tLength : L";
    cout << "\n\t\tQuit : Q";
    cout << "\n\n\t\tEnter your choice ";
    cin >> choice;
    //ff convert the choice to lower case
    if ( choice < 97)
    choice +=32;
    switch ( choice )
    {
    case 'a' : addition();
    break;
    case 's' : subtraction();
    break;
    case 'm' : multiplication();
    break;
    case 'l' : length();
    break;
    case 'q' : //ff pause the screen
    cout << "\n\n\t";
    exit(0);
    break;
    default : cout << "\n\n\tYou have entered wrong choice";
    } //ff end switch
    } while (choice !='q'); // end do-while
    return 0; //ff indicate program executed successfully
    } //ff end of function, main
    
    void getVect( int &f, int &s )
    {
    //fix" prompt and read the values into the vector
    cout << "\n\tEnter the values for the vector:";
    cout << "\n\tFirst Component : ";
    cin >> f;
    cout << "\tSecond Component : ";
    cin >>s;
    cout << "'\tThe vector is [ " << f << " , " << s << " ]";;
    } // end function getVect
    
    void addition()
    {
    //ff declare the variables
    int a1, a2, b1, b2 ;
    //ff get the values for the first vector
    getVect( a1, a2 );
    //ff get the values for the second vector
    getVect( b1, b2 );
    //ff do the addition
    cout<< "\n\n\tThe sum is [ " << ( a1 + b1 )<< "," << ( a2 + b2 ) << "]" ;
    } //ff end function addition
    
    void subtraction()
    {
    // declare the variables
    int al, a2, bl, b2 ;
    //ff get the values for the first vector
    getVect( al, a2 );
    //ff get the values for the second vector
    getVect( bl, b2 );
    //ff do the subtraction
    cout << "\n\n\tIhe difference is [ " << ( al - bl )<<", "<<(a2-b2)<<"]";
    } //fs'] end function subtraction
    
    
    
    void multiplication()
    {
    //fix" declare the variables
    int al, a2, bl, b2 ;
    //fra" get the values for the first vector
    getVect(al, a2) ;
    //fra" get the values for the second vector
    getVect( bl, b2 );
    //ff do the multiplication
    cout << "\n\n\tThe product is [ "  << ( al * bl )<< ", " << (a2 * b2) <<"]";
    }// ff end function multiplication
    void length()
    {
    //ff declare the variables
    int a1, a2;
    float len;
    
    //ff get the values for the vector
    getVect(a1,a2);
    //fir'] find the length
    a1=a1*a1;
    a2=a2*a2;
    len=a1+a2;
    len=sqrt(len);
    cout << "\n\n\tlength is "<<len;
    
    }
    // end function length 
    basharat is online now      
    
    basharat 
    View Public Profile 
    Send a private message to basharat 
    Find More Posts by basharat 
    Add basharat to Your Contacts 
    
     Yesterday, 07:07 PM    #9  
    basharat 
    Registered User
     
    
    Join Date: May 2008
    Posts: 4  Code:
    #include<iostream.h>
    #include <math.h>
    #include<process.h>
    #include<conio.h>
    // prototype for the the functions
    void getVect(int &s, int &e );
    void addition();
    void subtraction();
    void multiplication();
    void length();
    int main() // function main begins program execution
    {
    // let the user know about the program
    clrscr();
    cout << "\n\n\t program that describes something about vectors.";
    char choice;
    do{
    cout << "\n\n \t\t\t 'M E N U' ";
    cout << "\n\n\t\tAddition : A";
    cout << "\n\t\tSubtraction : S";
    cout << "\n\t\tMultiplication : M";
    cout << "\n\t\tLength : L";
    cout << "\n\t\tQuit : Q";
    cout << "\n\n\t\tEnter your choice ";
    cin >> choice;
    //ff convert the choice to lower case
    if ( choice < 97)
    choice +=32;
    switch ( choice )
    {
    case 'a' : addition();
    break;
    case 's' : subtraction();
    break;
    case 'm' : multiplication();
    break;
    case 'l' : length();
    break;
    case 'q' : //ff pause the screen
    cout << "\n\n\t";
    exit(0);
    break;
    default : cout << "\n\n\tYou have entered wrong choice";
    } //ff end switch
    } while (choice !='q'); // end do-while
    return 0; //ff indicate program executed successfully
    } //ff end of function, main
    
    void getVect( int &f, int &s )
    {
    //fix" prompt and read the values into the vector
    cout << "\n\tEnter the values for the vector:";
    cout << "\n\tFirst Component : ";
    cin >> f;
    cout << "\tSecond Component : ";
    cin >>s;
    cout << "'\tThe vector is [ " << f << " , " << s << " ]";;
    } // end function getVect
    
    void addition()
    {
    //ff declare the variables
    int a1, a2, b1, b2 ;
    //ff get the values for the first vector
    getVect( a1, a2 );
    //ff get the values for the second vector
    getVect( b1, b2 );
    //ff do the addition
    cout<< "\n\n\tThe sum is [ " << ( a1 + b1 )<< "," << ( a2 + b2 ) << "]" ;
    } //ff end function addition
    
    void subtraction()
    {
    // declare the variables
    int al, a2, bl, b2 ;
    //ff get the values for the first vector
    getVect( al, a2 );
    //ff get the values for the second vector
    getVect( bl, b2 );
    //ff do the subtraction
    cout << "\n\n\tIhe difference is [ " << ( al - bl )<<", "<<(a2-b2)<<"]";
    } //fs'] end function subtraction
    
    
    
    void multiplication()
    {
    //fix" declare the variables
    int al, a2, bl, b2 ;
    //fra" get the values for the first vector
    getVect(al, a2) ;
    //fra" get the values for the second vector
    getVect( bl, b2 );
    //ff do the multiplication
    cout << "\n\n\tThe product is [ "  << ( al * bl )<< ", " << (a2 * b2) <<"]";
    }// ff end function multiplication
    void length()
    {
    //ff declare the variables
    int a1, a2;
    float len;
    
    //ff get the values for the vector
    getVect(a1,a2);
    //fir'] find the length
    a1=a1*a1;
    a2=a2*a2;
    len=a1+a2;
    len=sqrt(len);
    cout << "\n\n\tlength is "<<len;
    
    }// end function length 
    
    Explain  each and every statement and what functions  insted of the functions are used and what other codes are used inted of that used in the program
    clerify the steps

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    6
    Code:
     i dont brake Your privicy i am a student i write the codes and want details about each and every step and each and every code and each and every function and each and every type of function please explain the codes in details

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Indent.
    Don't use code tags for comments.
    And don't expect anyone to muffle through all of that code and tell you exactly what happens.
    Post a smaller sample.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by basharat View Post
    i dont brake Your privicy i am a student i write the codes and want details about each and every step and each and every code and each and every function and each and every type of function please explain the codes in details
    Quote Originally Posted by Elysia View Post
    Indent.
    Don't use code tags for comments.
    And don't expect anyone to muffle through all of that code and tell you exactly what happens.
    Post a smaller sample.
    Yeah, and while you're at it, don't hijack other peoples threads...
    Read a C++ book. If you still have questions, post your own question in a new thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21