永发信息网

C++调试出错

答案:3  悬赏:10  手机版
解决时间 2021-03-23 21:18
  • 提问者网友:像風在裏
  • 2021-03-22 23:50
#include <iostream>
class box
{
private:
int life;
public:
box (int a);
box ();
void say();
};
void box (int a)
{
int life=a;
}
void box::say()
{
std::cout<<"BOX的生命剩余"<<life<<std::endl;
}
void main()
{
box box1 (10);
box1::say;
system("pause");
}
y一段小小的程序,调试的时候有以下错误提示:
1>d:\program test\class test2\class test2\class test2.cpp(25) : error C2146: syntax error : missing ';' before identifier 'box1'
1>d:\program test\class test2\class test2\class test2.cpp(25) : warning C4551: function call missing argument list
1>d:\program test\class test2\class test2\class test2.cpp(25) : error C3861: 'box1': identifier not found

本人新手,请高手指教,不胜感激。
最佳答案
  • 五星知识达人网友:想偏头吻你
  • 2021-03-23 01:24
你好:
1、错误1: 重载带参数的构造函数在函数定义时编写错误
// box (int a); 方法在类外定义可如下:
box::box(int a)
{
life = a;
}
2、 错误2: Say()方法 不是类静态成员方法,不可box1::say() 调用;
//box1::say() 改成: box1.say();
[注] 如果要使用box1::say() 方法调用say()函数,可以在类中声明say()方法为静态类型, 如下:
// 在类中:
static void say(box &stTemp1);
// 在类外照常实现其方法;
void box::say()
{
std::cout<<"BOX的生命剩余"<<stTemp1.life<<std::endl;
}
// 调用时,即可使用box::say(box1);
全部回答
  • 1楼网友:春色三分
  • 2021-03-23 04:21
1.声明构造器的时候,不应该把返回类型设置为void,直接去掉void就行了。 2.另外,你声明了两个构造器,没有声明析构器。
  • 2楼网友:时间的尘埃
  • 2021-03-23 02:57
cout<<"x+y="<<x+y<<endl; cout<<"x-y="<<x-y<<endl; cout<<"x*y="<<x*y<<endl; cout<<"x/y="<<x/y<<endl; 改为 cout<<"x+y="<<(x+y).get()<<endl; cout<<"x-y="<<(x-y).get()<<endl; cout<<"x*y="<<(x*y).get()<<endl; cout<<"x/y="<<(x/y).get()<<endl;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯