Re: [問題]螞蟻書範利一問 C程式設計藝術第五版
※ 引述《ctjh (forever)》之銘言:
: source code如下:
: #include "stdafx.h"
: #include <stdio.h>
: #include <stdlib.h>
: enum months {
: Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
: int _tmain(int argc, _TCHAR* argv[])
: {
: enum months month;
: const char *monthName[]={" ", "January", "February", "March", "April", "May", "June",
: "July", "August", "September", "October", "November", "December" };
: for ( month = Jan; month<=Dec; month++)
: {
: printf("%2d%15s\n", month, monthName[month]);
: }
: return 0;
: }
: 我翻了兩版的螞蟻書,都寫得一樣。可是...我實際操做出現error...
: 1>i:\project\enum_example_1\enum_example_1.cpp(19) : error C2676:
↑
副檔名請用 .c, 否則 VC會以 C++的語法規則去編譯, 在 C語言中
, 一個以列舉型態定義的變數, 是被當 int來用的. 但 C++不然;
因為列舉被視為自訂型態, 大小關係可以確定, 但是其他運算還需
要額外定義, 定義如下:
#include <stdint.h>
#include <cassert>
months& operator++( months &month )
{
// 注意! 編譯器實作months的整數型態, 因為要裝得
// 下你給的所有列舉值, 所以有可能過大! 使得轉為
// int 會造成資料遺失
assert( sizeof(months) == sizeof(int) );
int next = month + 1;
month = (months)next; // +1之後的列舉值必須是
// 合法的
return month;
}
// 使用前加來實作
months operator++( months month, int )
{
months old_value = month;
++month;
return old_value;
}
: binary '++' : 'months' does not define this operator or
: a conversion to a type acceptable to the predefined operatormonth++
: 問題出在month++這個用法不行...
: 所以是書打錯了嗎?????
--
▂▅▂ ▁ ● ◣ 朴 ☆ 素 ★ 妍 ◢
◢ ◣ ◢▂▂◣ ◢▂※◣ ◢▄▂◣ T.T.L Listen 2
★ ★ ★ ★ ▉ ★ ★▏▉ ★ ★◣ http://ppt.cc/jIUk
◥ˇ◢ ▃◥ˇ◤▃ ◥ˇ◤ ◥ˇ◤◢ 說什麼結束
▃▃▇▃▃ ◢▇◣ ▋▎ ▋¥▎ ◢ http://ppt.cc/zQtB
▼ ▼ ▼ ▼ ψ髮箍 ◤ ◣
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.121.197.115
→
04/29 19:58, , 1F
04/29 19:58, 1F
重建試試? 我倒是沒這問題~ 囧
推
04/29 21:44, , 2F
04/29 21:44, 2F
→
04/29 21:46, , 3F
04/29 21:46, 3F
→
04/30 00:31, , 4F
04/30 00:31, 4F
發現程式碼打錯, 已修正~ 囧
※ 編輯: loveme00835 來自: 140.121.197.115 (04/30 00:47)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):