0

如果我定义如下结构

typedef struct Coder {
    float **data;
};

为方便起见,我还定义了一个指向该结构的指针

typedef Coder *AUTO;

然后我必须通过调用来初始化它

AUTO  myInstance = new Coder;

我打电话时出现我的问题

myInstance->data= NULL;

VC 2010 告诉我没有类型说明符。我不明白这里有什么问题。请你帮助我好吗?

4

3 回答 3

6

如果您在C++中,只需执行以下操作:

struct Coder
{
    float **data;
};

typedef Coder *AUTO;

此外,您必须确保AUTO声明是在您的声明之后完成的,struct或者您可以前向声明您的struct.


此外,它可能NULL是未定义的。

您可以将其替换为0或仅查看我刚刚给您的链接。

这是一个活生生的例子


编辑: 你给我们的代码不能工作:

#include <tchar.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <iostream>

struct Coder {
        float **data; // 2-D matrix
};
typedef Coder *AUTO;

AUTO  myFeatures = new Coder;

myFeatures->data = NULL; // Forbidden

int main (){
        myFeatures->data = new float *[2];
        myFeatures->data[0] = new float [2];
}

您只能在命名空间范围内声明,而不是这样的表达式。

在标准的§7.3.1/1中:

命名空间主体:
     声明序列选择

这表示 namespace-body 可以选择仅包含declaration

此代码将起作用:

// Your includes

struct Coder {
        Coder() : data(NULL) {} // Constructor who initialize data to NULL
                                // via the member-initialization-list

        float **data; // 2-D matrix
};
typedef Coder *AUTO;

AUTO  myFeatures = new Coder; // Do you want to keep it in global ??
                              // It's not a really good design

int main (){
        // You should put your declaration here.

        // myFeatures->data = NULL; useless now
        myFeatures->data = new float *[2];
        myFeatures->data[0] = new float [2];
}
于 2013-08-20T11:27:58.197 回答
1

在 C 类型定义中:

typedef int mango;

现在你的芒果代表“int”数据类型。

用户定义的结构语法:

struct mystruct;

使用类型定义:

typedef struct mystruct newtype;

现在 newtype 代表你的结构 mystruct。

您的 typedef 名称丢失,

typedef struct Coder {
    float **data;
}Coder ;
于 2013-08-20T11:52:25.950 回答
1
typedef struct Coder {
    float **data;
}Coder;

会好的。

于 2013-08-20T12:04:38.260 回答