流运营商的问题 [英] Problem with stream operator
问题描述
我正在使用MFC VC ++ 6.0
我有一个带有CString成员var的简单类''Data''。姓名。
我添加了运营商<<和>> as:
#include< istream>
#include< ostream>
#include< fstream>
std :: ostream& operator<<(std :: ostream& fs,const Data& x)
{
fs<< x.Name;
返回fs;
};
std :: istream&运算符>>(std :: istream& fs,Data& x)
{
LPTSTR p = x.Name.GetBuffer(10);
fs>> p;
x.Name.ReleaseBuffer(-1);
返回fs;
};
现在当我在对话框中使用它时:
无效CIotestDlg :: OnFileOpen()
{
CFileDialog Dlg(TRUE,txt,*。* txt);
if(IDOK == Dlg.DoModal())
{
std :: ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs,myData);
}
UpdateData(FALSE);
}
我收到此错误。 (我也尝试过fs>> myData;)
错误C2665:''>>'':22个重载中没有一个可以转换参数1来自
类型''class std :: basic_ifstream< char,struct std :: char_traits< char> >''
我不知道如何解决这个问题。
< blockquote> 2004年6月2日星期三03:31:51 GMT,David Briggs < s@s.com>写道:
我正在使用MFC VC ++ 6.0
我有一个简单的类''数据''与CString成员var。姓名。
我添加了运营商<<和>> as:
#include< istream>
#include< ostream>
#include< fstream>
std :: ostream& operator<<(std :: ostream& fs,const Data& x)
{
fs<< x.Name;
返回fs;
};
std :: istream&运算符>>(std :: istream& fs,Data& x)
{
LPTSTR p = x.Name.GetBuffer(10);
fs>> p;
x.Name.ReleaseBuffer(-1);
返回fs;
};
现在当我在对话框中使用它时:
void CIotestDlg :: OnFileOpen()
{CFILEDialog Dlg(TRUE," txt"," * .txt");
if(IDOK == Dlg .DoModal())
{
std :: ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs,myData);
}
UpdateData(FALSE);
}
我收到此错误。 (我也尝试过fs>> myData;)
错误C2665:''>>'':22个重载中没有一个可以从
类型''类std ::转换参数1 basic_ifstream< char,struct std :: char_traits< char> >''
我不知道如何解决这个问题。
我对VC6中处理这个问题有一个模糊的回忆,最后
学习(来自Dinkumware的人,如果我记得的话)没有
真正的修复......我想我最终只是重载了运算符<<和>>
除了
常规之外还需要一个std :: ifstream和std :: ofstream。 <<和>>重载。可能有一个更好的解决方案,
但是尝试解决它可能不值得。如果你这么做,你找到了这样的解决方案,请告诉我们你找到的东西;-)
-leor
我确实尝试使用std :: ifstream和std :: ofstream进行重载,并且我得到了相同的
错误。
" Leor Zolman" <乐** @ bdsoft.com>在消息中写道
news:73 ******************************** @ 4ax.com ...2004年6月2日星期三03:31:51 GMT,David Briggs < s@s.com>写道:
我有一个模糊的回忆,在VC6中处理这个,最后学习(来自Dinkumware的人,如果我记得的话)那里没有
真实的修复...我想我刚刚重载了运算符<<和>>
除了
常规之外还需要一个std :: ifstream和std :: ofstream。 <<和>>重载。可能有一个更好的解决方案,
但尝试解决它可能不值得。如果您这样做,并且找到了这样的解决方案,请告诉我们您找到的内容;-)
-leor
2004年6月2日星期三03:31:51 GMT,David Briggs < s@s.com>写道:
我正在使用MFC VC ++ 6.0
我有一个简单的类''数据''与CString成员var。姓名。
我添加了运营商<<和>> as:
#include< istream>
#include< ostream>
#include< fstream>
std :: ostream& operator<<(std :: ostream& fs,const Data& x)
{
fs<< x.Name;
返回fs;
};
std :: istream&运算符>>(std :: istream& fs,Data& x)
{
LPTSTR p = x.Name.GetBuffer(10);
fs>>磷;
如果字符串超过10个字符怎么办?我认为你有未定义的
行为。
x.Name.ReleaseBuffer(-1);
编写运算符>>不是更好对于CString呢?一次读取
字符1并附加它们(检查空格)。
返回fs;
};
现在当我在我的对话框中使用它时:
void CIotestDlg :: OnFileOpen()
{CFileDialog Dlg(TRUE,txt," * .txt );
if(IDOK == Dlg.DoModal())
{
std :: ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs,myData);
这通常写成fs>> myData;
myData是Data类型的CIotestDlg的非const成员吗?
}
UpdateData(FALSE);
}
OnFileOpen定义是否真的出现在运算符>>
定义的下方?或者只有一些朋友声明在Data
类定义中可见?
我收到此错误。 (我也尝试过fs>> myData;)
错误C2665:''>>'':22个重载中没有一个可以从
类型''类std ::转换参数1 basic_ifstream< char,struct std :: char_traits< char> >''
我不知道如何解决这个问题。
看起来像名字查找问题。上面的简单改变可能很好地解决了这个问题。否则,一个最小的完整示例将使其更容易修复。
如果您只有朋友声明,您还应该声明
运算符>>数据类声明之外的函数。
Tom
-
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C常见问题解答: http://www.eskimo .com /~scs / C-faq / top.html
I am using MFC VC++ 6.0
and I have a simple class ''Data'' with a CString member var. Name.
I added operator << and >> as:
#include <istream>
#include <ostream>
#include <fstream>
std::ostream& operator<<(std::ostream& fs, const Data& x )
{
fs << x.Name;
return fs;
};
std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};
Now when I go to use this in my dialog:
void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);
}
UpdateData( FALSE );
}
I get this error. (I also tried just fs >> myData;)
error C2665: ''>>'' : none of the 22 overloads can convert parameter 1 from
type ''class std::basic_ifstream<char,struct std::char_traits<char> >''
I don''t know how to fix this.
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> wrote:
I am using MFC VC++ 6.0
and I have a simple class ''Data'' with a CString member var. Name.
I added operator << and >> as:
#include <istream>
#include <ostream>
#include <fstream>
std::ostream& operator<<(std::ostream& fs, const Data& x )
{
fs << x.Name;
return fs;
};
std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
x.Name.ReleaseBuffer(-1);
return fs;
};
Now when I go to use this in my dialog:
void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);
}
UpdateData( FALSE );
}
I get this error. (I also tried just fs >> myData;)
error C2665: ''>>'' : none of the 22 overloads can convert parameter 1 from
type ''class std::basic_ifstream<char,struct std::char_traits<char> >''
I don''t know how to fix this.
I have a vague recollection of dealing with this in VC6, and finally
learning (from someone at Dinkumware, if I recall) that there''s no
real fix...I think I just ended up overloading operators << and >> to
take a std::ifstream and std::ofstream in addition to the
"conventional" << and >> overloads. There may be a better solution,
but it may not be worth the trouble to try to figure it out. If you
do, and you find such a solution, let us know what you find ;-)
-leor
I did try overloads with std::ifstream and std::ofstream and I got the same
error.
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:73********************************@4ax.com...On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> wrote:
I have a vague recollection of dealing with this in VC6, and finally
learning (from someone at Dinkumware, if I recall) that there''s no
real fix...I think I just ended up overloading operators << and >> to
take a std::ifstream and std::ofstream in addition to the
"conventional" << and >> overloads. There may be a better solution,
but it may not be worth the trouble to try to figure it out. If you
do, and you find such a solution, let us know what you find ;-)
-leor
On Wed, 02 Jun 2004 03:31:51 GMT, "David Briggs" <s@s.com> wrote:
I am using MFC VC++ 6.0
and I have a simple class ''Data'' with a CString member var. Name.
I added operator << and >> as:
#include <istream>
#include <ostream>
#include <fstream>
std::ostream& operator<<(std::ostream& fs, const Data& x )
{
fs << x.Name;
return fs;
};
std::istream& operator>>( std::istream& fs, Data& x )
{
LPTSTR p = x.Name.GetBuffer( 10 );
fs >> p;
What if the string is longer than 10 chars? You have undefined
behaviour I think.
x.Name.ReleaseBuffer(-1);
Wouldn''t it be better to write an operator>> for CString too? Read the
characters 1 at a time and append them (checking for whitespace).
return fs;
};
Now when I go to use this in my dialog:
void CIotestDlg::OnFileOpen()
{
CFileDialog Dlg(TRUE, "txt", "*.txt");
if(IDOK == Dlg.DoModal())
{
std::ifstream fs;
fs.open(Dlg.GetPathName());
operator>>(fs, myData);
That''s normally written fs >> myData;
Is myData a non-const member of CIotestDlg of type Data?
}
UpdateData( FALSE );
}
Does the OnFileOpen definition really appear just below the operator>>
definitions? Or are only some friend declarations inside the Data
class definition visible?
I get this error. (I also tried just fs >> myData;)
error C2665: ''>>'' : none of the 22 overloads can convert parameter 1 from
type ''class std::basic_ifstream<char,struct std::char_traits<char> >''
I don''t know how to fix this.
Looks like a name lookup issue. The simple change above might well fix
it. Otherwise, a minimal complete example would make it easier to fix.
If you only have friend declarations, you should also declare the
operator>> functions outside the class declaration for Data.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
这篇关于流运营商的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!