3

这是我的第一个 c++ 程序,我对 c 也不是很熟悉,所以请对我温柔一点!

我试图从 ta_lib 库中调用 TA_MACD 函数但没有成功。有人可以帮帮我吗?

我的代码是这样的:

#include <iostream>
#include "ta_libc.h"

using namespace std;

void go(const TA_Real *array[], TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod);

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

int main(int argc, char**argv) 
{
    TA_Real close_data[]={1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

void go(const TA_Real *array[], TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod) {
    TA_Integer outBegin = 0, outElements = 0;
    TA_Real outMacd[];
    TA_Real outSignal[];
    TA_Real outHistogram[];

    TA_RetCode retCode = TA_MACD( (sizeof(array)/sizeof(*array))-1-slowPeriod, (sizeof(array)/sizeof(*array))-1, array, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd, outSignal, outHistogram );    

    if(retCode == TA_SUCCESS) 
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

当我使用 MinGW 编译时,出现以下错误:

"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
"/C/MinGW/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/ta_macd_example.exe
make.exe[2]: Entering directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/io.o.d"
g++    -c -g -I/C/ta-lib-0.4.0-msvc/ta-lib/c/include -MMD -MP -MF "build/Debug/MinGW-Windows/io.o.d" -o build/Debug/MinGW-Windows/io.o io.cpp
io.cpp: In function `int main(int, char**)':
io.cpp:51: error: cannot convert `TA_Real*' to `const TA_Real**' for argument `1' to `void go(const TA_Real**, TA_Integer, TA_Integer, TA_Integer)'
io.cpp: In function `void go(const TA_Real**, TA_Integer, TA_Integer, TA_Integer)':
io.cpp:57: error: storage size of `outMacd' isn't known
io.cpp:58: error: storage size of `outSignal' isn't known
io.cpp:59: error: storage size of `outHistogram' isn't known
io.cpp:61: error: cannot convert `const TA_Real**' to `const double*' for argument `3' to `TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)'
make.exe[2]: *** [build/Debug/MinGW-Windows/io.o] Error 1
make.exe[2]: Leaving directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
make.exe": *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 10s)

我将是第一个承认我不知道自己在做什么的人,因此将不胜感激。

另外,当我不知道 TA_MACD 函数返回多少时,如何设置 out 变量的大小?

提前致谢。

使用 C++11 Std 修改后,我现在在这里,仍然有问题。

#include <iostream>
#include <array>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(TA_Real *close, const int array_size, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;
    array<TA_Real, array_size-1> outMacd;
    array<TA_Real, array_size-1> outSignal;
    array<TA_Real, array_size-1> outHistogram;

    TA_RetCode retCode = TA_MACD( array_size-1-slowPeriod, array_size-1, close, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd, outSignal, outHistogram );

    if(retCode == TA_SUCCESS)
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    array<TA_Real,259> close_data={1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,close_data.size(),12,26,9);
    return 0;
}

这是输出:

||=== Build: Debug in Test (compiler: GNU GCC Compiler) ===|
C:\Users\ypx7647\Documents\C++\Test\main.cpp||In function 'void go(TA_Real*, int, TA_Integer, TA_Integer, TA_Integer)':|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 9 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 10 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 11 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|26|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|27|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|28|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp||In function 'int main(int, char**)':|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|70|error: cannot convert 'std::array<double, 259u>' to 'TA_Real* {aka double*}' for argument '1' to 'void go(TA_Real*, int, TA_Integer, TA_Integer, TA_Integer)'|
||=== Build failed: 16 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

进行了修改和调整后,建议我的代码现在是:

#include <iostream>
#include <vector>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx = close.size()-1-slowPeriod;
    int endIndx = close.size()-1;
    int array_size = endIndx - beginIndx + 1;
    vector<TA_Real> outMacd(array_size, 0.0);
    vector<TA_Real> outSignal(array_size, 0.0);
    vector<TA_Real> outHistogram(array_size, 0.0);

    const TA_Real* close_c_pointer = close.data();
    TA_Real* outMacd_c_pointer = outMacd.data();
    TA_Real* outSignal_c_pointer = outMacd.data();
    TA_Real* outHistogram_c_pointer = outMacd.data();

    TA_RetCode retCode = TA_MACD( beginIndx, endIndx, close_c_pointer, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd_c_pointer, outSignal_c_pointer, outHistogram_c_pointer );

    if(retCode == TA_SUCCESS)
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    vector<TA_Real> close_data {1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

我相信我现在有一个链接器错误:

-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -I..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include -c C:\Users\ypx7647\Documents\C++\Test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\Test.exe obj\Debug\main.o   
obj\Debug\main.o: In function `Z2goRSt6vectorIdSaIdEEiii':
C:/Users/ypx7647/Documents/C++/Test/main.cpp:30: undefined reference to `TA_MACD'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 4 second(s))
1 error(s), 0 warning(s) (0 minute(s), 4 second(s))

到底什么是函数“Z2goRSt6vectorIdSaIdEEiii”????

进展顺利,程序现在编译并链接,但它为输出生成 0。有任何想法吗?

这是我的最新代码:

#include <iostream>
#include <vector>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx = close.size()-slowPeriod-1;
    int endIndx = close.size();
    int array_size = endIndx - beginIndx + 1;
    cout << "beginIndx = " << beginIndx << endl << "endIndx = " << endIndx << endl << "array_size = " << array_size << endl;
    vector<TA_Real> outMacd(array_size, 0.0);
    vector<TA_Real> outSignal(array_size, 0.0);
    vector<TA_Real> outHistogram(array_size, 0.0);

    const TA_Real* close_c_pointer = close.data();
    TA_Real* outMacd_c_pointer = outMacd.data();
    TA_Real* outSignal_c_pointer = outMacd.data();
    TA_Real* outHistogram_c_pointer = outMacd.data();

    TA_RetCode retCode = TA_MACD( beginIndx, endIndx, close_c_pointer, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd_c_pointer, outSignal_c_pointer, outHistogram_c_pointer );

    cout << "retCode = " << retCode << endl;

    if(retCode == TA_SUCCESS)
    {
        cout << "outBegin = " << outBegin << " outElements = " << outElements << endl;
        int lastElement = outElements-1;
        cout << "outMacd.at(lastElement) = " << outMacd.at(lastElement) << " outSignal.at(lastElement) = " << outSignal.at(lastElement) << " outHistogram.at(lastElement) = " << outHistogram.at(lastElement) << endl;
        MacdResult.Macd = outMacd.at(lastElement);
        MacdResult.Signal = outSignal.at(lastElement);
        MacdResult.Histogram = outHistogram.at(lastElement);

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histogram = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    vector<TA_Real> close_data {1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

程序输出:

beginIndx = 232
endIndx = 259
array_size = 28
retCode = 0
outBegin = 232 outElements = 28
outMacd.at(lastElement) = 0 outSignal.at(lastElement) = 0 outHistogram.at(lastEl
ement) = 0
Macd = 0 Signal = 0 Histogram = 0

Process returned 0 (0x0)   execution time : 0.080 s

@truf 发现 ec 指针引用不指向单个数组的愚蠢错误。

这是我的最终代码(输入太长,所以这里是一个片段!)帮助有问题的人:

    MacdResult.Macd = outMacd.at(lastElement);
    MacdResult.Signal = outSignal.at(lastElement);
    MacdResult.Histogram = outHistogram.at(lastElement);
4

1 回答 1

2

有几个问题(至少三个):

  1. io.cpp:51:错误:无法转换TA_Real*' toconst TA_Real**' ...
    • 使用简单的 'const TA_Real *array' 而不是 'const TA_Real *array[]'
    • 不要使用 'sizeof(array)/sizeof(*array)' 来确定数组长度(它在函数内部不起作用),只需将此长度传递给附加参数: void go(const TA_Real *array[], TA_Integer array_size, ...) 并调用它: go(close_data, sizeof(close_data)/sizeof(*close_data), 12, 26, 9);
  2. io.cpp:57:错误:“outMacd”的存储大小未知
    • 将大小放在括号中,例如 'TA_Real outMacd[array_size]' 而不是 'TA_Real outMacd[]'
  3. io.cpp:61:错误:无法转换const TA_Real**' toconst double*'...
    • 我希望这会自行解决:)

编辑:因此 TA_MACD 来源(http://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_MACD.c),输出变量的大小是( endIdx-startIdx+1)。

EDIT2:嗯,C++11 让事情变得更糟:) TA_Lib 使用纯 C 接口,因此您需要准确地进行 C++ 对象和 C 类型之间的转换。

我提出这样的建议:

  1. 使用向量而不是数组(在 C++11 兼容的编译器上)

    #include <array>
    vector<TA_Real> close_data { 31.09999000,1.09995000,1.09996000, ... }
    
  2. 所以你不需要在调用时传递数组长度

    go(close_data,12,26,9);
    
  3. 编辑功能(vector::data() 也需要 C++11 兼容编译器)

    void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
    {
        TA_Integer outBegin = 0, outElements = 0;
        vector<TA_Real> outMacd(array_size, 0.0);
        vector<TA_Real> outSignal(array_size, 0.0);
        vector<TA_Real> outHistogram(array_size, 0.0);
    
        TA_RetCode retCode = TA_MACD( array_size-1-slowPeriod, array_size-1, close.data(), fastPeriod, slowPeriod, signalPeriod, outBegin, outElements, outMacd.data(), outSignal.data(), outHistogram.data() );
    
        if(retCode == TA_SUCCESS)
        {
            int lastElement = outElements-1;
            MacdResult.Macd = outMacd[lastElement];
            MacdResult.Signal = outSignal[lastElement];
            MacdResult.Histogram = outHistogram[lastElement];
    
            cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
        }
    }
    

不幸的是,我无法使用编译器重新检查此代码,因此将其用作模板并在出现错误时进行更改。

编辑3:

到底什么是函数“Z2goRSt6vectorIdSaIdEEiii”????

是的,C++ 喜欢这样的名字!它是 go() 函数的编码的编译器内部名称。您可以看到名称 - 'go',以及参数类型 - 'vector'、i(整数)等等。

C:/Users/ypx7647/Documents/C++/Test/main.cpp:30:未定义对“TA_MACD”的引用

看起来 TA_lib 需要在链接器命令行中构建和指定外部 lib 文件。您可以查看 ta-lib/README.TXT 文件以获取说明。

  1. 例如转到:ta-lib/c/make/csr/linux/g++/ 并在那里运行“make”
  2. 将 lib 文件和 lib 文件的路径添加到编译器命令行:

mingw32-g++.exe [...] -Lta-lib\c\lib -lta_libc_csr.a

另外,我无法检查这个,所以如果出现错误,请有创意,祝你好运!

于 2015-08-28T10:28:13.340 回答