61 1 C:\Users\86139\Documents\c++\实验19.cpp [Error] new types may not be defined in a return type
时间: 2023-06-30 11:12:03 浏览: 57
这个错误通常是由于在函数的返回类型中定义了新类型导致的。根据 C++ 的语法规则,函数的返回类型必须是已知的类型,不能在返回类型中定义新类型。
例如,在以下代码中:
```
struct Point {
int x, y;
};
Point::Point() {
struct InnerPoint {
int x, y;
};
// ...
}
```
我们试图在 Point 类的构造函数中定义一个名为 InnerPoint 的结构体,这是无效的,并且会导致编译器报告 "new types may not be defined in a return type" 错误。
要解决这个错误,你需要将定义新类型的代码移动到函数体内部,或将其定义为类的成员类型。例如:
```
struct Point {
int x, y;
Point() {
// Move InnerPoint definition to the function body
struct InnerPoint {
int x, y;
};
// ...
}
};
// Or define InnerPoint as a member type of Point
struct Point {
int x, y;
struct InnerPoint {
int x, y;
};
Point() {
// ...
}
};
```
在上面的例子中,我们将 InnerPoint 结构体的定义移动到 Point 类的构造函数体内部,或将其定义为 Point 类的成员类型。这样就可以避免编译器报告 "new types may not be defined in a return type" 错误。