我嘗試呼叫一個函式,secim()因為我想縮短這個函式,但是它給出了一個 c3861 錯誤。我嘗試了很多東西,但每次都會給出不同的錯誤。我認為最好不拆分共享功能,因為我不知道哪種方式是正確的。我是編程新手,我認為這是一個簡單的問題,但我無法解決。
#include <iostream>
using namespace std;
int fact(int n) { // function to calculate factorial of a number
if (n <= 1)
return 1;
return n * fact(n - 1);
}
int npr(int n, int r) { // finding permutation
int pnr = fact(n) / fact(n - r);
return pnr;
}
int combin(int n, int r)
{
int f1, f2, f3, y;
f1 = fact(n);
f2 = fact(r);
f3 = fact(n - r);
y = f1 / (f2 * f3);
return y;
}
int secimm2(int s, int n, int r)
{
int ss;
cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
cin >> ss;
if (ss == 1) {
return secim();
}
else {
return 0;
}
}
int secim()
{
int s, n, r;
cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
cin >> s;
if (s == 1) {
cout << "1 adet sayi girin\ " << endl;
cin >> n;
cout << fact(n) << endl;
return secimm2(s,n,r);
}
else if (s == 2) {
cout << "2 adet sayi girin\n ";
cin >> n;
cin >> r;
cout << npr(n, r) << endl;
return secimm2(s, n, r);
}
else if (s == 3) {
cout << "2 adet sayi girin\n ";
cin >> n;
cin >> r;
cout << combin(n, r) << endl;
return secimm2(s, n, r);
}
else {
cout << "hatali giris tekrar dene\n" << endl;
return secimm2(s, n, r);
}
}
int main()
{
int s;
cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
cin >> s;
cout << secim();
}
uj5u.com熱心網友回復:
錯誤 C3861是“找不到識別符號”。問題是在宣告之前secimm2()嘗試呼叫secim(),所以編譯器不知道是什么secim()。如果你移動secim()上面的實作,你會得到一個關于在宣告之前嘗試呼叫secimm2()的類似錯誤。secim()secimm2()
您需要使用前向宣告來解決此問題,例如:
int secim(); // <-- add this
int secimm2(int s, int n, int r)
{
...
return secim();
...
}
int secim()
{
...
}
uj5u.com熱心網友回復:
主要問題(在您編輯問題之后)是識別符號secim在第一次遇到時尚未宣告(在函式中secimm2)。解決此類問題的常規方法是宣告函式原型。
但是,這里不合適。您正在使用遞回來控制實際上是基本回圈構造的內容。更改您的“繼續”功能,如下所示:
int secimm2()
{
int ss;
cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
return (cin >> ss && ss == 1) ? 1 : 0;
}
除非成功讀取值 1,否則將回傳 0。
然后在你的 main 中,你可以回圈直到secim回傳零:
int main()
{
while (secim() != 0);
}
uj5u.com熱心網友回復:
編譯器從上到下讀取你的代碼,如果它看到一個未定義的函式會報錯。
例如:
void foo() {
bar(); // Error: What is bar()? Not declared yet.
}
void bar() {
foo(); // OK: foo was declared and defined above.
}
解決方案是在呼叫之前宣告導致問題的函式:
void bar(); // Tells that a method void bar() exists. But doesn't define it yet.
void foo() {
bar(); // OK: we know what bar() is: a call to the void bar() function.
}
void bar() { // Bar is now defined. Signature must follow declaration.
foo(); // OK: foo was declared and defined above.
}
使用您的代碼,它提供了以下修復:
#include <iostream>
using namespace std;
int secim(); // Declares the secim function, but doesn't define it.
int fact(int n) { // function to calculate factorial of a number
if (n <= 1)
return 1;
return n * fact(n - 1);
}
int npr(int n, int r) { // finding permutation
int pnr = fact(n) / fact(n - r);
return pnr;
}
int combin(int n, int r)
{
int f1, f2, f3, y;
f1 = fact(n);
f2 = fact(r);
f3 = fact(n - r);
y = f1 / (f2 * f3);
return y;
}
int secimm2(int s, int n, int r)
{
int ss;
cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
cin >> ss;
if (ss == 1) {
return secim();
}
else {
return 0;
}
}
int secim()
{
int s, n, r;
cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
cin >> s;
if (s == 1) {
cout << "1 adet sayi girin\ " << endl;
cin >> n;
cout << fact(n) << endl;
return secimm2(s,n,r);
}
else if (s == 2) {
cout << "2 adet sayi girin\n ";
cin >> n;
cin >> r;
cout << npr(n, r) << endl;
return secimm2(s, n, r);
}
else if (s == 3) {
cout << "2 adet sayi girin\n ";
cin >> n;
cin >> r;
cout << combin(n, r) << endl;
return secimm2(s, n, r);
}
else {
cout << "hatali giris tekrar dene\n" << endl;
return secimm2(s, n, r);
}
}
int main()
{
int s;
cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
cin >> s;
cout << secim();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/523355.html
標籤:C 功能控制台应用程序
上一篇:std::decay和std::remove_cvref之間的區別?
下一篇:將資料從一個表拆分到另一個表
