[問題] 關於class簡單程式的問題

看板C_and_CPP (C/C++)作者 (sdimkk)時間15年前 (2010/09/11 00:42), 編輯推噓1(1019)
留言20則, 5人參與, 最新討論串1/1
我自己寫了一小段程式,用的是visual c++ 6.0免費版 #include "stdafx.h" #include <iostream> using namespace std; class Score{ public: set_initial(); int totalscore(); int number; char name[10]; int chinese,english,math,total; }; Score::set_initial(){number=chinese=english=math=total=0;} int Score::totalscore(){ total=chinese+english+math; return total; } int main(int argc, char* argv[]) { Score student[]={{1,"John",60,70,80,0},{2,"Mary",70,70,60,0}, {3,"Sherry",60,60,90,0}}; for(int index=0;index<3;index++) cout<<"Total score of student "<<student[index].name<<"is" <<student[index].totalscore<<endl; system("PAUSE"); return 0; } 其實就是三個學生student[]={{1,"John",60,70,80,0},{2,"Mary",70,70,60,0}, {3,"Sherry",60,60,90,0}};都宣告成class student 第一個數字表示學生編號,第二個數字是學生名字,後面試三個成績和要 存三成績總和的地方 我的問題是: 1.此宣告方法好像不能把 int number; char name[10]; int chinese,english,math,total; 這三個設成private, 因為main沒辦法直接對private做設定, 如果我想要維持 這種三個學生的宣告方式, 又想加入private, 改怎麼進行呢? 2.就算我把所有class內容全部設定成public後(這根本就是struct一樣了) 有個超奇怪的error atal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information Error executing cl.exe. student.exe - 1 error(s), 0 warning(s) 啥都沒講究要打發我, 難道是因為我是免費版嗎= = 請問一下各位高手該如何解決呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.120.134.40

09/11 00:49, , 1F
Constructor
09/11 00:49, 1F

09/11 01:05, , 2F
哦 樓上不就是解決各路問題的高手嗎 上次多謝啦
09/11 01:05, 2F

09/11 01:13, , 3F
還有為什麼你要用12年前的compiler...
09/11 01:13, 3F

09/11 01:14, , 4F
VC 2010 express 也是可以免費下載的
09/11 01:14, 4F

09/11 01:15, , 5F
想要做initial可以寫在constructure
09/11 01:15, 5F

09/11 01:16, , 6F
你的initial也沒有型態@@?
09/11 01:16, 6F

09/11 01:16, , 7F
因為以前的書有光碟就直接灌了 哈~
09/11 01:16, 7F

09/11 01:16, , 8F
(  ̄ c ̄)y▂ξ 不要相信免費的東西會有多好
09/11 01:16, 8F

09/11 01:17, , 9F
存取private member可以用public member fun來存取
09/11 01:17, 9F

09/11 01:17, , 10F
因為本來那三個存的東西是放在private裡面的@@
09/11 01:17, 10F

09/11 01:18, , 11F
善用OOP的觀念應該就了解了~~
09/11 01:18, 11F

09/11 01:21, , 12F
把constructor 弄掉之後 就變成有先宣告int number那堆了
09/11 01:21, 12F

09/11 01:21, , 13F
但還是fatal error 哈~
09/11 01:21, 13F

09/11 01:25, , 14F
我改完可以跑不知道你怎麼改?你可以在constructor初始
09/11 01:25, 14F

09/11 01:30, , 15F
可以麻煩樓上寄code讓我學習一下嗎= = 感激不盡
09/11 01:30, 15F

09/11 01:44, , 16F
constructor不能有回傳型態吧...對吧?
09/11 01:44, 16F

09/11 01:45, , 17F
看錯 沒事 別理我
09/11 01:45, 17F

09/11 01:51, , 18F
set_initial()已經改成Score();了
09/11 01:51, 18F

09/11 01:52, , 19F
而且後面student[index].totalscore()漏掉() = =
09/11 01:52, 19F

09/11 02:10, , 20F
書上的建構子章節還需要再回去看看... ppt.cc/5e7H
09/11 02:10, 20F
文章代碼(AID): #1CYb_gJX (C_and_CPP)
文章代碼(AID): #1CYb_gJX (C_and_CPP)