簡體   English   中英

錯誤C2660:函數沒有2個參數C ++

[英]Error C2660: function does not take 2 arguments C++

好的,所以我是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;
}

這是函數聲明。 您是否認為我在輸入“&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;
            }
        }
    }
}

可能這就是原因。 這里:

vector<int> calc_primes(const int max);

你用一個參數聲明

在這里,您使用2個參數聲明它:

void calc_primes(unsigned long long int max, vector<unsigned long long int> &primes)

您的函數聲明

vector<int> calc_primes(const int max);

與您的函數定義不匹配:

void calc_primes(unsigned long long int max, vector<unsigned long long int> &primes)

這些應該相同以獲得期望的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM