2013-04-10 96 views
0

我想要一個(int,fstream *)映射並使用某些函數對其進行修改。我可以在main中輕鬆修改它,但是如果我想通過將指針發送到fstream來使用它,我得到了這個編譯器錯誤:error C2440:'=':無法從'std :: fstream'轉換爲'std :: basic_fstream < _Elem,_Traits> *'C++中fstream指針的映射

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"); 
} 

我該如何改變它?

回答

3

不要在您的函數中取消引用指針。

使用

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

明顯。謝謝 – rank1 2013-04-10 08:49:15