好吧所以我是C ++编码的初学者,当我偶然发现这个错误时,我正在制作这个素数查找程序。这可能不是最好的,我愿意接受反馈。以下代码的顺序正确且连续。
#include "stdafx.h"
using namespace std;
//finds prime numbers using Sieve of Eratosthenes algorithm
vector<int> calc_primes(const int max);
int main()
{
unsigned long long int minValue;
unsigned long long int maxValue;
bool Loop = true;
char chContinue;
vector<unsigned long long int> primes;
std::string Path;
Path = "D:\\Work\\Documents\\prime.txt";
// TODO: code your application's behavior here.
while (Loop == true)
{
cout << "Enter minimum prime number checking range (__________)" << endl ;
cin >> minValue;
cout << "Enter maximum prime number checking range (__________)" << endl ;
cin >> maxValue;
if (maxValue <= minValue)
{
cout << "Invalid selection" << endl <<endl <<endl <<endl ;
continue;
}
所以这就是它给我一个错误的地方
calc_primes(maxValue,primes);
它告诉我该函数不会带两个参数。但是,声明明确指出它需要一个unsigned long long int和一个unsigned long long int的向量,所以我不太确定。
//opens file path
std::ofstream of;
of.open(Path);
//writes to file and displays numbers
for(unsigned long long int i = 0; i < primes.size(); i++)
{
if(primes.at(i) != 0)
{
cout << primes.at(i) <<" ";
of << primes.at(i) << " ";
}
}
cout << endl <<endl <<endl <<endl ;
of.close();
cout << "Continue? (y/n)" << endl ;
cin >> chContinue;
if (chContinue == 'y') {
continue;
}
else
{
if (chContinue == 'n')
{
break;
}
else
{
cout << "Invalid Selection" << endl << endl ;
}
}
}
return 0;
}
这是函数声明。你认为我通过推出&amp; primes犯了错误吗?
void calc_primes(unsigned long long int max, vector<unsigned long long int> &primes)
{
// fill vector with candidates
for(unsigned long long int i = 2; i < max; i++)
{
primes.push_back(i);
}
// for each value in the vector...
for(unsigned long long int i = 0; i < primes.size(); i++)
{
//get the value
unsigned long long int v = primes[i];
if (v != 0)
{
//remove all multiples of the value
unsigned long long int x = i + v;
while(x < primes.size())
{
primes[x] = 0;
x = x + v;
}
}
}
}
答案 0 :(得分:2)
可能是这个原因。 这里:
vector<int> calc_primes(const int max);
你用一个参数声明它
在这里你用2个参数声明它:
void calc_primes(unsigned long long int max, vector<unsigned long long int> &primes)
答案 1 :(得分:2)
您的函数声明:
vector<int> calc_primes(const int max);
与您的函数定义:
不匹配void calc_primes(unsigned long long int max, vector<unsigned long long int> &primes)
这些应该是相同的,以达到预期的效果。