C++ 对象作为函数参数
我写了一个简单的程序 我得到这个错误:C++ 对象作为函数参数,c++,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
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;
}
#包括
使用名称空间std;
上课时间
{
整小时;
int-min;
公众:
void gettime(inth,intm)
{
小时=小时;
min=m;
}
void puttime()
{
请记住,有一个名为的标准函数
这是您应该避免使用命名空间std;
的一个主要原因。请记住,有一个名为的标准函数
这是您应该避免使用命名空间std;的一个主要原因。这里的putime()必须是b.puttime()。否则编译的代码必须是b.puttime()。否则,使用命名空间std;编译的代码可能会引入一个名为
time()的函数
从标准库中。找到一个新名称。Time
浮现在脑海中。将Time
改为自始至终说Time
,并修复打字错误b.putime();
->b.puttime()使用nampespace std
的另一种方法是使用std::cout
和使用std::endl
。也就是说,只将您实际使用的std中的内容纳入范围。关于名称时间
,我建议两件事:1)将类时间
放在命名空间中。2)不要使用ode>使用namespace std
指令。使用namespace std;
可能会从标准库中引入一个名为time()
的函数。找到一个新名称。time
浮现在脑海中。将time
改为自始至终说time
,并修复打字错误b.puttime();
->b.puttime()使用nampespace std
的另一种方法是使用std::cout
和使用std::endl
。也就是说,只将您实际使用的std中的内容纳入范围。关于名称时间
,我建议两件事:1)将类时间
放在命名空间中。2)不要使用ode>使用命名空间std
指令。