formatting strings.

This is a discussion on formatting strings. within the C++ Programming forums, part of the General Programming Boards category; hello. i've got a monkey of a problem with a program that converts some text into a single string. Exerpt ...

  1. #1
    Unregistered
    Guest

    formatting strings.

    hello. i've got a monkey of a problem with a program that converts some text into a single string. Exerpt follows:

    // include files
    #include <iostream>
    #include <sstream>
    #include <string>
    #include "Room.h"
    #include "String.h"
    using namespace std;

    and the problem area of code in the program:

    string Room::display_room()
    {

    ostringstream format_message;

    format_message << "Room details" << endl
    << "===========" << endl
    << endl
    << "Room Name : " << room_name << endl
    << "Length : " << length << endl
    << "Width : " << width << endl
    << "Height : " << height << endl;

    string a_string = format_message.str();
    cout << a_string << endl;
    return a_string;
    }

    there's also a header file but i've made the same alterations (String display_room, and pretty much the same files included).

    i get errors like 'string not defined'. how can I get this routine to return string without error?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Can you post the exact errors? I get the feeling your compiler is choking on your attempt to return a local variable from a function. The variable goes out of scope when you leave the function and is wiped out. But I can't be sure without more complete code to test with and/or error messages.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    5
    k...here we go. This was done on a linux by the way...

    CC Room.cc
    "Room.h", line 34: Error: string is not defined.
    "Room.cc", line 133: Error: Room::display_room(), returning std::basic_string<char, std::char_traits<char>, std::allocator<char>>, was previously declared returning int.
    2 Error(s) and 6 Warning(s) detected.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Check your prototype for display_room(), apparently the compiler thinks that it is supposed to return an int and you actually return a string.

    Of course the other error seems to say that you forgot to include <string> in your .h file with the class. In the absence of the proper type that is declared the compiler will assume that you meant int by default.

    Check those two items and let me know if one of them was missing or incorrect compared to the implementation of your method.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    9 out of 10 doctors agreeing on anything seems like a miracle in and of itself. I'd vote for the loner.

  6. #6
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    I'm guessing it's got something to do with the "string" make sure you have all the required headers and capitalization correct. It's telling you it's not defined, so figure out why.

    In debugging, the first error is always right. The rest are many times related to that first one. So solve the first error and you'll be on your way.

  7. #7
    Unregistered
    Guest
    ok... i've finally had time to get back to doing this. Been to see my tutor who helped me out a bit BUT i'm still getting fundemental errors.


    "Room.cc", line 138: Error: The operation "std::basic_ostream<char, std::char_traits<char>> << String" is illegal.
    "Room.cc", line 143: Error: Cannot assign std::basic_string<char, std::char_traits<char>, std::allocator<char>> to String without "String:perator=(const String&)";.
    "Room.cc", line 144: Error: The operation "std::basic_ostream<char, std::char_traits<char>> << String" is illegal.

    here's my code as of late:

    // display room details
    String Room::display_room()
    {
    String a_string;
    String rn1 = "Room Name : ";
    rn1 += room_name;

    ostringstream format_message;

    format_message << "Room details" << "\n"
    << "===========" << "\n"
    << "\n"
    << rn1 << endl
    << "Length : " << length << "\n"
    << "Width : " << width << "\n"
    << "Height : " << height << "\n";

    a_string = format_message.str();
    cout << a_string;
    return a_string;
    }

    Aaaaaaaaaanybody?

  8. #8
    Unregistered
    Guest
    apparently you need to overload the assignment operator, not sure about the other errors though.

  9. #9
    Unregistered
    Guest

    Question

    Originally posted by Unregistered
    apparently you need to overload the assignment operator, not sure about the other errors though.
    erm... little more info please... help!

  10. #10
    Unregistered
    Guest
    look at this line:

    a_string = format_message.str();

    The compiler is saying you cannot assign the type/class associated with format_message.str() to a_string because the assignment operator isn't overloaded in the type/class a_atring to allow you to do that.

    I think the first error has to do with your attempt to do this:

    format_message << m1

    I thinkd it is saying the compiler can't find an overloaded << for the class of format_message to allow you to do that either, although I am less sure of that error message.

    The last error message appears to be associated with this line:

    cout << a_string;

    and would seem to indicate the compiler can't find an overloaded << operator to allow you to use the << to output the contents of a String, either.

    If you have written an overloaded assignment operator (=) and an overloaded << operator for your String class, then the compiler can't find them. If you haven't written them, you need to. If you are not familiar with the concept of overloading operators please so indicate.

  11. #11
    Unregistered
    Guest
    w00t!!! i figured it out! it works now! after all that i'd made a syntax error earlier in the code.... whoops...

    also u were right about the a_string thing, but i've fixed that too. no worries m8.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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