Results 1 to 10 of 10

Thread: 'string' in function prototype

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    'string' in function prototype

    Hi,

    Here is a small portion of code that results in an error message "'string' was not declared in this scope".

    Code:
    #include <string.h>
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    using namespace std;
     
    bool ReadBitmap(string);  //<------------ doesn't like this
    
    int main(int argc, char *argv[])
    {
      if(ReadBitmap("bg.bmp")==false) return 0; //read file...return if not opened
      system("pause");
      return 0;
    }
    Any regular type in the prototype such as char, float, long, etc would be acceptable. But 'strings' aren't well liked. I can do this with 'char' but would like strings since I am a VB dependent programmer. ;-)

    So, I imagine there is something 'special' that I must do for this prototype. It probably involves some C++ wierd thing that I am still learning such as... **, (void), ->,*(char*), ., :: :-)

    Looking for help.
    Regards,
    ChuckB

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    drop the .h on <string.h>

    possibly even for the other ones if that alone doesn't help.

    having .h on the end and using namespace std don't go well together.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hobo,
    Thanks for responding so quickly. That did it. Thanks!

    Regards,
    ChuckB

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Chuck, all of the new standard headers are without the .h. So, you have <vector>, <list>, <iostream>, <string>, <map>, etc, etc. The .h versions are just there for compatability's sake.

    Also, if you are just using one or two classes from the std namespace, you can use this syntax:
    Code:
    #include <vector>
    #include <string>
    using std::vector;
    using std::string;
    Doing it this way keeps your global namespace clear.

    Z.

  5. #5

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi Z,
    Thanks for the tip. I have been studying lots of C/C++ code in the past couple of months and only now is it getting straightened out. I have no idea how I learned to program in the 80's without the internet and forums such as these. :-)

    Incidently, I wrote my first 'vector' program tonight demonstrating various uses of vectors and I finally get it. I like the 'dynamic resizing' with push_back(). Really nice.

    The code in this thread is helping me to read a bitmap which I will place into a vector instead of an array so I can load my first .bmp textures into my OpenGL stuff.

    One last note, do you realize I now have collected three 3" binders full of notes and tutorials I have collected just learning C++? Gotta figure out how to archive this stuff in an easily retrievable manner.

    Regards,
    ChuckB

  6. #6
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    ::taps head:: practice... after a while, it all comes naturally =).

    That is a LOT of notes =).

    Same thing with this twistiness. At first its just a bunch of gobbldegoop, but after a while, how thing interact start to make sense, and it just opens door after door.

    Z.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by ChuckB
    The code in this thread is helping me to read a bitmap which I will place into a vector instead of an array so I can load my first .bmp textures into my OpenGL stuff.
    You're still better off loading it into an array, it's *far* faster, and you shouldn't need the dynamic sizing :-/
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Parksie,
    I didn't know arrays were faster then vectors. BUT, I know see the distinction. Let me know if this is correct or not.

    If I am loading vertices for a 3D model in which the number of vertices may vary from model to model, I can do two things.

    1. Encode the number of vertices in a number at the top of an ASCII/Binary file. Read this number and then create array of that size and load this data into the array.
    2. Read the data into vector when I don't know the size using push_back();

    For bitmap (.bmp) files, I can read the headers, get the actual size of the bitmap image, so there is no guess work. I simply create an array/vector of that size.

    Right?

    Regards,
    ChuckB

  9. #9
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Easier and faster to encode the number at the top of the file.

    Z.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For the points, use a vector.

    For the bitmap, use an array
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width