我希望得到一些关于我遇到的错误的帮助 - 我搜索过类似的问题,但这些问题并没有真正给我我所追求的东西。下面列出了一个代码片段:
class NewSelectionDlg : public CDialog
{
// Construction
public:
class CProductListBox
{
public:
friend ostream& operator <<(ostream& o, const CProductListBox& b);
};
ostream& operator<<(ostream& o, const CProductListBox& b)
{
std::cout << o.m_lstEclispeProducts;
return o;
}
我有一个包含许多字符串的列表框 - 这些字符串可能会根据所选的其他下拉框而有所不同。我想要将此框中的内容保存到文件中,以及用户从填充该文件的下拉列表中选择的内容。但是我收到以下错误(我正在 VS 2008 中开发)。
错误 C2804:二进制“运算符 <<”
参数过多
错误 C2333:'NewSelectionDlg::operator <<'
:函数声明中出现错误;跳过函数体
我不知道为什么,因为我相信重载运算符的语法是可以的 - 任何人都可以看到我所做的愚蠢或可能错过的任何事情 - 非常感谢您的帮助。
I am hoping to get some help with an error I am getting - I have searched similar questions which havent really gave me what I'm after. A code snippet is listed below:
class NewSelectionDlg : public CDialog
{
// Construction
public:
class CProductListBox
{
public:
friend ostream& operator <<(ostream& o, const CProductListBox& b);
};
ostream& operator<<(ostream& o, const CProductListBox& b)
{
std::cout << o.m_lstEclispeProducts;
return o;
}
I have a list box that contains a number of strings - these can vary depending on other drop down boxes selected. I want to what is in this box to a file as well as what the user selects from the drop downs that popluate it. Howvever I am getting the following error (I am developing in VS 2008).
error C2804: binary 'operator <<'
has too many parameters
error C2333: 'NewSelectionDlg::operator <<'
: error in function declaration; skipping function body
I am not sure why as i belive the syntax of overloading the operator is OK - can anyone see anything I have done stupid or may have missed - Many Thanks for any Help.
发布评论
评论(6)
只需在类定义之外定义它或在声明友谊时在子类中定义它:
或
Just define it outside of class definition or define it in subclass when declaring friendship:
or
运算符 <<
不能是成员函数。第一个参数必须是 std::ostream ;在您的代码中,第一个(隐式)参数是this
指针,即NewSelectionDlg*
类型的对象。您需要将
operator <<
实现为自由函数。operator <<
must not be a member function. The first argument must bestd::ostream
; in your code, the first (implicit) argument is thethis
pointer, i.e. an object of typeNewSelectionDlg*
.You need to implement
operator <<
as a free function instead.您应该在
NewSelectionDlg
的定义和范围CProductListBox
的定义之外相应地定义重载的运算符<<
。You should define the overloaded
operator<<
outside the definition ofNewSelectionDlg
and scopeCProductListBox
accordingly.另外,在 << 中它应该是
b
而不是o
:Also, should it be a
b
rather than ano
in the <<:我采用了你的第二个解决方案。
我仍然收到错误 - 错误 C2039: 'm_lstEclispeProducts' : 不是 'NewSelectionDlg::CProductListBox' 的成员
我不确定为什么这是因为 NewSelectionDlg 类的一部分包含此代码(相关行以粗体显示)-如果您有任何进一步的帮助/建议,这将是一个很大的帮助。谢谢
I went with your second solution.
I am still getting an error - error C2039: 'm_lstEclispeProducts' : is not a member of 'NewSelectionDlg::CProductListBox'
I am not sure as to why this is because part of the NewSelectionDlg class contains this code (relevant line in bold) - if you have any further help/suggestions it would be a great help. Thanks
当您使用声明时:
friend void foo();
您正在做的是在封闭的命名空间范围中声明一个函数。
对于运营商来说也是如此。
When you use the declaration:
friend void foo();
what you are doing is declaring a function in the enclosing namespace scope.
The same goes for operators.