PCZONE 討論區

PCZONE 討論區 (https://www.pczone.com.tw/vbb3/)
-   -- 其 他 軟 體 討 論 版 (https://www.pczone.com.tw/vbb3/forum/8/)
-   -   【問題】我的C++程式為何不能用COUT指令 (https://www.pczone.com.tw/vbb3/thread/8/123552/)

barlowts 2006-05-03 05:39 PM

【問題】我的C++程式為何不能用COUT指令
 
-------------------------------------------------------------------------
C:\Program Files\Microsoft Visual Studio\MyProjects\proj_01\main_proj01.c(9) : error C2065: 'cout' : undeclared identifier
-------------------------------------------------------------------------
一用了就出現這樣的錯誤訊息,真的不知道要從何下手去修正他。

HarrisonLin 2006-05-03 05:42 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
程式碼咧?

不過不用程式碼也可以猜得到 : 沒有 include iostream,或是 include 了沒有宣告你所使用的 namespace 為 std...

barlowts 2006-05-03 05:57 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
#include <iostream.h>

void main()
{
int r=5;
const float pi=3.1415926f;
float area;
area = pi*r*r;
cout << "r = " << r << " , circle area = " << area << "\n" ;
}
--------------------------------------------------------------------------
剛開始學C++,上面是照著書本的範例打的,但是卻出現這樣的錯誤訊息:
C:\Program Files\Microsoft Visual Studio\MyProjects\proj_01\main_proj01.c(10) : error C2065: 'cout' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\proj_01\main_proj01.c(10) : error C2297: '<<' : illegal, right operand has type 'char [5]'
Error executing cl.exe.

proj_01.exe - 2 error(s), 0 warning(s)
--------------------------------------------------------------------------

琥珀 2006-05-03 06:13 PM

.h 去掉。

lenbo 2006-05-03 06:16 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
[CODE]
#include <iostream>
using namespace std;
int main(void)
{
int r=5;
const float pi=3.1415926f;
float area;
area = pi*r*r;
cout << "r = " << r << " , circle area = " << area << "\n" ;
return 0;
}
[/CODE]

HarrisonLin 2006-05-03 06:18 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
學 C++, 可是怎麼附檔名是 .c ? iostream 可不是隨著 C 的編譯器所附上的函式庫哦!把附檔名換成 cpp 看看!

barlowts 2006-05-03 06:22 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
#include <iostream>
using namespace std;

int main(void)
{
int r=5;
const float pi=3.1415926f;
float area;
area = pi*r*r;
cout << "r = " << r << " , circle area = " << area << "\n" ;
return 0;
}
--------------------------------------------------------------------------
照著上面的打,結果出現:
c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error : "eh.h is only for C++!"
Error executing cl.exe.

main_proj01.exe - 1 error(s), 0 warning(s)

barlowts 2006-05-03 06:36 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
順便請問一下 namespace是什麼功用??
還有啊 #include<iostream.h>
#include<iostream>
這兩個有什麼不一樣呢 ??
"iostream 可不是隨著 C 的編譯器所附上的函式庫哦!" <=這是什麼意思呢 ??

jasonyu007 2006-05-03 07:19 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
main_proj01.c 改成 main_proj01.cpp 試試吧 !!

acer1832a 2006-06-03 08:37 PM

回覆: 【問題】我的C++程式為何不能用COUT指令
 
[QUOTE=barlowts]順便請問一下 namespace是什麼功用??
還有啊 #include<iostream.h>
#include<iostream>
這兩個有什麼不一樣呢 ??
"iostream 可不是隨著 C 的編譯器所附上的函式庫哦!" <=這是什麼意思呢 ??[/QUOTE]

#include<iostream.h>跟#include<iostream>基本上是一樣的
只是,iostream這個函式庫是標準函式庫中的一個,所以,基本上,打iostream就可以了
如果不能用,可能要更新你的編譯器

所謂的namespace,我忘了中文是什麼,但我看的那一本書的說明還蠻好玩的
namespace可看成是一個家族的名稱,不同的家族中可能會有相同名字的人
namespace就是用來區分的
也就是說,是指定接下來的程式中,是使用某個namespace中的函式
(很久沒有去翻了,不知道有沒有錯)


所有時間均為 +8。現在的時間是 09:42 PM



 XML   RSS 2.0   RSS 
本站使用 vBulletin 合法版權程式
站務信箱 : [email protected]

本論壇所有文章僅代表留言者個人意見,並不代表本站之立場,討論區以「即時留言」方式運作,故無法完全監察所有即時留言,若您發現文章可能有異議,請 email :[email protected] 處理。