我如何操纵此动态向量进行折叠操作

问题描述 投票:0回答:0
%type <list> list expressions list_choice

野牛方向是传递折叠的方向,折叠的操作员以及动态向量到函数
evaulateFold()
.
FOLD direction operator list_choice ENDFOLD SEMICOLON {$$ = evaluateFold($2, $3, $4);}

我到这一点上,我无法用动态数组做任何事情。我试图将其复制到使用循环的常规数组中,但它说动态阵列没有起点参考。我尝试将数组传递到
this range-based 'for' statement requires a suitable "begin" function and none was foundC/C++(2291)

使用
evaluateFold()
值,但后来说没有办法将

const std::vector<double>&

转换为
std::vector<double>*
。我应该如何能够将此数组用于函数?
std::vector<double>&``no suitable constructor exists to convert from "std::vector<double, std::allocator<double>> *" to "std::vector<double, std::allocator<double>> &"C/C++(415)
i我试图为实际的折叠功能使用variadic函数,但我什至无法让矢量允许我使用它。
double evaluateFold(Operators direction, Operators action, vector<double>* values){

    double result = 0;
    switch(direction){
        case LFOLD:
            switch(action){
                case SUBTRACT:
                    subFromLeft(values);
                    for (auto i : values)
                        cout << i << endl;
                    break;
            }
        case RFOLD:
            switch(action){
                case SUBTRACT:
                    for (auto i : values)
                        cout << i << endl;
                    break;
            }
            break;
    }
    return result;
}
我确实使用
template <typename ...Args>
auto subFromLeft(Args ...args)
{
    return (... - args);
}
一直通过动态数组,但后来说没有办法解决

vector<double>* values
vector<double>* values

,这就是我从操作中需要的结果。

这里是我尝试完成折叠操作的另一种方式,但是再次,它不允许我使用
double
参考,因为它说没有可行的转换。

const std::vector<double>& values

如果任何人都可以帮助我解决这个问题,或者至少将我指向正确的方向,那真是太棒了。这是C ++的问题,而不是Flex/Bison,但我不知道这会帮助我。
make生成以下内容:
double subFromLeft(const std::vector<double>& values) {
    if (values.empty()) return 0;  // Handle empty vector
    double result = values[0];

    for (size_t i = 1; i < values.size(); i++) {
        result -= values[i];
    }

    return result;
}
parser.y: In function ‘int yyparse()’: parser.y:127:146: error: invalid initialization of reference of type ‘std::vector<double>&’ from expression of type ‘std::vector<double>*’

贝洛(Below)是折叠功能可以解释/评估的样本。

In file included from parser.y:21: values.h:18:76: note: in passing argument 3 of ‘double evaluateFold(Operators, Operators, std::vector<double>&)’

    

@yksisarvinen最初对此进行了评论,但由于某种原因,他删除了评论。使用
// Test Left Fold function main returns integer; begin fold left - (3, 2, 1) endfold; end;

取消动态阵列

(*values)

使我可以轻松地将其发送到其他功能并获得结果。其他几个回答,但@yksisarvinen是第一个提及它的人。


c++ compiler-construction bison dynamic-arrays bisonc++
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.