[英]How does typedef work in place of struct/class for Procedural c++?
I have to build a program(Procedural C++) that has two data types. 我必须构建一个具有两种数据类型的程序(过程C ++)。 One called Elem that is an element of a vector(single dimensional array).
一个称为Elem的元素,它是向量(单维数组)的元素。 and One called Vector that contains an unsigned int for the size of the array and also the array of Elem's itself.
一个称为Vector的向量,其中包含一个无符号整数,该整数的大小与数组的大小以及Elem本身的数组相同。 I can't seem to figure out the correct way to structure these so that they work as I have never done anything with procedural c++ before.
我似乎无法弄清楚构造这些结构的正确方法,以使它们正常工作,因为我以前从未使用过过程性c ++进行任何操作。
This is what I have 这就是我所拥有的
typedef Elem {
float Element;
}
typedef Vector {
unsigned int size = 0;
Elem* Array = new array[];
}
but I'm getting this error 但我收到这个错误
C++ requires a type specifier for all declarations
typedef Elem {
~~~~~~~ ^
and also 并且
error: expected ';' after top level declarator
typedef Elem {
^
I'm at a loss here, any help would be appreciated! 我不知所措,任何帮助将不胜感激!
You don't say 你不说
typdef Elem {
...
};
The correct way is 正确的方法是
struct Elem {
...
};
Note also the semicolon ;
注意分号
;
at the end of the declaration. 在声明的末尾。
See also Class declaration for some small examples. 有关一些小示例,请参见类声明 。
typedef float Elem;
struct Vector {
unsigned int size;
Elem* Array;
};
You may define a full-fledged Vector class with constructor, destructor, copy semantic, etc. Or just use std::vector<Elem>
. 您可以使用构造函数,析构函数,复制语义等定义完整的Vector类。或者只需使用
std::vector<Elem>
。
You might be confused by C, in which a common idiom is: 您可能会对C感到困惑,其中一个常见用法是:
typedef struct tagVector {
...
} Vector;
But this verbose syntax is unnecessary in C++. 但是这种冗长的语法在C ++中是不必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.