rank1
rank1

Reputation: 1038

map of fstream pointers in c++

I want to have a map of (int, fstream*) and to modify it using some functions. I can easily modify it within main, but if I want to use it by sending the pointer to the fstream I got this compiler error : error C2440: '=' : cannot convert from 'std::fstream' to 'std::basic_fstream<_Elem,_Traits> *'

map<int, fstream*> m;
void func(fstream* f){
m[0] = *f; //compile error
}

int main( int argc, const char* argv[] )
{
fstream f("hi.txt");
func(&f); //error
m[0] = &f;   //work fine
f.close();
system("pause");
}

How can I change it ?

Upvotes: 0

Views: 460

Answers (1)

Ian
Ian

Reputation: 336

Don't dereference the pointer inside your function.

Use

void func(fstream* f){
  m[0] = f; //no more compile errors
}

Upvotes: 3

Related Questions