object作为函数参数

时间:2013-01-29 10:16:06

标签: c++

我写了一个简单的程序。

我收到此错误:

time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope

这是我的代码:

#include<iostream>
using namespace std;
class time
{
    int hour;
    int min;

public:
    void gettime(int h,int m)
    {
        hour=h;
        min=m;
    }

    void puttime()
    {
        cout<<hour<<endl<<min;
    }

    void sum(time x,time y)
    {
        min=x.min+y.min;
        hour=min/60;
        min=min%60;
        hour=hour+x.hour+y.hour;
    }
};

int main()
{
    time a,b,c;
    a.gettime(2,45);
    b.gettime(3,35);
    c.sum(a,b);
    a.puttime();
    b.putime();
    c.puttime();
    return 0;
}

2 个答案:

答案 0 :(得分:5)

请记住,有一个名为time的标准函数。

这是你应该避免using namespace std;的一个主要原因。

答案 1 :(得分:1)

b.putime()必须是b.puttime()。否则编译此代码