C++ 是否可以在for循环中使用trace语句?
我需要在for循环的每个语句中打印一些内容。因为我的程序在for循环中崩溃了。所以我尝试在for循环中添加跟踪语句C++ 是否可以在for循环中使用trace语句?,c++,loops,for-loop,trace,comma-operator,C++,Loops,For Loop,Trace,Comma Operator,我需要在for循环的每个语句中打印一些内容。因为我的程序在for循环中崩溃了。所以我尝试在for循环中添加跟踪语句 for ( ICollection::const_iterator iter = pCol->begin(NULL),OutputDebugString(L"One"); iter != pCol->end(NULL); ++iter) { //see OutputDebugString 我有以下错误 错误1错误C2664: “IIteratable
for ( ICollection::const_iterator iter = pCol->begin(NULL),OutputDebugString(L"One"); iter != pCol->end(NULL); ++iter)
{ //see OutputDebugString
我有以下错误
错误1错误C2664:
“IIteratable::construtator::construtator(std::auto_ptr)”
:无法将参数1从“常量wchar\u t[4]”转换为
“std::auto_ptr”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;
}
void justPrint(std::string s)
{
cout错误在于将OutputDebugString
的返回值分配给iter
。请尝试交换顺序,因为逗号运算符(,
)给出了最后一个值,在这种情况下,它是outputdugstring
的返回值
for ( ICollection::const_iterator iter = (OutputDebugString(L"One"), pCol->begin(NULL)); iter != pCol->end(NULL); ++iter)
{ //see OutputDebugString
但是这就是为什么需要在那里使用OutputDebugString
?您可以将它添加到for循环之前,以避免混淆
如果需要在pCol->end(NULL)
之后打印调试字符串,可以使用helper函数
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
pCol的类型是什么?为什么在begin()
和end()中使用NULL
?std::auto_ptr
…一些旧代码需要升级…pCol是某种内部类型的智能指针,TNSmartPtr pCol
传递NULL,因为它需要一些会话对象。所有这些都与内部管理有关。它应该防止任何类型的错误吗?因为没有trace语句,一切都很好。@BryanChen True。但是它的庞大的旧遗留代码。我正在修复小模块上的错误。如果尝试这样做,需要大量的重新分解。在for循环的第一个参数中包含print语句有什么意义?因为它无论如何只执行一次,为了清晰起见,您最好将它移出循环。我想看看赋值和for循环的其他部分,因为我在for循环中遇到错误。我的意思不是在循环语句中,而是在循环本身的定义中。我的错误已经解决了。谢谢。有一个问题,为什么编译器不认为我在这里将justPrint赋值给I,for(I=0,justPrint(“a”); i@PranitKothari赋值的优先级高于逗号运算符,因此它相当于((i=0),justPrint(“a”)
int i=0,justPrint(“a”)
失败,因为它是一个初始化;它相当于int i=(0,justPrint(“a”)