2011-05-19 47 views
1

任何人都可以解釋爲什麼代碼塊是給我的這些錯誤?:瘋狂的基本的C++錯誤

error: ISO C++ forbids declaration of 'cout' with no type 
error: invalid use of '::' 
error: expected ';' before '<<' token 
error: '<<x>>' cannot appear in a constant-expression  // <<x>> is many different variable names 

我的代碼是從字面上簡單:

#include <iostream> 
#include "myclass.h" 

int main(){ 
    std::string data; 
    std::string e; 

    e = myclass().run(data); 
    std::cout << e << std::endl; 

    return 0; 
} 

在世界上是怎麼回事?

編輯:是的,我有iostream的。抱歉,沒有把它還有更早

+2

在處理編譯錯誤時,最好從頭開始逐個解決。在很多情況下,一個錯誤會使解析器混淆,一些後來診斷的錯誤並非如此(我指的是那裏的'<>'部分,可能指的是或不是真正的錯誤)。 – 2011-05-19 07:46:22

+0

@calccrypto - 關於編輯 - 然後告訴我們什麼'myclass()。run(data)'做 – 2011-05-19 07:48:35

+0

myclass()。run(data)返回一個字符串 – calccrypto 2011-05-19 07:48:59

回答

11

添加

#include <iostream> 

std::cout是這個水箱內


編輯:關於你的編輯 - 這意味着,這個問題是肯定的內myclass.h或有一些代碼,這裏沒有顯示。

2

你應該包括<iostream>

2

是否包含<iostream>地方?知道你已經添加後

編輯<iostream>

那麼你可以檢查:

  • #include <string>
  • 如果你的類定義是由分號結束

如果一切正常我想檢查你的myclass.h :-(

3

#include <string>怎麼樣?

沒有它(以下代碼)

#include <iostream> 

int main(){ 
    std::string data; 
    std::string e; 

    std::cout << e << std::endl; 

    return 0; 
} 

我的G ++報道:

tst.cpp: In function `int main()': 
tst.cpp:4: undeclared variable `string' (first use here) 
tst.cpp:4: parse error before `;' 
tst.cpp:5: parse error before `;' 
tst.cpp:7: `e' undeclared (first use this function) 
tst.cpp:7: (Each undeclared identifier is reported only once 
tst.cpp:7: for each function it appears in.) 
1

您發佈(與編輯)代碼是正確的。在myclass.h必須有 有趣的事情發生。 (也許

#define std 

,使編譯器看到::cout。)

你可能想看看預處理器輸出:Unix上,/E下視覺工作室 編譯器選項-E。它 將是龐大的,但所有你感興趣的是最後10 線左右;預處理器對您的代碼做了什麼。