下面的C++代碼提供了一個錯誤,而編譯:爲什麼不使用class-keyword不能創建對象?
#include<iostream>
using namespace std;
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{ hours = h; minutes = m; }
void puttime(void)
{
cout << hours << " hours and ";
cout << minutes << " minutes " << "\n";
}
void sum(time, time);
};
void time::sum(time t1, time t2)
{
minutes = t1.minutes + t2.minutes;
hours = minutes/60;
minutes = minutes % 60;
hours = hours + t1.hours + t2.hours;
}
int main()
{
time T1, T2, T3; // LINE NUMBER 32.
T1.gettime(2, 45);
T2.gettime(3, 30);
T3.sum(T2, T2);
cout << "T1 = "; T1.puttime();
cout << "T2 = "; T2.puttime();
cout << "T3 = "; T3.puttime();
return 0;
}
以下錯誤,我越來越:
habeebperwad:~/study/cpp/eb$ g++ 5.7-objects-as-arguments.cpp
5.7-objects-as-arguments.cpp: In function ‘int main()’:
5.7-objects-as-arguments.cpp:32:7: error: expected ‘;’ before ‘T1’
5.7-objects-as-arguments.cpp:34:2: error: ‘T1’ was not declared in this scope
5.7-objects-as-arguments.cpp:35:2: error: ‘T2’ was not declared in this scope
5.7-objects-as-arguments.cpp:37:2: error: ‘T3’ was not declared in this scope
habeebperwad:~/study/cpp/eb$
如果我的聲明前添加類的時間T1,T2,T3 ;',它工作正常。
爲什麼它不工作沒有類?
您使用哪種編譯器? – Zeta 2012-03-25 14:22:35
嘗試不使用名稱空間標準;(並明確限定所有內容)。 – 2012-03-25 14:23:35
這可能與'std :: time'的衝突有關。 – Mankarse 2012-03-25 14:24:33