c++ - 为什么类的函数没有在主函数中被调用?
问题描述
我想创建一个计算整数阶乘的类,我的代码搞砸了,我需要一些方向......你能帮我找到问题吗?
#include <iostream>
class fact {
private:
int a;
public:
fact(){};
fact(int a){this->a=a;}
void setfact(int a){this->a=a;}
int getfact(){return a;}
int Fact(){
int i;
if (a>0){
for(i=2;i<=a;i++){
return a=a*i;
}
}
else if (a=0)
return 1;
else
return 0;
}
};
using namespace std;
int main()
{
fact b;
int j;
cout << "entrer un nombre pour calculer sa factorielle" << endl;
cin >> j;
Fact b(j);
cout << "la factorielle de" << j << "est:" << b.Fact(j);
return 0;
}
解决方案
感谢您的帮助,现在我的代码完美运行......不管变量和声明的汤哈哈。
#include <iostream>
using namespace std;
class fact{
private:
int a;
public:
fact(){};
fact(int a){this->a=a;}
void setfact(int a){this->a=a;}
int getfact(){return a;}
int Fact(int a){
int i;
int res=1;
if (a>0){
for(i=2;i<=a;i++){
res=res*i;
}
return res;
}
else if (a==0)
return 1;
else
return 0;
}
};
int main()
{
int j;
std::cout << "entrer un nombre pour calculer sa factorielle" << endl;
std::cin >> j;
fact b(j);
cout << "la factorielle de " << j << " est: " << b.Fact(j);
return 0;
}
推荐阅读
- r - 无法使用动物园创建 ts
- node.js - 第二次摩卡测试没有运行
- javascript - onBlur 下拉问题
- java - RSocket 在 500 条消息后未发送
- html - 当您使用
标签作为行距时,该空间中有多少像素(高度)? - php - 如何加快查询执行 MySQL
- postman - 导入 Github RAML 文件夹失败并显示“导入的文件夹包含 RAML 0.8 规范的多个根目录”。
- java - 如何在没有“ids”引用的情况下获得 json 响应
- java - 如何进行内部方法调用检查
- python - 在 Win10 上安装 Kivy 应用似乎没有从 Virtualenv 获取 python