Değişkenlerle ifadeleri değerlendirmek için C++ şablonları yazıyorum. Temel olarak, (x + 5) * (x-2) gibi bir yapı için x'in herhangi bir değişkeni için tüm ifadeyi değerlendirir. İlgili kod İşte:İfade - işaretçi hatalarını değerlendirmek için C++ şablonları yazmak?
.Cpp dosyası:
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;
}
başlık dosyası:
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);
};
};
doğru idam Şimdi
(x+5)*(x-2) = 30 for x=5
yazdırır, ben çalışıyorum Bir değişken dizisini kabul etmek için bu kodu genişletin. Yani
int arr[2] = {1,2};
kullanarak aynı şeyi 2 (ya da herhangi bir sayı) bir dizi ayrı değerler (süper basit bir örnek) için değişkenler koymak ve hesaplanması gerekmektedir
(x+y)
verilen.
.Cpp dosyası: Ben bu noktada tıkanır
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;
}
ve burası.
//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);
};
};
ama bunu derlemeye çalıştığımda hataların bir demet olsun: Bu benim başlık dosyasına ne var
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[]){
Herhangi bir yardım en takdir edilecektir
:)
Kodunuzun çalışmasını nasıl beklediğinizden emin değilsiniz. 'ADD' içinde' eval' ın tek bir sürümü vardır ve 'int' kelimesini değil, 'int' (dizinin bozulduğu) için bir işaretçi değildir. – SergeyA