2013-03-23 131 views
1

所以我在一個對象的方法中,但cout語句完全沒有輸出。cout不會產生輸出

#include <iostream> 
#include <stdio.h> 

Object::Method() 
{ 
    printf("why is the next line not printing? This one prints fine\n"); 

    std::cout << "This line should print second, but doesnt" << std::endl; 

    printf("but this line prints fine like the first!\n"); 
} 

輸出爲:

爲什麼下一行不打印?這一個打印罰款

但這行打印好像第一!

我似乎無法弄清楚爲什麼它不會打印。 std::flush也沒有效果。

+5

這段代碼沒有錯。請嘗試拿出一個SSCCE(http://sscce.org/)。 – NPE 2013-03-23 15:57:26

+4

可能'std :: cout'已被重定向到控制檯輸出以外的另一個輸出源。我無法真正想到任何其他解釋。 – 2013-03-23 15:57:28

+0

您正在使用哪個IDE?因爲我認爲它與此有關... – Saqlain 2013-03-23 16:34:56

回答

1

您應該使用std::cout(C++流)或C風格的流。混合它們可能會產生未定義的行爲。

例如,他們可能有單獨的「緩衝」通道。

+0

只留下printf讓我所有的代碼都可以工作。但我認爲他們的目的是一起工作?所以我現在就接受它。 – Wildcat313 2013-03-23 18:18:17

0

代碼片段在我的系統上運行良好,您的問題可能來自您的代碼的其他部分,這裏沒有列出。 在方法開始處嘗試fflush(stdout)並查看它是否有效。

+0

不幸的是它沒有效果:/ – Wildcat313 2013-03-23 18:22:02