C++我的程序这个问题 应该怎么改 高手请进
来源:百度知道 编辑:UC知道 时间:2024/05/05 14:52:31
#include <iostream>
using namespace std;
class Time
{ public:
Time(int,int,int);
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{ hour=h;
minute=m;
sec=s;
}
void fun(const Time &t)
{t.hour=18;}
int main()
{Time t1(10,13,56);
fun(t1);
cout<<t1.hour<<endl;
return 0;
}
编译的时候说我这{t.hour=18;} 报错 报错信息为error C2166: l-value specifies const object 高手告诉我下怎么改
using namespace std;
class Time
{ public:
Time(int,int,int);
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{ hour=h;
minute=m;
sec=s;
}
void fun(const Time &t)
{t.hour=18;}
int main()
{Time t1(10,13,56);
fun(t1);
cout<<t1.hour<<endl;
return 0;
}
编译的时候说我这{t.hour=18;} 报错 报错信息为error C2166: l-value specifies const object 高手告诉我下怎么改
因为hour是私有变量,不能直接引用,所以把fun改成成员函数即可。
#include <iostream>
using namespace std;
class Time
{ public:
Time(int,int,int);
int hour;
int minute;
int sec;
public:void fun(int);
};
Time::Time(int h,int m,int s)
{ hour=h;
minute=m;
sec=s;
}
void Time::fun(int n){
hour=n;
}
int main()
{Time t1(10,13,56);
t1.fun(18);
cout<<t1.hour<<endl;
return 0;
}
左值不能使const。
把
void fun(const Time &t)
{t.hour=18;}
改成
void fun(Time &t)
{t.hour=18;}
即可!
void fun(const Time &t)
{t.hour=18;
}
参数有 const 修改参数的值当然会报错
如果函数里要修改参数值去掉 const就行了
void fun(Time &t)
{t.hour=18;
}
#include <iostream>
using namespace std;
class Time
{ public:
Time(int,int,int);
int hour;
int minute;
int sec;
};
Time::
C语言中用while语句写这个问题的程序。
我的c程序
我的电脑打开每一个程序都会提示选择找开方式打开,请问这个问题应该怎样才能解决?
我的电脑魔兽争霸澄海3C的可以玩,但有点卡,应该怎样做才能解决这个问题呢?
怎么样用c程序编写这个问题
我正在学习C语言,用C语言编程序应该用什么样的软件?
我自学c语言看谭浩强的C程序设计应该配合哪种程序平台?
我的电脑C盘有200M的空间,可是每当运行程序的时候C盘中就只剩下4M的空间,怎样才能解决这个问题?
我的电脑启动时出现“c:\windows\system32\supdate2.dll,找不到模块”这个问题应该如何解决?
这个问题用ASP程序应该怎么写?