-5

currently compiling my set.cpp file(we had to make that file based on the set.h file and test_set.cpp) Using a g++ compiler, and I keep running into these warnings:

set.cpp: In member function âvoid set::remove(const set::value_type&)â:
set.cpp:30: warning: comparison between signed and unsigned integer expressions
set.cpp: In member function âbool set::contains(const set::value_type&) constâ:
set.cpp:50: warning: comparison between signed and unsigned integer expressions
set.cpp: In function âset set_union(const set&, const set&)â:
set.cpp:65: warning: comparison between signed and unsigned integer expressions
set.cpp: In function âset set_intersection(const set&, const set&)â:
set.cpp:76: warning: comparison between signed and unsigned integer expressions
set.cpp: In function âset set_difference(const set&, const set&)â:
set.cpp:90: warning: comparison between signed and unsigned integer expressions
set.cpp: In function âbool is_subset(const set&, const set&)â:
set.cpp:104: warning: comparison between signed and unsigned integer expressions
set.cpp: In function âbool operator==(const set&, const set&)â:
set.cpp:118: warning: comparison between signed and unsigned integer expressions
    set.cpp: In function âstd::ostream& operator<<(std::ostream&, const set&)â:
set.cpp:131: warning: comparison between signed and unsigned integer expressions

I am unsure of what these mean, and was wondering how one would go about fixing this.

Iris
  • 29
  • 4
  • 10

2 Answers2

2

The warning you are getting is most likely coming from your for loops:

example:

void set::remove(const value_type& entry)
{
    for(int i = 0; i < used; i++) //the comparison in question is on this line
    {
       if(data[i] == entry)
       {
            data [i]  = data [used - 1];
            used --;
            return;
       }
    }
}

The statement: i < used is comparing i which is an int and used which I am assuming is of an unsigned type.

If you were to look at each line number specified in the warning I believe they would all correspond to the for loops in your functions.

The easiest way to fix these warnings would be to replace int with whatever type you are using for used.

For example if used were and unsigned int your for loops would become:

void set::remove(const value_type& entry)
{
    for(unsigned int i = 0; i < used; i++) 
    {
        /*...*/
    }
}
Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
1

Without seeing your header file, I am assuming used is defined as an unsigned int. In your loops, you define i as an int, leading to your warnings.

Because a negative value in a signed integer evaluates to a large positive number in an unsigned integer, comparing the two can lead to unexpected results. The quick solution is to change all uses of i to unsigned int or whatever actual type is used for used.

Mikel F
  • 3,567
  • 1
  • 21
  • 33