From: polaris on
Im not specialized in c++ but I have application written in c++ I have
to compile and run for research purposes.

the g++ compiler (version 3.4) gives me this error

iq_router.cpp:32: error: ISO C++ forbids initialization in array new

this is the line of code where I get this error

_vc[i] = new VC [_vcs] ( config, _outputs );

and another line of code gives the same error:

_next_vcs = new BufferState [_outputs] ( config );

how can I fix the problem

I tried g++ 3.3 but it seems never work.

please help

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran Pusic on
On Dec 17, 2:35 pm, polaris <ksa....(a)hotmail.com> wrote:
> Im not specialized in c++ but I have application written in c++ I have
> to compile and run for research purposes.
>
> the g++ compiler (version 3.4) gives me this error
>
> iq_router.cpp:32: error: ISO C++ forbids initialization in array new
>
> this is the line of code where I get this error
>
> _vc[i] = new VC [_vcs] ( config, _outputs );

I don't know what standard says, but I've never seen such code.
Normally, one can only use default constructor when allocating an
array. That is, only

TYPE* p = new TYPE[count];

is possible. Perhaps some compiler writer in some compiler version
decided that this syntax is ok, and initially, this code was compiled
with a such compiler.

You can do this:

_vc[i] = new VC [_vcs];
for (size_t j=0;j<_vcs; j++)
vc[i][j] = VC(config, _outputs);

If you can switch _vc[i] to be std::vector<VC>, not VC*, you can do

_vc = std::vector(_vcs, VC(config, _outputs));

(You should be doing something similar anyway.)

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bo Persson on
polaris wrote:
> Im not specialized in c++ but I have application written in c++ I
> have to compile and run for research purposes.
>
> the g++ compiler (version 3.4) gives me this error
>
> iq_router.cpp:32: error: ISO C++ forbids initialization in array new
>
> this is the line of code where I get this error
>
> _vc[i] = new VC [_vcs] ( config, _outputs );
>
> and another line of code gives the same error:
>
> _next_vcs = new BufferState [_outputs] ( config );
>
> how can I fix the problem
>

Just like the compiler says, you cannot do this. :-)

You either have to initialize the array separately, perhaps using
std::fill_n, or use a std::vector instead of a raw array.

std::vector<BufferState> vcs(_outputs, config);


Bo Persson



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: analizer on
On Dec 17, 4:35 pm, polaris <ksa....(a)hotmail.com> wrote:
> _vc[i] = new VC [_vcs] ( config, _outputs );

can be rewritten with:

_vc[i] = (VC*)calloc(_vcs, sizeof(VC));
for(int j = _vcs; j-- ;)
{
new(_vc[i]+j) VC(config[j], _outputs[j]);
}

P.S. don't forget to #include <memory.h>, not sure if <new> required.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
polaris wrote:
> Im not specialized in c++ but I have application written in c++ I have
> to compile and run for research purposes.
>
> the g++ compiler (version 3.4) gives me this error
>
> iq_router.cpp:32: error: ISO C++ forbids initialization in array new
>
> this is the line of code where I get this error
>
> _vc[i] = new VC [_vcs] ( config, _outputs );
>
> and another line of code gives the same error:
>
> _next_vcs = new BufferState [_outputs] ( config );
>
> how can I fix the problem
>
> I tried g++ 3.3 but it seems never work.
>
> please help
>

It never has worked in C++ but will in C++0x where we have provided
several mechanisms that will allow solving the problem (but not for
basic arrays.)

If you use a std::vector instead of a dynamic array you will IIRC find
that in C++0x you can provide an initialisation list.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]