Page 1 of 1

Unable to initialize variables or constance using {}. Using () works fine

Posted: Fri May 15, 2020 2:44 am
by BeijingCowboy
CodeLite 14.0.0
iMac Pro Processor 2.3GHz, 18-Core Intel Xeon W
Memory 128GB 2666 MHz DDR4
Graphics Radeon Pro9 Vega 64 16GB
OS Catalina 10.15.4

I'm following an online tutorial that has me initializing variables using curly braces rather than parenthesis.

Unfortunately I keep getting the following build error.

At first I thought I was writing the code incorrectly. So I copied the code onto my windows machine and compiled it using the MS Blend IDE. It compiled and ran fine.

Next I assumed it must be the compiler. So I went into the project settings and changed the compiler CLANG. When I built it again the error persisted.

Here is the error I'm getting, followed by my code.

Code: Select all

***************************************
/bin/sh -c '/usr/bin/make -j36 -e -f Makefile'
--------------Building project: [Shipping - Debug ]------------------
/usr/bin/g++ -c "/Users/BeijingCowboy/Programming_Projects/CodeLite_Projects/Shipping/Shipping/main.cpp" -g O0 -Wall -o Debug/main.cpp.o -I. -I./user/bin/g++ -c "/Users/BeijingCowboy/Programming_Projects/CodeLite_Projects/Shipping/Shipping/main.pp:30:14: expected ';' at end of declaration

int length{}, width{}, height{};
                 ^
                  ;
/usr/bin/g++ -c "/Users/BeijingCowboy/Programming_Projects/CodeLite_Projects/Shipping/Shipping/main.cpp:31:21: error: expected ';' at end of declaration
double base_cost{2.50};
                                ^
                                 ;

//it continues to do this for every variable and constant in my code

14 errors generated.
make[1]: *** [Debug/main.cpp.o] Error 1
make: *** [All] Error 2
=====15 errors, 0 warnings======

Code: Select all

********************************************

// My code
include <iostream>

using namespace std;

int main()

{
 
   int length{}, width{}, height{};
    double base_cost{2.50};
    
    const int tier1_threshold{100}; //volume
    const int tier2_threshold{500}; // volume
    
    int max_dimension_length {10}; // inches
    
    double tier1_surcharge{0.10}; // 10% extra
    double tier2_surcharge{0.25}; // 25% extra
    
    // All dimensions must be 10 inches or less
    
    int package_volume{};
    
    cout << "Welcome to the package cost calculator" << endl;
    cout << "Enter the length, width, and height of the package separated by spaces";
    
    cin >> length >> width >> height;
    
    if (length > max_dimension_length || width > max_dimension_length || height > max_dimension_length){
        cout << "Sorry, package rejected - dimension exceeded" << endl;
        } else {
        cout << "xxx" << endl;}
        
        
    return 0;
}
*******************************************************

Re: Unable to initialize variables or constance using {}. Using () works fine

Posted: Fri May 15, 2020 1:39 pm
by DavidGH
Hi,

(I edited your post to wrap the code in code-tags (the </> toolbar button); they make it much easier to read, and also to copy, more than 1 or 2 lines of code.)

I doubt if this is a CodeLite issue; it's more likely to be due to your mac compiler or its version. I just tested in a CodeLite project, using g++ 8.3.0 (on linux). It happily compiled your code.

I suggest you try compiling the project outside CodeLite, in a terminal. If you get the same problem you'll know it's not due to CodeLite, in which case you might ask on a mac or g++ forum.

Regards,

David

Re: Unable to initialize variables or constance using {}. Using () works fine

Posted: Fri May 15, 2020 1:50 pm
by eranif
Try using clang and not g++ on Mac.
If you don't have it, you will need to install it:

Code: Select all

xcode-select --install 
Then switch the compiler to use clang in CodeLite