【发布时间】:2014-01-09 20:43:52
【问题描述】:
在构造函数并调用它时,我的参数有些问题(这是我第一次尝试使用两个函数)。
这是调试器的错误:
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'bool isPrime(int*)':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|11|error: 'i' was not declared in this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|16|error: 'i' was not declared in this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'int main()':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|34|error: too few arguments to function 'bool isPrime(int*)'|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|9|note: declared here|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
这里是代码:
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
bool isPrime(int t[])
{
for(int j=2; j<=sqrt(t[i]); j++)
{
if(t[i]%j==0)
return false;
}
if(t[i]<2)
return false;
return true;
}
int main()
{
int t[50], n, i;
cout<<"Size of array: ";
cin>>n;
for (int i=1; i<=n; i++)
{
cout<<i<<". Element: ";
cin>>t[i];
}
i=1;
while(i<=n)
{
if(isPrime())
{
for(int j=n; j>=i+1; j--)
{
t[j+1]=t[j];
}
t[i+1]=99;
n++;
i=i+2;
}
else
{
i++;
}
}
for(int i=1; i<=n; i++)
{
cout<<t[i]<<", ";
}
cout<<"\n";
system("pause");
return 0;
}
我知道 system("pause") 很慢,不推荐使用等等。不要怪我使用它(建议使用其他方法)。
【问题讨论】:
-
如果这是 c++,您可能希望包含
和 。但这并不能解决您的问题。 -
这段代码有很多问题。您需要做的第一件事是阅读错误消息。从顶部开始并修复它(
i甚至不应该在那里,你可能不应该传递数组)。下一个告诉你调用isPrime时需要提供一个参数,否则它不知道你在测试什么。如果你对这一切感到困惑,不妨退后一步,从一本关于 C 的好书中阅读几章。 -
不,这是我想要包含的内容(cmath 与 math.h 的作用相同,我使用 cout 和 cin 操作数,使用 cstdlib 的编译器无法识别)。包含没有什么不好的。也许我是一个初学者,但不是这么多
-
cout 和 cin 在 iostream 标头中...不确定 cstdlib 会如何影响
-
对不起,我后来意识到这是另一个标题。但还是没有解决。
标签: c++ primes function-call