[英]Initial typedef struct pointer (C/C++)
If I define a struct like below 如果我定义如下的结构
typedef struct Coder {
float **data;
};
For my convenience, I also define a pointer to this struct 为了方便起见,我还定义了一个指向该结构的指针
typedef Coder *AUTO;
and then I have to initial it by calling 然后我必须通过调用来初始化
AUTO myInstance = new Coder;
My problem appear when I call 我打电话时出现问题
myInstance->data= NULL;
The VC 2010 told me that there was no type specifier. VC 2010告诉我,没有类型说明符。 I don't understand what problem is here.
我不明白这是什么问题。 Could you please help me?
请你帮助我好吗?
If you are in C++ , just do : 如果您使用的是C ++ ,请执行以下操作:
struct Coder
{
float **data;
};
typedef Coder *AUTO;
Also you have to be sure that the AUTO
declaration is done after the declaration of your struct
or you can forward-declare your struct
. 另外,您还必须确保在声明
struct
之后执行AUTO
声明,或者可以向前声明 struct
。
Also, it is possible that NULL
is undefined. 同样,有可能未定义
NULL
。
You can replace it by 0
or just look at the link I just gave you. 您可以将其替换为
0
也可以只看我刚刚给您的链接。
Here is a live example . 这是一个生动的例子 。
EDIT : The code you gave us cannot work : 编辑:您给我们的代码无法正常工作:
#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];
}
You can only have declaration in namespace scope, not expression like that. 您只能在名称空间范围内进行声明,而不能在那样的表达式中进行声明。
In §7.3.1/1 of the standard : 在该标准的§7.3.1/ 1中:
namespace-body:
名称空间主体:
declaration-seq opt声明顺序选择
which says the namespace-body can optionally contain only declaration . 表示命名空间主体可以选择仅包含声明 。
This code will work : 该代码将起作用:
// 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];
}
In C typedef: 在C typedef中:
typedef int mango;
Now your mango represent "int" datatype. 现在您的芒果代表“ int”数据类型。
user defined struct syntax: 用户定义的结构语法:
struct mystruct;
use typedef: 使用typedef:
typedef struct mystruct newtype;
Now newtype represent your struct mystruct. 现在,新类型代表您的结构mystruct。
Your the typedef name is missing, 您的typedef名称丢失,
typedef struct Coder {
float **data;
}Coder ;
typedef struct Coder {
float **data;
}Coder;
will be ok. 会好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.