VS 2010 treating code differently to Dev C++

Hi there, I recently made a program using Dev C++ and it would compile and run just as I wanted it. I then imported the .cpp files into Visual Studio 2010 Ultimate and went to recompile except it is not compiling the following part of a function. The code in question is:


1
2
3
4
5
6
7
string Descrambler::order(string word) //descramble words and check them
{
      string input = word;
      int len = word.length(); //length of string
      
      int num[len]; //how many chars there are
...


This would be fine in Dev C++. However in VS it gives the following error:

	1	IntelliSense: expression must have a constant value	


This seems odd and I am unsure of what I am doing wrong. I changed it to the following but it still gave the same error:

1
2
3
string input = word;
int len = 2; //length of string
int num[len];


I see no reason in why this would work fine in Dev C++ but not in VS. any help would be greatly appreciated thank you.
That is because
1
2
int len = 2; //length of string
int num[len];
is illegal in standard C++.

You must use dynamic allocation to do this:
http://cplusplus.com/doc/tutorial/dynamic/

Btw, Dev C++ is old and outdated, I hope that's why you are trying something else.
closed account (z05DSL3A)
Dev-C++ got it wrong, C++ standard only allows for constant integer values to be used for arrays.
1
2
int len = 2; //length of string
int num[len];

This isn't legal C++. In C++, a stack allocated array must have a constant as its size. And you shouldn't use Dev-C++ because it hasn't been updated in over 5 years.

EDIT: :)
Last edited on
Cheers guys. Ok this is just annoying. I prefer the Dev approach it is much more flexible XD

But yes the only reason I used Dev in the first place was that I was unable to download VS to my home comp (vista :'( ...).

The devc++ isn't more flexible, you can do the same things in standard c++. You just do it a bit differently. In C++ you can just use vectors, or dynamic allocation as already pointed out.

New throws an exception when the memory allocation fails, alone because of that it's better.
Last edited on
Topic archived. No new replies allowed.