Trending News
使用dev_c++,但main不能設void
最近才剛開始學c++
使用的編譯器是dev-c++4.9.9.2
但我照書上打以下的程式,但卻出現一堆錯誤,
1 #include <iostream.h>
2 class Printer
3 {
4 private:
5 int Serial_Number;
6 public:
7 void Hello()
8 {
9 cout<<"HELLO\\n";
10 }
11 };
12
13 void main()
14 {
15 Printer My_Printer;
16 My_Printer.Hello();
17 }
錯誤內容:
1 C:\\Dev-Cpp\\include\\c++\\3.4.2\\backward\\iostream.h:31,
from D:\\C++\\hello.cpp
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
32:2 C:\\Dev-Cpp\\include\\c++\\3.4.2\\backward\\backward_warning.h
#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
16 D:\\C++\\hello.cpp `main' must return `int'
但之後問了同學…
1.把void main改成 int main
2.cout前加上std ::
3.#include <iostream.h>把.h拿掉
把這三點改掉就可以執行,
請問這是為什麼呢?
以前這麼打都沒問題耶…
我明明照書上打,還是不行,
是跟使用編譯器有關嗎?還是dev還要再做其他設定…
或是…我該去買別本書呢?
((現在看的書是 文魁 visual c++ 入門進階 從c++、物件導向到視窗程式設計))
請問我打的這段程式有哪部份是錯的嗎?
請問那dev_c++我該怎麼設定呢?或是用哪個編譯軟體才可以使用呢?請推薦較適合初學者的編譯軟體吧…
剛上網查C++ Primer,英文不好,但繁體版的只到第三版,而拍賣上多是簡體版,看了簡體版的內容摘要,那翻譯的名詞跟過去學習的名詞有所差異,所以還以繁體為主,現在好像出到第五版了,那跟第三版有差嗎?
或是有比較推薦的其他書藉呢?
所以說int main才是正確的囉…過去學習印象都是不回傳就設void……看來還真該好好重新學習了
3 Answers
- noneLv 41 decade agoFavorite Answer
因為 dev-c++ 是使用標準的 C++ format
Standard C++ 的規格
1 Standard C++ Library(就是以前所說得 STL)
把 .h 移走
ex: .#include <iostream.h> 變成 .#include <iostream>
#include <stdio.h> 變成 #include <cstdio>
有 .h 是 pre-Standard C++ 或為了與 c 相容的寫法
2 int main() 或 int main(int argc, char* argv[]) 才是標準, 其他不是
3 cout 的 namespace 是 std, 不需要寫 std 的時候,都是 pre-Standard C++
結論: 如果你想學習標準的 C++, 請換一本書(建議是 C++ Primer 中文版), dev-C++ 是標準 C++, 不應該因此而放棄他
如果你只是想接觸 C++, 但是可以符合市面上大部分的書, 找找 VC 6.0, 他不是標準 C++, 但是很多書(但並不是全部),也不是標準的