typedef 如何代替过程 C++ 的 struct/class?
How does typedef work in place of struct/class for Procedural c++?
我必须构建一个具有两种数据类型的程序(过程 C++)。一个叫做 Elem,它是向量(一维数组)的一个元素。一个叫做 Vector,它包含一个 unsigned int 来表示数组的大小以及 Elem 本身的数组。我似乎无法找出正确的方法来构造这些,以便它们工作,因为我以前从未用过过程 C++ 做过任何事情。
这就是我的
typedef Elem {
float Element;
}
typedef Vector {
unsigned int size = 0;
Elem* Array = new array[];
}
但我遇到了这个错误
C++ requires a type specifier for all declarations
typedef Elem {
~~~~~~~ ^
还有
error: expected ';' after top level declarator
typedef Elem {
^
我在这里不知所措,任何帮助将不胜感激!
你不说
typdef Elem {
...
};
正确的方法是
struct Elem {
...
};
另请注意声明末尾的分号 ;
。
另请参阅 Class declaration 以获取一些小示例。
typedef float Elem;
struct Vector {
unsigned int size;
Elem* Array;
};
您可以使用构造函数、析构函数、复制语义等定义完整的 Vector class。或者只使用 std::vector<Elem>
.
你可能被C搞糊涂了,其中一个常见的成语是:
typedef struct tagVector {
...
} Vector;
但是这种冗长的语法在 C++ 中是不必要的。
我必须构建一个具有两种数据类型的程序(过程 C++)。一个叫做 Elem,它是向量(一维数组)的一个元素。一个叫做 Vector,它包含一个 unsigned int 来表示数组的大小以及 Elem 本身的数组。我似乎无法找出正确的方法来构造这些,以便它们工作,因为我以前从未用过过程 C++ 做过任何事情。
这就是我的
typedef Elem {
float Element;
}
typedef Vector {
unsigned int size = 0;
Elem* Array = new array[];
}
但我遇到了这个错误
C++ requires a type specifier for all declarations
typedef Elem {
~~~~~~~ ^
还有
error: expected ';' after top level declarator
typedef Elem {
^
我在这里不知所措,任何帮助将不胜感激!
你不说
typdef Elem {
...
};
正确的方法是
struct Elem {
...
};
另请注意声明末尾的分号 ;
。
另请参阅 Class declaration 以获取一些小示例。
typedef float Elem;
struct Vector {
unsigned int size;
Elem* Array;
};
您可以使用构造函数、析构函数、复制语义等定义完整的 Vector class。或者只使用 std::vector<Elem>
.
你可能被C搞糊涂了,其中一个常见的成语是:
typedef struct tagVector {
...
} Vector;
但是这种冗长的语法在 C++ 中是不必要的。