-
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
-
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
-
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
-
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());
-
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;