【问题标题】:Error C2660: function does not take 2 arguments C++错误 C2660:函数不接受 2 个参数 C++
【发布时间】:2017-04-28 02:03:35
【问题描述】:

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

【问题讨论】:

    标签: c++ function primes


    【解决方案1】:

    你的函数声明

    vector<int> calc_primes(const int max);
    

    与你的函数不匹配定义

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

    这些应该相同才能达到预期的结果。

    【讨论】:

    • 看你的回答对你的回答很有帮助我想到我没有声明重载函数,声明后它可以工作。
    • @shoaibsabir 太棒了!
    【解决方案2】:

    可能是这个原因。 这里:

    vector<int> calc_primes(const int max);
    

    你用一个参数声明它

    在这里你用 2 个参数声明它:

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

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2013-11-18
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多