是否可以在 for 循环中使用 trace 语句?

Will it be possible to use trace statement in for loop?

我需要在 for 循环的每个语句中打印一些东西。因为我的程序在 for 循环中崩溃了。所以我尝试在for循环中加入trace语句,

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

我收到以下错误,

Error 1 error C2664: 'IIteratable::ConstIterator::ConstIterator(std::auto_ptr<_Ty>)' : cannot convert parameter 1 from 'const wchar_t [4]' to '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 returns 无效,我的代码有什么问题。

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

的 returned 值
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