2013-05-27 41 views
1

我尋找了解決方案,我的問題,並張貼的似乎並沒有工作。我試圖在Visual Studio 2012中運行以下代碼。我以前使用Eclipse進行編程,並且正在調整到新的IDE。新編譯器 - 使用Cout?

class 
    IntSLLNode{ 
    public: 
    int info; 
    IntSLLNode *next; 

    IntSLLNode(){ 
    next = 0; 
    } 

    IntSLLNode (int el, IntSLLNode *ptr= 0) { 
    info = el; 
    next = ptr; 
    } 

}; 

int main(){ 

#include <iostream> 

using namespace std; 

IntSLLNode *p = new IntSLLNode(18); 
cout << *p; 

return; 

} 

當我嘗試運行,它給了我下COUT錯誤。我通常包含iostream和std命名空間。這是不正確的?任何人都可以幫助我實現這個目標,因爲我非常喜歡Visual Studio IDE的外觀,並且希望繼續使用它。

+0

如果你編輯你的問題並添加你從IDE獲得的錯誤信息,那麼會更好。 – p91paul

+1

還要注意「使用namespace std'的字符數多於'std ::',所以在這種情況下它甚至不會保存你的任何輸入。 – juanchopanza

+1

很確定這不會在eclipse中編譯。 –

回答

4

的這裏的問題是,線

#include <iostream> 
using namespace std; 

不應該是main內。相反,他們應該在計劃的最高層。你的程序應該看起來更像

#include <iostream> 
using namespace std; 

/* Other definitions */ 

int main() { 
    IntSLLNode *p = new IntSLLNode(18); 
    cout << *p; 
} 

此外,您必須聲明

return; 

返回一個int的功能,這是不允許的內部。試試這個變化要麼

return 0; 

或者,因爲這是main,只跳過return一起。

希望這有助於!

+0

此外,對於IntSLLNode對象,沒有重載的'operator <<',所以'cout << * p;'仍然是一個錯誤。也許'cout << p;'? – bcrist

+0

謝謝!不是回報;相當於返回0 ;,但? 我想輸出指針p的值,所以我解除了引用。不是嗎? –

+5

@ user2395694'return;'不等於'return 0;'。 – juanchopanza

相關問題