Stuck on lab assignment.

I require assistance on my code, I'm new to using iterators, and in the my lab today we were instructed to use it, and my instructor has "@todo's" in our source code that we are suppose to do, and I done them, but I get error's such
"
find_max.cpp:42:26: error: no match for call to ‘(std::list<int>) ()’
find_max.cpp:48:28: error: no match for ‘operator[]’ in ‘it[i]’"
and i'm changing things here and there, but no luck. If anyone can point me to right direction, my instructor extended the assignment until midnight tonight.
Any simple ways to explain it to me will be great, since I'm slow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include <list>
#include <climits>
#include "unit_test.h"

using namespace std;

/// @brief Given a list of integers find the maximum valued integer in the list
/// @param l Input list
/// @return Maximum integer value stored in the list
///
/// Use iterators to scan through the list, and find the maximum value stored in 
/// the list.
/// If list is empty, return INT_MIN
int find_max(list<int>& l) {
  ///@todo Implement finding maximum integer

	int max = 0;
	for(list<int>::iterator it = l.begin(); it != l.end(); ++it)
		if(*(it) > max)
			max = *(it);
		
  return max;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief Test Find Max Function
////////////////////////////////////////////////////////////////////////////////
class test_find_max : public test_class {
  public:
    void test() {
      //setup
      //@todo Implement setup
	  list<int> it = {0, 2, 4, 6, 8, 10};
	  
      //test
      //@todo Implement test
      
      int *find_max = it();
	  if(find_max == NULL)
		assert_msg(false, "Error message");  
	  else
	  {
		for (size_t i = 0; i < 10; ++i)
		   if(find_max[i] != it[i]){
			assert_msg(false, "Error message");	
			break;
		   }	
	  }
	  
      //tear down
      //@todo Implement tear down
	  delete[] find_max;
    }
};

/// @brief Create test and run test
int main() {
  test_find_max tfm;
  if(tfm.run())
    std::cout << "Find max was successful!" << std::endl;

  return 0;
}

I don't know what unit testing framework you're using, so it is hard to tell which functions exactly to implement. You shouldn't have to do much logic at all in your test, though. All the logic should already be inside the function you're testing. Look at main in this code and see how it tests the findMax function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <list>
#include <limits>

int findMax(std::list<int> l)
{
    int max = std::numeric_limits<int>::min();

    for (std::list<int>::iterator it = l.begin(); it != l.end(); ++it)
        if ((*it) > max)
            max = (*it);

    return max;
}

int main()
{
    const std::list<int> testList = { -5, 0, 2, 4, 6, 7 };
    int theMax = findMax(testList);
    
    //you would call assert_msg(theMax == 7, "findMax did not return expected value");
    //instead of this if-else, but I don't have the unit_test header that you do
    if (theMax == 7)
    {
        std::cout << "Test passed\n";
    }
    else
    {
        std::cout << "Test failed\n";
    }
}


Make sure you test the case where all the numbers in the list are negative. Right now that test would fail because you set max to zero initially in your find_max function. It should actually be set to the smallest integer possible.
Last edited on
@booradley60
Here's the unit test frame work that they provided:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

////////////////////////////////////////////////////////////////////////////////
/// @brief Test class
////////////////////////////////////////////////////////////////////////////////
class test_class {
  public:
    /// @brief Constructor
    test_class() {}
    /// @brief Destructor
    virtual ~test_class() {}
    /// @brief Run Unit test's setup, test, and teardown functions
    /// @return Passed test or not
    bool run() {
      setup();
      test();
      tear_down();
      return all_tests_passed();
    }

  protected:
    /// @brief Setup data to be tested
    virtual void setup() {};
    /// @brief Test data
    virtual void test() = 0;
    /// @brief Teardown data which was tested
    virtual void tear_down() {};

    /// @return All asserts have passed
    bool all_tests_passed() {return !fail;}

    /// @brief Assert a unit test passes
    /// @param b Conditional for assert
    /// @param msg Message on fail
    void assert_msg(bool b, std::string msg) {
      if(!b) {
        std::cerr << msg << std::endl;
        fail = true;
      }
    }

  private:
    bool fail = false; ///< Stores if any test case has failed
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <list>
#include <limits>
#include <string>

int findMax(std::list<int> l)
{
    int max = std::numeric_limits<int>::min();

    for (std::list<int>::iterator it = l.begin(); it != l.end(); ++it)
        if ((*it) > max)
            max = (*it);

    return max;
}

class test_find_max : public test_class
{
private:
    std::list<int> testData;
    int expectedMax;
protected:
    virtual void setup()
    {
        testData = { -5, 0, 2, 4, 6, 7 };
        expectedMax = 7;
    }

    virtual void test()
    {
        int testMax = findMax(testData);
        assert_msg(testMax == expectedMax, "findMax did not return expected result");
    }

    //no tear_down needed, nothing is actually constructed in setup
};

int main()
{
    test_find_max tfm;
    if (tfm.run())
    {
        std::cout << "All tests passed.\n";
    }
    else
    {
        std::cout << "There was a test failure.\n";
    }
}
Last edited on
hmm I see, thank you!
Any suggestions on where to look for to get a better understanding of using iterators? Beside cplusplus.
Topic archived. No new replies allowed.