Problem with Functions/Classes

This is a discussion on Problem with Functions/Classes within the C++ Programming forums, part of the General Programming Boards category; I am using strings in a program I am writing, however whenever I try to make a string the return ...

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19

    Problem with Functions/Classes

    I am using strings in a program I am writing, however whenever I try to make a string the return type of the function, I get tons of errors saying that its not a real type.

    How would I make a string a return type?
    BTW I am using minigw and codeblocks

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Post one of the function which you return string

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    #include <string>
    std::string myfunc(...)

    If that doesn't work, show the appropriate code.
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    okay here is a function that doesn't even have string as a return type, but uses string and has it in the header, but it gets errors on every variable declared as a string!

    Code:
    int Player::inventoryCatalogLookUp(int searchNumber)
    {
        string returnString;
        string fileCatalogName = "\\inventoryCatalogy\\";
        fileCatalogName.append(searchNumber);
        // reading a text file
        string line;
        ifstream myfile (fileCatalogName);
        if (myfile.is_open())
        {
            while (! myfile.eof() )
            {
                getline (myfile,line);
            }
            myfile.close();
            return stringToInt(line);
        }
        else
        {
          //unable to openFile
          return -1;
        }
    }
    Please help

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Except that...
    ifstream myfile (fileCatalogName);
    ...should be...
    ifstream myfile (fileCatalogName.c_str());
    ...there is little to tell the error.
    Post the smallest possible compilable example that demonstrates the problem. And make sure you've included <string> before you use it anywhere.
    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.

  6. #6
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    when you #include <string> you get access to std::string. If you put using namespace std; after your #includes, then you can declare a string with string my_string;, otherwise you must declare strings with std::string my_string;. I would imagine getline() is giving you an error as well, for the same reason.
    C+/- programmer extraordinaire

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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