Reputation: 217
I am trying to create a polynomial struct that holds its coefficients as well as a monomial function for it. However I get the error:
main.cpp:10:1: error: template declaration of ‘typedef’ typedef struct polynomial_t
My code:
template <typename precision>
typedef struct polynomial_t
{
precision coefficients[] = {1};
precision exact(precision xx) {
return (xx - 2) ^ 9;
};
} poly;
Upvotes: 0
Views: 437
Reputation: 217
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <limits>
using namespace std;
// ONLY MESS WITH THIS STRUCT
template <typename precision>
struct polynomial
{
int degree = 9;
precision coefficients[] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
precision exact(precision xx) {
return pow(xx-2 , 9);
};
} poly;
void summary(const char* prec) {
printf("=========================\n");
printf("\nPrecision: %s\n", prec);
printf("=========================\n");
}
template <typename precision>
precision Horners(const precision xx) {
precision qq = poly<precision>.coefficients[poly<precision>.degree];
for(int i = poly<precision>.degree-1; i >= 0; i--) {
qq = xx * qq + poly<precision>.coefficients[i];
}
return qq;
}
template <typename precision>
precision ForwardError(const precision xx) {
precision pTilde;
for(int i = 0; i <= poly<precision>.degree; i++) {
pTilde += abs(poly<precision>.coefficients[i]) * (abs(xx) ^ i);
}
precision machineEps = numeric_limits<precision>::epsilon();
precision bound = pTilde * (2 * poly<precision>.degree * machineEps) /
(1 - 2 * poly<precision>.degree * machineEps);
return bound;
}
/*
Parameter 1: Singl/Double precision
Parameter 2:
*/
int main(int argc, char *argv[]) {
char prec[6];
if((strcmp(argv[1], "s") == 0) || (strcmp(argv[1], "S") == 0))
strcpy(prec, "Single");
else if((strcmp(argv[1], "d") == 0) || (strcmp(argv[1], "D") == 0))
strcpy(prec, "Double");
else {
printf("Invalid precision identifier\n");
return -1;
}
summary(prec);
float tt = 2.01;
float qq = Horners(tt);
printf("Answer is %f\n", qq);
return 0;
}
Upvotes: 0
Reputation: 56128
When C++ added the class
keyword it also changed so that the entities named by the struct
and class
keywords were treated the same way as other type identifiers, and you no longer had to use those keywords to qualify references to them. Partly this was because there is very little distinction between struct
and class
and forcing people to either remember which one they used or allowing people to use them interchangeably would be very confusing.
The only distinction is the default access level of things declared in a struct. A class foo { .... };
is exactly the same as struct foo { private: .... };
.
So your typedef
is unnecessary and, in fact, no longer legal in C++.
To get the same effect you should probably do this:
template <typename precision>
struct poly
{
precision coefficients[] = {1};
precision exact(precision xx) {
return (xx - 2) ^ 9;
};
};
And if you really want to be able to use polynomial_t
later, you could add typedef poly polynomial_t;
after the declaration. But I wouldn't recommend it.
Your code as other interesting issues. I'm not really sure where you're going with these declarations, but they don't currently look like the beginning of a viable polynomial representation in C++. But, that's just a guess based on limited information.
Particularly concerning to me is that you appear to be trying to use ^
to represent exponentiation. C++ allows operator overloading, and so doing that might be very tempting. But it's a huge change in semantics. Exponentiation and bitwise xor do not have a natural intuitive mapping to each other like string concatenation and addition. Also, the precedence level of ^
is all wrong for it to replace exponentiation sensibly in mathematical expressions.
Upvotes: 2