Problem with functions

I created different functions to make the programming easier. Here they are:
 

bool fexists(string filename)
{
ifstream ifile(filename.c_str());
if(ifile) return true;
return false;
}
1
2
3
4
5
6
7
8
9
10
11
int GetNewFileNumber()
{
    for(int i=0;i<MAX_USERS;i++)
    {
            string fnamestring;
            stringstream convert;
            convert << i;
            fnamestring=convert.str()+".txt";
            if(!fexists(fnamestring)) return i;
    }
}


1
2
3
4
5
6
7
8
string GetConvertedFileName(int num)
{
       string fname;
       stringstream convert;
       convert << num;
       fname=convert.str()+".txt";
       return fname;
}


When I try to combine them, I get errors and I can't compile.
1
2
3
4
5
string a=GetConvertedFileName(GetNewFileNumber);

Errors:
invalid conversion from `int (*)()' to `int' 
initializing argument 1 of `std::string GetConvertedFileName(int)'  


Do you know what does he mind?
When a function name is passed as argument it is implicitly converted to the function pointer.

So in this statement

string a=GetConvertedFileName(GetNewFileNumber);

argument GetNewFileNumber is converted to the function pointer. Function

GetNewFileNumber is declared as

int GetNewFileNumber();

so its name is converted to int ( * )()


However function GetConvertedFileName expects an argument of type int because it is declared as

string GetConvertedFileName(int num);

So you specified argument for GetConvertedFileName incorrectly.


I think you wanted to write

string a=GetConvertedFileName(GetNewFileNumber() );
Last edited on
Thank you very much, it works. Although, the real reason for this line was to do an ofstream to write in to file. Now when I do ofstream(GetConvertedFileName(GetNewFileNumber())); it gives me errors again (Im sure that the string a=... was right though as you said). The error I get is: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string)'

EDIT: I made a big mistake, forgot how ofstream works. anyways, this is what I tried next:
1
2
3
4
5
ofstream(userfile);
userfile.open(GetConvertedFileName(GetNewFileNumber()));

Errors:
no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::string)'  
Last edited on
I do not understand what you are doing but

try to change

ofstream(GetConvertedFileName(GetNewFileNumber()));

to

ofstream(GetConvertedFileName(GetNewFileNumber()).c_str());
Last edited on
Ok now, I have been trying some new ways and I found a way to make it work.
1
2
3
4
string a=GetConvertedFileName(GetNewFileNumber());
const char * c = a.c_str();
ofstream(userfile);
userfile.open(c);


You said you dont understand what im doing. To explain, im creating a database with users. This current lines I was asking for were being executed when a user is creating a new contact. It will create something like 6.txt (according to how many accounts were created already(from 0 to 100)).

Now, I can't understand one thing.
This works when its in the int main(), but doesnt when its in a void:
 
if(fexists(GetConvertedFileName(i)))
The code

1
2
3
4
5
6
7
8
9
10
11
int GetNewFileNumber()
{
    for(int i=0;i<MAX_USERS;i++)
    {
            string fnamestring;
            stringstream convert;
            convert << i;
            fnamestring=convert.str()+".txt";
            if(!fexists(fnamestring)) return i;
    }
}


is invalid because it returns nothing in case when there is no such a filename as you are building in the loop.
Topic archived. No new replies allowed.