Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板结构错误_C++_Templates_Struct_Typedef - Fatal编程技术网

C++ 模板结构错误

C++ 模板结构错误,c++,templates,struct,typedef,C++,Templates,Struct,Typedef,我试图创建一个多项式结构,它保存它的系数以及它的单项式函数。但是我得到了一个错误: 我的代码: template <typename precision> typedef struct polynomial_t { precision coefficients[] = {1}; precision exact(precision xx) { return (xx - 2) ^ 9; }; } poly; 模板 类型定义结构多项式 { 精度系

我试图创建一个多项式结构,它保存它的系数以及它的单项式函数。但是我得到了一个错误:

我的代码:

template <typename precision>
typedef struct polynomial_t
{
    precision coefficients[] = {1};
    precision exact(precision xx) {
        return (xx - 2) ^ 9;
    }; 
} poly;
模板
类型定义结构多项式
{
精度系数[]={1};
精度精确(精度xx){
报税表(xx-2)^9;
}; 
}聚;

当C++添加了<代码>类< /Cord>关键字时,它也改变了,因此,由<代码>结构> <代码>和<代码>类< /Cord>关键字命名的实体被视为与其他类型标识符相同的方式,并且不再需要使用这些关键字来限定对它们的引用。这部分是因为
struct
class
之间几乎没有区别,强迫人们记住他们使用的是哪一个,或者允许人们互换使用它们会让人非常困惑

唯一的区别是结构中声明的内容的默认访问级别。A
类foo{…}与结构foo{private:..}完全相同

<> p> >您的<代码> TyPulfF<代码>是不必要的,事实上,在C++中不再合法。 要获得相同的效果,您可能需要执行以下操作:

template <typename precision>
struct poly
{
    precision coefficients[] = {1};
    precision exact(precision xx) {
        return (xx - 2) ^ 9;
    }; 
};
模板
结构多边形
{
精度系数[]={1};
精度精确(精度xx){
报税表(xx-2)^9;
}; 
};
如果您真的希望以后能够使用
polymonal\u t
,您可以添加
typedef poly polymonal\u t在声明之后。但我不推荐

您的代码与其他有趣的问题一样。我不太确定这些声明的位置,但它们现在看起来不像C++中可行的多项式表示的开始。但是,这只是基于有限信息的猜测


特别让我担心的是,您似乎试图使用
^
来表示幂运算。C++允许运算符重载,这样做可能非常诱人。但这是语义上的巨大变化。求幂运算和按位异或运算不像字符串串联和加法那样具有自然直观的相互映射。另外,在数学表达式中,代码> > 的优先级水平是完全错误的,在C++中添加了<>代码>类< /COD>关键字时,它也改变了,使得由<代码>结构> <代码>和<代码>类< /Cord>关键字命名的实体被视为与其他类型标识符相同的方式,您不再需要使用这些关键字来限定对它们的引用。这部分是因为
struct
class
之间几乎没有区别,强迫人们记住他们使用的是哪一个,或者允许人们互换使用它们会让人非常困惑

#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;
}
唯一的区别是结构中声明的内容的默认访问级别。A
类foo{…}与结构foo{private:..}完全相同

<> p> >您的<代码> TyPulfF<代码>是不必要的,事实上,在C++中不再合法。 要获得相同的效果,您可能需要执行以下操作:

template <typename precision>
struct poly
{
    precision coefficients[] = {1};
    precision exact(precision xx) {
        return (xx - 2) ^ 9;
    }; 
};
模板
结构多边形
{
精度系数[]={1};
精度精确(精度xx){
报税表(xx-2)^9;
}; 
};
如果您真的希望以后能够使用
polymonal\u t
,您可以添加
typedef poly polymonal\u t在声明之后。但我不推荐

您的代码与其他有趣的问题一样。我不太确定这些声明的位置,但它们现在看起来不像C++中可行的多项式表示的开始。但是,这只是基于有限信息的猜测

特别让我担心的是,您似乎试图使用
^
来表示幂运算。C++允许运算符重载,这样做可能非常诱人。但这是语义上的巨大变化。求幂运算和按位异或运算不像字符串串联和加法那样具有自然直观的相互映射。另外,
^
的优先级别完全错误,它在数学表达式中明智地取代了幂运算。

\include
#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;
}
#包括 #包括 #包括 #包括 #包括 使用名称空间std; //只会弄乱这个结构 模板 结构多项式 { int度=9; 精度系数[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; 精度精确(精度xx){ 返回功率(xx-2,9); }; }聚; 无效摘要(常量字符*prec){ printf(“===========================================\n”); printf(“\n精度:%s\n”,prec); printf(“===========================================\n”); } 模板 精密喇叭(常数精度xx){ 精度qq=多系数[多阶]; 对于(int i=poly.degree-1;i>=0;i--){ qq=xx*qq+多元系数[i]; } 返回qq; } 模板 精度转发错误(常数精度xx){ 精密光电二极管; 对于(int i=0;i
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//只会弄乱这个结构
模板
结构多项式
{
int度=9;
精度系数[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
精度精确(精度xx){
返回功率(xx-2,9);
}; 
}聚;
无效摘要(常量字符*prec){
printf(“===========================================\n”);
printf(“\n精度:%s\n”,prec);
printf(“===========================================\n”);
}
模板
精密喇叭(常数精度xx){
精度qq=多系数[多阶];
对于(int i=poly.degree-1;i>=0;i--){
qq=xx*qq+多元系数[i];
}
返回qq;
}
模板
精度转发错误(常数精度xx){
精密光电二极管;

对于(int i=0;我为什么你认为那里需要
typedef
呢?来自C?我认为
typedef
是声明structs时的标准。“我认为
typedef