Return a string in a class

This is a discussion on Return a string in a class within the C++ Programming forums, part of the General Programming Boards category; This seems like it would be easy to solve, but im not sure how to do it. I made a ...

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Return a string in a class

    This seems like it would be easy to solve, but im not sure how to do it. I made a class called Person, and in it, I have a getName(). This is what it looks like:

    Code:
    #include <iostream>
    #include <string>
    
    class Person{
    
              public:
                       std::string setName(std::string name1){
                                           name1 = name2;
                                           }
                       std::string getName(){
                                   getline(std::string, name2);
                                    }
    
             private:
                       std::string name2;
    };

    With the above code I get an error in the getName part. I figured it would be wrong when I was typing it, but I tried anyways. Im not sure how to write that part to where it works. Right now I get this error:
    Code:
    In member function `std::string Person::getName()':
    error: expected primary-expression before ',' token
    I have a good feeling the std::string part is whats wrong, but im not positive. Can anyone explain to me the correct way to write that part?
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I'm guessing you were working to this or something like that:
    Code:
    #include <iostream>
    #include <string>
    
    class Person{
      public:
        std::string setName(std::string name1){
          name2 = name1;
          return name2;
        }
        std::string getName(){
          std::getline(std::cin, name2);
          return name2;
        }
      private:
        std::string name2;
    };

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Well, firstly in setname, you don't have to return anything, so change the return type to void. And also:

    name1 = name2;

    should be the opposite way around. name2 is the variable you want to set.

    just say:

    return name2;

    inside getName.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yes, type 'string' is not a stream, so you can't put it in the first parameter of getline(); Like Noir posted, you would return just like any other type.

  5. #5
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Oh ok. Thanks for clearing that up for me. I got it now.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    setName(std::string name1)

    better pass parameter as const reference to avoid unneeded copying of the source string to the temp var during calling this function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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