我在這裏越來越arduino錯誤。Arduino錯誤。可以; t找到它
struct student{
int num,ac,sp,co, sum = 0;
if (ac>=30 && sp>=30 && co>=30)
^
sum = ac+sp+co;
};
我現在只是學習我的C++方式,所以它可能是一個愚蠢的錯誤..在此先感謝。 確切的錯誤:行:12西:6 [錯誤]預期前 '如果'
我在這裏越來越arduino錯誤。Arduino錯誤。可以; t找到它
struct student{
int num,ac,sp,co, sum = 0;
if (ac>=30 && sp>=30 && co>=30)
^
sum = ac+sp+co;
};
我現在只是學習我的C++方式,所以它可能是一個愚蠢的錯誤..在此先感謝。 確切的錯誤:行:12西:6 [錯誤]預期前 '如果'
1) Here可以讀取關於什麼在C++結構不合格-ID。簡而言之,它是其他數據類型的集合。所有的邏輯(你的if語句)應該放在函數中。
2) 看起來你想要做這樣的事情:
int num = 0, ac = 0, sp = 0, co = 0, sum = 0;
相反,你只初始化sum
。其他變量採用默認值。
我不知道我compeltely理解你想要達到的,而是試圖尋找這個代碼(這不是Arduino的,但你應該明白我的意思):
struct Student {
int num, ac, sp, co;
int getSum() {
if (ac >= 30 && sp >= 30 && co >= 30)
return = ac + sp + co;
else return 0;
}
};
int main() {
Student student;
student.ac = 30;
student.cp = 40;
student.co = 50;
cout << student.getSum();
return 0;
}
現在,這是問題。 在致電student.getSum()
之前,您必須始終初始化ac
,sp
和co
(正如我在main()
中所做的那樣),否則您可能會得到垃圾結果。爲了避免這種情況,給每個變量賦予默認值(構造函數是一個很好的地方)。
這是更大代碼的一部分。它們已經在外部初始化。 – hiteshn97
您可以在編輯中尋找一個小例子。 –
[在C++上找到一本很好的初學者書籍](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list),並從那裏開始。 –