Strings - functions

This is a discussion on Strings - functions within the C++ Programming forums, part of the General Programming Boards category; I have declared the following function prototypes in "functions.h". Here is the contents of the file: Code: #ifndef FUNCTIONS_H #define ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Strings - functions

    I have declared the following function prototypes in "functions.h". Here is the contents of the file:
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    #include <string>
    
    int open_source_file(string);
    int display_source_file(string);
    int display_story_file(string);
    void menu();
    
    #endif
    I get an error when I try to compile this which says that string is an undeclared identifier.
    Maybe I am doing something wrong, but whar I mean by the first 3 function prototypes is that they all take a string as an argument.

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Prefix all occurances of string with std::.
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    #include <string>
    
    int open_source_file(std::string);
    int display_source_file(std::string);
    int display_story_file(std::string);
    void menu();
    
    #endif
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    10
    Thanks, it works fine now, but I am getting a weird error with the actual function, like this one:
    Code:
    int display_source_file(std::string fileName)
    {
    	ifstream file(fileName);
    	string current_line;
    	
    	// read one line at a time from the file
    	// and print it
    	while(getline(file, current_line))
    	{
    		cout << current_line << endl;
    	}
    }
    The error I am getting is:
    error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'

    Thanks

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The ifstream constructor takes a const char*, not a string. Use the strings c_str member to pass to the ifstream constructor.

    Code:
    ifstream file(fileName.c_str());
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    As with string, you must prefix cout and endl with std:: if you have not included the namespace std.
    Code:
       std::cout << current_line << std::endl;
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings and functions
    By simo_mon in forum C Programming
    Replies: 8
    Last Post: 09-16-2008, 05:18 AM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. Replies: 2
    Last Post: 04-13-2003, 08:40 PM

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