dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Expression did not evaluate to a constant

  1. #1
    Join Date
    Dec 2017
    Posts
    38

    Expression did not evaluate to a constant

    I'm getting this error "Expression did not evaluate to a constant" when declaring a string in Visual Studio;

    string data[inc+1];

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,460

    Re: Expression did not evaluate to a constant

    What is inc defined as?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  3. #3
    Join Date
    Dec 2017
    Posts
    38

    Re: Expression did not evaluate to a constant

    "inc" is an int

    Code:
    int inc = 0;
    
        string line;
        varCount1 = 0;
        ifstream ddt( "file.dat" );
        if (ddt){
            while (getline( ddt, line)){
                if( varCount1 == 1 ){
                     inc = atoi(line.c_str());
                }
                varCount1++;
            }
        wallet1.close();
        }
    
        string data[inc +1];

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,460

    Re: Expression did not evaluate to a constant

    Sorry, you can't do that in c++. At compile time the compiler needs to know the size of the array. If you only know the size at run-time then you need to consider using a container such as a vector. See http://www.cplusplus.com/reference/vector/vector/ - or if absolutely necessary then use dynamic memory.

    What are you trying to achieve?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,251

    Re: Expression did not evaluate to a constant

    Quote Originally Posted by binary2 View Post
    I'm getting this error "Expression did not evaluate to a constant" when declaring a string in Visual Studio;

    string data[inc+1];
    Just use vector<string> rather than plain string array.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)