0

我需要在 for 循环的每个语句中打印一些内容。因为我的程序在 for 循环中崩溃了。所以我尝试在 for 循环中添加跟踪语句,

for (  ICollection::const_iterator iter = pCol->begin(NULL),OutputDebugString(L"One"); iter != pCol->end(NULL); ++iter)
        { //see OutputDebugString

我收到以下错误,

错误 1 ​​错误 C2664: 'IIteratable::ConstIterator::ConstIterator(std::auto_ptr<_Ty>)' : 无法将参数 1 从 'const wchar_t [4]' 转换为 'std::auto_ptr<_Ty>' filename.cpp 629

现在我在示例应用程序中尝试了同样的事情,它工作正常,

void justPrint(std::string s)
{
    cout<<"Just print";
}

int main()
{
    int i;

    for(i = 0,justPrint("a"); i<3; i++)
    {

    }

    return 0;
}

OutputDebugString 和 justPrint 都返回 void,我在代码中做错了什么。

4

1 回答 1

0

OutputDebugString错误是您正在分配to的返回值iter。尝试交换顺序,因为逗号运算符 ( ,) 给出了最后一个值,在这种情况下,是 的返回值OutputDebugString

for (  ICollection::const_iterator iter = (OutputDebugString(L"One"), pCol->begin(NULL)); iter != pCol->end(NULL); ++iter)
    { //see OutputDebugString

但这为什么你需要OutputDebugString那里?您可以在 for 循环之前添加它以避免混淆。


如果需要在 之后打印调试字符串pCol->end(NULL),可以使用辅助函数。

static ICollection::const_iterator begin_helper(SomeType &pCol) {
    auto iter = pCol->begin(NULL);
    OutputDebugString(L"One")
    return iter;
}

for (  ICollection::const_iterator iter = begin_helper(pCol); iter != pCol->end(NULL); ++iter)
    { //see OutputDebugString
于 2015-09-22T05:12:16.443 回答