Failed to Compile ( comparison between signed and unsigned integer expressions)

Jan 30, 2019 at 4:48am
Hello, can someone help with this code ? its not compiling and I am having such hard time fixing it. This is regarding a lab I'm working on.
-----------------------------------------------------------------------

Failed to compile

ShoppingCart.cpp: In member function ‘void ShoppingCart::add(ItemToPurchase&)’:
ShoppingCart.cpp:65:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < item.size(); i++)
~~^~~~~~~~~~~~~
ShoppingCart.cpp: In member function ‘void ShoppingCart::remove(std::__cxx11::string)’:
ShoppingCart.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < item.size(); i++)
~~^~~~~~~~~~~~~
ShoppingCart.cpp: In member function ‘void ShoppingCart::update(ItemToPurchase&)’:
ShoppingCart.cpp:143:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < item.size(); i++)
~~^~~~~~~~~~~~~
ShoppingCart.cpp:155:15: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member named ‘SetDescription’
item.at(i).SetDescription(updateItem.GetDescription());
^~~~~~~~~~~~~~
ShoppingCart.cpp:155:41: error: ‘class ItemToPurchase’ has no member named ‘GetDescription’
item.at(i).SetDescription(updateItem.GetDescription());
^~~~~~~~~~~~~~
ShoppingCart.cpp: In member function ‘void ShoppingCart::showDescription()’:
ShoppingCart.cpp:197:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < item.size(); i++)
~~^~~~~~~~~~~~~
ShoppingCart.cpp:201:55: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member named ‘GetDescription’
cout << item.at(i).GetName() << " : " << item.at(i).GetDescription() << endl;
^~~~~~~~~~~~~~
ShoppingCart.cpp: In member function ‘void ShoppingCart::showCart()’:
ShoppingCart.cpp:227:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < item.size(); i++)
~~^~~~~~~~~~~~~
Jan 30, 2019 at 4:53am
The "comparison between signed and unsigned integer expressions" is just a warning. Warnings can be just as bad as an error, but they don't stop the code from compiling.

The following are your errors. They are saying that "SetDescription" and "GetDescription" don't exist in the "ItemToPurchase" class.

ShoppingCart.cpp:155:15: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member namedSetDescription
item.at(i).SetDescription(updateItem.GetDescription());

ShoppingCart.cpp:155:41: error: ‘class ItemToPurchasehas no member namedGetDescription
item.at(i).SetDescription(updateItem.GetDescription());

ShoppingCart.cpp:201:55: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member namedGetDescription
cout << item.at(i).GetName() << " : " << item.at(i).GetDescription() << endl;
Last edited on Jan 30, 2019 at 4:56am
Jan 30, 2019 at 6:22am
The sign comparison warnings happen because the size function returns a type of std::size_t ,so try this:

for (std::size_t i = 0; i < item.size(); i++)

If you are going to do something to all the items in a container, you can use a range based for loop, look at the example at the end:

https://en.cppreference.com/w/cpp/language/range-for
Jan 30, 2019 at 3:46pm
thanks you very much guys, I was able to fix the "for loop" but I am still trying to get around the ones that "duch" suggested with little to no luck at all.

Any other suggestions for the below errors ?

ShoppingCart.cpp: In member function ‘void ShoppingCart::update(ItemToPurchase&)’:
ShoppingCart.cpp:155:15: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member named ‘SetDescription’
item.at(i).SetDescription(updateItem.GetDescription());
^~~~~~~~~~~~~~
ShoppingCart.cpp:155:41: error: ‘class ItemToPurchase’ has no member named ‘GetDescription’
item.at(i).SetDescription(updateItem.GetDescription());
^~~~~~~~~~~~~~
ShoppingCart.cpp: In member function ‘void ShoppingCart::showDescription()’:
ShoppingCart.cpp:201:55: error: ‘__gnu_cxx::__alloc_traits<std::allocator<ItemToPurchase> >::value_type {aka class ItemToPurchase}’ has no member named ‘GetDescription’
cout << item.at(i).GetName() << " : " << item.at(i).GetDescription() << endl;
Jan 30, 2019 at 3:52pm
Make sure the spelling is correct. C++ is case-sensitive, so GetDescription and getDescription would be considered different names.
Feb 5, 2019 at 5:18am
thank you all.
Topic archived. No new replies allowed.