所以我的代码正在编译好 - 但它没有做我所希望的:(。
我试着尽可能地解释这个 -
下面是我写入磁盘上文件的代码。
void NewSelectionDlg::PrintInfoFile()
{
**CProductListBox b;**
ofstream outdata;
outdata.open("test.dat", ios::app); // opens the file & writes if not there. ios:app - appends to file
if( !outdata )
{ // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
outdata << m_strCompany << endl;
outdata << m_strAppState << endl;
outdata << m_strPurpose << endl;
outdata << m_strChannel << endl;
outdata << m_strProductName << endl;
**outdata << b << endl;**
outdata << endl;
outdata.close();
return;
}
我关注的关键线是Bold。我想打印出一个类CProductListBox。现在因为这不是一个字符串等我知道我必须覆盖&lt;&lt;为了能够做到这一点。所以我的CProductListBox类看起来像这样:
class CProductListBox : public CListBox
{
DECLARE_DYNAMIC(CProductListBox)
public:
CProductListBox();
virtual ~CProductListBox();
**friend std::ostream& operator<< (std::ostream& o, const CProductListBox& b)
{
return o;
}**
我再次强调了我认为重要的东西 - 它不会在输出文件上打印任何东西,遗憾的是我希望它能打印出b(CProductList类)中的内容。
任何人都可以看到我可能遗失的蠢事 - 非常感谢,
Colly(爱尔兰)
答案 0 :(得分:4)
您的运营商&lt;&lt;不包含任何试图打印任何内容的代码。
friend std::ostream& operator<< (std::ostream& o, const CProductListBox& b)
{
o << b.SomeMember << b.AnotherMember;
return o;
}
答案 1 :(得分:1)
你的operator<<
被调用,但它没有做任何事情。它只返回流。
如果要将数据写入流,则需要编写代码以将数据写入流中。
答案 2 :(得分:0)
为此,您需要在
中提供一些代码friend std::ostream& operator<< (std::ostream& o, const CProductListBox& b)
{
return o;
}
这样的东西会写b的“名字”(假设b每次你向ostream写“b”时都有一个返回std :: string的getName())。
friend std::ostream& operator<< (std::ostream& o, const CProductListBox& b)
{
o << b.getName();
return o;
}