vendredi 2 février 2018

c++ error using std::accumulate creating Least Common Multiple class

I am trying to write a class the calculates the Least Common Multiplie:

LeastCommonMultiple.h:

#pragma once

#ifndef LEASTCOMMONMULTIPLE_H
#define LEASTCOMMONMULTIPLE_H

#include <vector>
#include <numeric>

class LeastCommonMultiple
{
public:
//LeastCommonMultiple() {};
LeastCommonMultiple(std::vector<int>&);

int GetLcmForVector();

private:
    std::vector<int> &m_list;

    int GetGreatestCommonDivisor(int, int);
    int GetLeastCommonMultiple(int, int);
};
#endif //! LEASTCOMMONMULTIPLE_H

LeastCommonMultiple.cpp

#include "stdafx.h"
#include "LeastCommonMultiple.h"


LeastCommonMultiple::LeastCommonMultiple(std::vector<int>& list) : m_list(list) 
{ };

int LeastCommonMultiple::GetGreatestCommonDivisor(int a, int b)
{
    for (;;)
    {
        if (a == 0)
        {
        return b;
        }           

        b %= a;

        if (b == 0) 
        {
            return a;
        }

        a %= b;
    }
}

int LeastCommonMultiple::GetLeastCommonMultiple(int a, int b)
{
    int temp = LeastCommonMultiple::GetGreatestCommonDivisor(a, b);

    return temp ? (a / temp * b) : 0;
}

int LeastCommonMultiple::GetLcmForVector()
{
//std::accumulate takes first, last, initial value, operation
    int result = std::accumulate(m_list.begin(), m_list.end(), 1, LeastCommonMultiple::GetLeastCommonMultiple);

    return result;
}

I am getting the error:

Error C3867 'LeastCommonMultiple::GetLeastCommonMultiple': non-standard syntax; use '&' to create a pointer to member EarliestDeadlineFirstScheduling

Before making this code object Oriented I tested it using the GetLcmForVector() function in main() and passing std::accumulate an array and it worked fine.

I have read other questions on this error and the issue was forgetting to add parenthesis at the end of the function BUT I should not have to do that withLeastCommonMultiple::GetLeastCommonMultiple since it is being used in std::accumulate

I am basing my code off this post:

C++ algorithm to calculate least common multiple for multiple numbers

Aucun commentaire:

Enregistrer un commentaire