2016-04-08 13 views
2

私は変数を使って式を評価するC++テンプレートを書いています。基本的には、(x + 5)*(x-2)のような構造の場合、任意の変数xについて式全体を評価します。ここでは、関連するコードは次のとおりです。式ポインタエラーを評価するためのC++テンプレートの作成?

.cppファイル:

int main(int argc, const char * argv[]){ 

    int x = 5; 
    typedef MULTIPLY < 
     ADD < VAR, LIT<5> >, 
     SUBSTRACT < VAR, LIT<2> > 
    > 
    EXPRESSION; 

    EXPRESSION e; 

    printf("(x+5)*(x-2) = %d for x=%d", e.eval(x), x); 

    return 0; 

} 

ヘッダファイル:

struct VAR{ 
    static inline int eval(int i){ return i; }; 
}; 

template<int INT> 
struct LIT{ 
    static inline int eval(int i){ return INT; }; 
}; 

template<class L, class R> 
struct ADD{ 
    static inline int eval(int i){ 
     return L::eval(i) + R::eval(i); 
    }; 
}; 

template<class L, class R> 
struct SUBSTRACT{ 
    static inline int eval(int i){ 
     return L::eval(i) - R::eval(i); 
    }; 
}; 

template<class L, class R> 
struct MULTIPLY{ 
    static inline int eval(int i){ 
     return L::eval(i) * R::eval(i); 
    }; 
}; 

正しく実行今

(x+5)*(x-2) = 30 for x=5 

を出力したときに、私がしようとしています変数の配列を受け入れるようにこのコードを展開します。だから、

(x+y) 

に与え

int arr[2] = {1,2}; 

は順番に変数を入れて、アレイ(超簡単な例)から2を使用して同じこと(または任意の数の)別の値を計算する必要があります。

.cppファイル:

int main(int argc, const char * argv[]){ 

    int arr[2] = {1,2}; 
    typedef ADD < VARS<2>, VARS<2> > 
    EXPRESSION; 

    EXPRESSION e; 

    printf("(x+y) = %d\n", e.eval(arr)); 

    return 0; 

} 

と私は動けなくなる場所です。

//take an array arr[] of size N 
template<int N> 
struct VARS{ 
    static inline int eval(int arr[]){ 
     //go for next value 
     VARS<N-1>::eval(arr+1); 
     //end return current one 
     return arr[0]; 
    }; 
}; 

// if array size = 0, end execution 
template<> 
struct VARS<0>{ 
    static inline int eval(int arr[]){ return 0; }; 
}; 

template<class L, class R> 
struct ADD{ 
    static inline int eval(int i){ 
     return L::eval(i) + R::eval(i); 
    }; 
}; 

が、私はそれをコンパイルしようとすると、私はエラーの束を得る:これは私がヘッダファイルに持っているものである

ExpressionTemplate.cpp: In function 'int main(int, const char**)': 
ExpressionTemplate.cpp:17:35: error: invalid conversion from 'int*' to 'int' [-fpermissive] 
    printf("(x+y) = %d\n", e.eval(arr)); 
           ^
In file included from ExpressionTemplate.cpp:5:0: 
ExpressionTemplate.h:19:23: note: initializing argument 1 of 'static int ADD<L, R>::eval(int) [with L = VARS<2>; R = VARS<2>]' 
    static inline int eval(int i){ 
        ^
ExpressionTemplate.h: In instantiation of 'static int ADD<L, R>::eval(int) [with L = VARS<2>; R = VARS<2>]': 
ExpressionTemplate.cpp:17:35: required from here 
ExpressionTemplate.h:20:23: error: invalid conversion from 'int' to 'int*' [-fpermissive] 
     return L::eval(i) + R::eval(i); 
        ^
ExpressionTemplate.h:6:23: note: initializing argument 1 of 'static int VARS<N>::eval(int*) [with int N = 2]' 
    static inline int eval(int arr[]){ 
        ^
ExpressionTemplate.h:20:36: error: invalid conversion from 'int' to 'int*' [-fpermissive] 
     return L::eval(i) + R::eval(i); 
            ^
ExpressionTemplate.h:6:23: note: initializing argument 1 of 'static int VARS<N>::eval(int*) [with int N = 2]' 
    static inline int eval(int arr[]){ 
すべてのヘルプは、ほとんど理解されるであろう

:)

+3

コードがどのように機能するのかよくわかりません。 'ADD'の中に' eval'のバージョンは1つだけあり、 'int'へのポインタではなく' int'をとります(あなたの配列が崩壊する)。 – SergeyA

答えて

4

あなたのコードは、ADDeval関数に(int*になる)の配列を渡しています。しかし、ADD::evalを定義して、単一のintを取得しました。配列を渡すときには、ADDに別の関数を追加するか、このクラスを扱う新しいクラス(すなわち、VAR_ADD)を作成する必要があります。

+0

ありがとうございました! C++とポインタ全体についてはまだ新しいです。 – tihomirbz

関連する問題