Possible error in Chater 18.7 Gassup

#1
struct CityAndRemainingGas
{
  int city=0, remaining_gallons=0;
};

cityRemainingGas = {i, remaining_gas}

Setting member variable inline overrides the default constructor.

error: no match for 'operator=' (operand types are 'FindAmpleCity(const std::vector<int>&, const std::vector<int>&)::CityAndRemainingGas' and '<brace-enclosed initializer list>')

To fix it, you need to remove the int city=0, remaining_gallons=0 to int city, remaining_gallons

0 Likes

#2

Hey @telenoobies,

I wonder what is your command to compile this file? Following is mine, and it works perfectly on my Mac:

clang++ -Weverything -Wno-c++98-compat -Wno-missing-prototypes Gassing_up.cc -stdlib=libc++ -std=c++14 -O3

Could you try that and let me know if there is any problem still?

0 Likes

#3

Hey Tsung Hsein,

I dont haveclang++. I am using g++ -std=c++11 gassup.cpp on 64bit windows 10.

I also tried with -std=c++14 flag, and it gives me the same error.

0 Likes

#4

You can try this at http://ideone.com/, and this is my try there (by using C++14), http://ideone.com/ICx9jd

0 Likes

#5

Hey @telenoobies,

I used to use gcc before but due to its hard-to-understand compile message and lack of support of C++11 (at that time 5 years ago). I switched to clang eventually. You might want to try https://clang.llvm.org/get_started.html to install clang on your windows.

0 Likes

#6

Hey Tsung Hsein.

The reason why its failing is because I compiled it with g++4.8.

I used a g++6.2 compiler and it works with -std=c++14 flag.

0 Likes