2015-08-28 36 views
1

這是我的第一個C++程序,我不太熟悉c,所以請對我溫柔!C++&ta_lib - 無法解決如何使用TA_MACD函數

我想從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)) 

什麼是e關節是功能「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發現權證指針引用沒有指向單個陣列愚蠢的錯誤。

這裏是我的最終代碼(太長,所以這裏輸入一段代碼!)幫助誰是有太多問題的人:

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

回答

2

有幾個問題(至少3):

  1. io.cpp:51:錯誤:無法轉換TA_Real*' to const TA_Real **'...
    • 使用簡單的 '常量TA_Real *陣列' 而不是 'const的TA_Real *陣列[]'
    • 不使用 '的sizeof(陣列)/的sizeof(*陣列)',以確定陣列長度(它不在函數內部工作),只需在附加參數中傳遞此長度: 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的`存儲大小」是不知道
    • 地方大小在括號中,例如'[ARRAY_SIZE] TA_Real outMacd' 而不是 'TA_Real outMacd []'
  3. io.cpp:61:錯誤:無法轉換const TA_Real**' to常量雙*」 ......
    • 我希望這將自我解決:)

編輯:因此TA_MACD源(http://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_MACD.c),出變量的大小是(endIdx-startIdx + 1)。編輯2:Uhh,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. 編輯功能(C++ 11兼容編譯器還要求d矢量::數據())

    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; 
        } 
    } 
    

可惜的是,我不能重新勾選這個代碼的編譯器,所以用它作爲模板和主題在錯誤的情況下更改。

EDIT3:

What on earth is function "Z2goRSt6vectorIdSaIdEEiii"????

葉,C++喜歡這樣的名字!它是編碼的,你的go()函數的編譯器內部名稱。你可以看到名字 - 'go'和參數類型 - 'vector',i(整數)等等。

C:/Users/ypx7647/Documents/C++/Test/main.cpp:30: undefined reference to `TA_MACD'

看起來TA_lib需要在鏈接器命令行中構建和指定外部lib文件。你可以看到ta-lib/README.TXT文件的說明。

  1. 例如,請訪問:TA-LIB/C /製作/ CSR/LINUX/G ++ /運行 '使' 有
  2. 添加到您的編譯器命令行的路徑,LIB文件和庫文件:

的mingw32-G ++ .EXE [...] -Lta-LIB \ C \ LIB -lta_libc_csr.a

而且,我不能檢查這一點,所以在創作錯誤的情況下,祝你好運!

+0

謝謝,這很有幫助。我修改了代碼來使用C++ 11 std,但仍然有問題。 – Andrew

+0

再次感謝您的幫助。看起來你是正確的。但是,我認爲我現在有一個鏈接問題!見編輯的答案。 – Andrew

+0

它編譯和鏈接 - 非常感謝您的幫助,但是,它不會產生任何結果!有任何想法嗎?我會將代碼添加到編輯的答案中。 – Andrew

相關問題