Thread: How to create a function to return a std::string, what return type is needed?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    How to create a function to return a std::string, what return type is needed?

    What return type is needed to return a c++ style string?
    Code:
    ???? GetObject(std::string Option);
    {
    	std::string DirOrFileName;
    	std::cout"Please enter "<<Option;
    	getline(std::cin,DirOrFileName,'\n');
    	return DirOrFileName;
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    std::string GetObject(std::string Option) // No semi-colon
    {
    	std::string DirOrFileName;
    	std::cout << "Please enter "<< Option; // Blah blah blah
    	std::getline(std::cin,DirOrFileName,'\n'); // Blah blah blah
    	return DirOrFileName;
    }

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Replace ???? with std::string.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    It doesn't work, I tried. I get "expected unqualified-id before '{' token"
    expected ',' or ';' before '{' token

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    are you #including <string>?

    post the code and mark the line that's giving you an error.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bikr692002
    It doesn't work, I tried. I get "expected unqualified-id before '{' token"
    expected ',' or ';' before '{' token
    As pointed out already, get rid of the unexpected semicolon.
    Code:
    std::string GetObject(std::string Option);
    {
    	std::string DirOrFileName;
    	std::cout<<"Please enter "<<Option;
    	getline(std::cin,DirOrFileName,'\n');
    	return DirOrFileName;
    }
    Last edited by Dave_Sinkula; 04-09-2006 at 04:30 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by Dave_Sinkula
    As pointed out already, get rid of the unexpected semicolon.
    Code:
    std::string GetObject(std::string Option);
    {
    	std::string DirOrFileName;
    	std::cout<<"Please enter "<<Option;
    	getline(std::cin,DirOrFileName,'\n');
    	return DirOrFileName;
    }
    omg haha thanks, didnt see that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM