bcc32 中的简单数组的初始值设定项过多错误 [英] Too many initializers error for a simple array in bcc32

查看:23
本文介绍了bcc32 中的简单数组的初始值设定项过多错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译下面的例子

struct S {};int main() {S 数组[1] = { S() };}

使用 bcc32我收到以下错误:

[bcc32 Error] test.cpp(4): E2225 初始值设定项过多

这是 bcc32 中的错误还是我遗漏了什么并且上面的示例不是有效的 C++?

Clang 和 GCC 编译这个例子都没有问题.

解决方案

Borland BDS2006(可能还有更新的版本)

在其 C++ 引擎中,classstruct默认构造函数/析构函数存在一些问题.

添加自定义(甚至是空的)构造函数/析构函数可以解决许多问题,甚至是您的问题.试试:

struct S{S(){};S(S& a){};~S(){};S* 运算符 = (const S *a){};//S* operator = (const S &a){};//仅当您有动态分配成员时才使用它};int main(){S 数组[1] = { S() };}

我在 BDS2006 中尝试了这个,它看起来有效(如果 struct 中没有任何内容,很难判断)但你至少可以编译和运行......

我首先在 BDS2006 中检测到这种行为......还没有真正尝试过 BCB6,因为它从一开始就是垃圾,几天后就将其关闭(我认为最糟糕的BCB甚至在BCB5中击败了BCB3,4)都很好(在BDS2006之前这是我最喜欢的IDE) 因此他们必须更改 C++ 引擎(不要与运行时库混淆!!!).

添加甚至空的构造函数析构函数也有帮助.如果您有动态分配,则需要处理粗分配.如果您有嵌套的类/结构,请不要忘记也将这些添加到它们中.

Compiling the following example

struct S {};

int main() {
  S array[1] = { S() };
}

with bcc32 I get the following error:

[bcc32 Error] test.cpp(4): E2225 Too many initializers

Is it a bug in bcc32 or am I missing something and the above example is not valid C++?

Both Clang and GCC compile this example without problems.

解决方案

Borland BDS2006 (and possibly newer versions)

has some issues with default constructor/destructor for class and struct inside its C++ engine.

Adding custom (even empty) constructor/destructor solves many issues even yours. Try:

struct S
    {
    S(){};
    S(S& a){};
    ~S(){};
    S* operator = (const S *a){};
    //S* operator = (const S &a){}; // use this only if you have dynamic allocation members
    };

int main()
    {
    S array[1] = { S() };
    }

I tried this in BDS2006 and it looks like it works (hard to tell without anything inside struct) but you can compile and run at least...

I detect this behavior first in BDS2006 ... haven't really try BCB6 as it was junk from the start and dismiss it after few days (I think the worst BCB ever even beats BCB3,4) in BCB5 was all fine (before BDS2006 was this my favorite IDE) with this so they must have change the C++ engine (do not confuse with runtime libs !!!).

Adding even empty constructor destructor helps. If you got dynamic allocations you need to handle those of coarse. If you got nested class/struct do not forget to add these also to them too.

这篇关于bcc32 中的简单数组的初始值设定项过多错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆