Why this code does not work?

Hi

Why this code does not work?

1
2
  std::vector<int>::iterator itMinElem = std::find(inputArr.begin(),
            inputArr.end(), minElem);


Output:
error: conversion from '__gnu_cxx::__normal_iterator<const int*, std::vector<int> >' to non-scalar type 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' requested

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
/* 
 * File:   CHomework.cpp
 * Author: Ivan
 * 
 * Created on May 30, 2013, 1:51 PM
 */

#include <algorithm>
#include "CHomework.h"

void CHomework::homework(const std::vector<int>& inputArr,
        int& sum,
        int& product) throw (std::out_of_range) {

    sum = 0;
    product = 1;

    // Generate an exception if the input data is out of range
    for (std::size_t i = 0; i < inputArr.size(); i++) {
        if ((inputArr[i] < -100) || (inputArr[i] > 100)) {
            throw std::out_of_range("Input out of range: [-100, 100]");
        }
    }

    // Find the sum of the positive elements of the array
    for (std::size_t i = 0; i < inputArr.size(); i++) {
        if (inputArr[i] > 0) {
            sum += inputArr[i];
        }
    }
    
    // Find product between the first maximum and minimum elements of an array

    int minElem = *std::min_element(inputArr.begin(), inputArr.end());
    int maxElem = *std::max_element(inputArr.begin(), inputArr.end());
    
    std::vector<int>::iterator itMinElem = std::find(inputArr.begin(),
            inputArr.end(), minElem);
//    std::vector<int>::iterator itMaxElem = std::find(inputArr.begin(),
//            inputArr.end(), maxElem);
//    
//    if (itMinElem == itMaxElem) {
//        product = 0;
//        return;
//    }
//    
//    std::vector<int>::iterator itBeginProduct;
//    std::vector<int>::iterator itEndProduct;
//    if (itMinElem < itMaxElem) {
//        itBeginProduct = itMinElem;
//        itEndProduct = itMaxElem;
//    }
//    else {
//        itBeginProduct = itMaxElem;
//        itEndProduct = itMinElem;
//    }
//    
//    for (std::vector<int>::iterator it = itBeginProduct+1;
//            it < itEndProduct; it++) {
//        product *= *it;
//    }
}


But it works:
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
/* 
 * File:   main.cpp
 * Author: Ivan
 *
 * Created on May 29, 2013, 4:52 PM
 */

// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector

int main() {
    std::vector<int> inputArr;

    inputArr.push_back(-7);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int minElem = *std::min_element(inputArr.begin(), inputArr.end());
    int maxElem = *std::max_element(inputArr.begin(), inputArr.end());

    std::vector<int>::iterator itMinElem = std::find(inputArr.begin(),
            inputArr.end(), minElem);
}
Maybe because your homework() function has its parameter as a const?
Yes, you're right! But I have not changed anything ... Now I'll know. Thank you!
Last edited on
You should use ::const_iterator instead of ::iterator ;)
This is Excellent! I'll try it. Thanks!
And now another error. Help please!

Output:
39: undefined reference to `CHomework::homework(std::vector<int, std::allocator<int> > const&, int&, int&)'

For each line:
 
CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * File:   newtestclass.cpp
 * Author: Ivan
 *
 * Created on May 30, 2013, 2:02:10 PM
 */

#include "newtestclass.h"
#include "../CHomework.h"


CPPUNIT_TEST_SUITE_REGISTRATION(newtestclass);

newtestclass::newtestclass() {
}

newtestclass::~newtestclass() {
}

void newtestclass::setUp() {
}

void newtestclass::tearDown() {
}

void newtestclass::testHomework_01() {
    std::vector<int> inputArr;

    inputArr.push_back(-7);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));

    int expectedSum = 17;
    int expectedProduct = -15;

    CPPUNIT_ASSERT_EQUAL(expectedSum, sum);
    CPPUNIT_ASSERT_EQUAL(expectedProduct, product);
}

void newtestclass::testHomework_02() {
    std::vector<int> inputArr;

    inputArr.push_back(3);
    inputArr.push_back(14);
    inputArr.push_back(-9);
    inputArr.push_back(4);
    inputArr.push_back(-5);
    inputArr.push_back(1);
    inputArr.push_back(-12);
    inputArr.push_back(4);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));

    int expectedSum = 26;
    int expectedProduct = 180;

    CPPUNIT_ASSERT_EQUAL(expectedSum, sum);
    CPPUNIT_ASSERT_EQUAL(expectedProduct, product);
}

void newtestclass::testHomework_03() {
    std::vector<int> inputArr;

    inputArr.push_back(-5);
    inputArr.push_back(1);
    inputArr.push_back(2);
    inputArr.push_back(3);
    inputArr.push_back(4);
    inputArr.push_back(5);
    inputArr.push_back(6);
    inputArr.push_back(7);
    inputArr.push_back(8);
    inputArr.push_back(-3);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));

    int expectedSum = 36;
    int expectedProduct = 5040;

    CPPUNIT_ASSERT_EQUAL(expectedSum, sum);
    CPPUNIT_ASSERT_EQUAL(expectedProduct, product);
}

void newtestclass::testHomework_MinAndMaxAreLocatedNear() {
    std::vector<int> inputArr;

    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(-7);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));

    int expectedSum = 17;
    int expectedProduct = 0;

    CPPUNIT_ASSERT_EQUAL(expectedSum, sum);
    CPPUNIT_ASSERT_EQUAL(expectedProduct, product);
}

void newtestclass::testHomework_THROW_lowBound() {
    std::vector<int> inputArr;

    inputArr.push_back(-101);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_THROW(cHomework.homework(inputArr, sum, product),
            std::out_of_range);
}

void newtestclass::testHomework_NO_THROW_lowBound() {
    std::vector<int> inputArr;

    inputArr.push_back(-100);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));
}

void newtestclass::testHomework_THROW_highBound() {
    std::vector<int> inputArr;

    inputArr.push_back(101);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_THROW(cHomework.homework(inputArr, sum, product),
            std::out_of_range);
}

void newtestclass::testHomework_NO_highBound() {
    std::vector<int> inputArr;

    inputArr.push_back(100);
    inputArr.push_back(5);
    inputArr.push_back(-1);
    inputArr.push_back(3);
    inputArr.push_back(9);

    int sum;
    int product;
    CHomework cHomework;

    CPPUNIT_ASSERT_NO_THROW(cHomework.homework(inputArr, sum, product));
}
I would guess that you have failed to provide a definition for CHomework::homework().
I do not see an error.

CHomework.h
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
/* 
 * File:   CHomework.h
 * Author: Ivan
 *
 * Created on May 30, 2013, 1:51 PM
 */

#ifndef CHOMEWORK_H
#define	CHOMEWORK_H

#include <stdexcept>
#include <vector>

/**
 * Implementation of the class CHomework
 */
class CHomework {
public:
    /**
     * Returns the sum and the product from the input array
     * 
     * @param inputArr The input array with the
     * elements from the range [-100, 100]
     * @param sum The sum of the positive elements of the array
     * @param product The product numbers arranged between the
     * minimum and maximum elements
     */
    void homework(std::vector<int>& inputArr,
            int& sum,
            int& product) throw(std::out_of_range);
};

#endif	/* CHOMEWORK_H */ 


CHomework.cpp
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
/* 
 * File:   CHomework.cpp
 * Author: Ivan
 * 
 * Created on May 30, 2013, 1:51 PM
 */

#include <algorithm>
#include "CHomework.h"

void CHomework::homework(std::vector<int>& inputArr,
        int& sum,
        int& product) throw (std::out_of_range) {

    sum = 0;
    product = 1;

    // Generate an exception if the input data is out of range
    for (std::size_t i = 0; i < inputArr.size(); i++) {
        if ((inputArr[i] < -100) || (inputArr[i] > 100)) {
            throw std::out_of_range("Input out of range: [-100, 100]");
        }
    }

    // Find the sum of the positive elements of the array
    for (std::size_t i = 0; i < inputArr.size(); i++) {
        if (inputArr[i] > 0) {
            sum += inputArr[i];
        }
    }
    
    // Find product between the first maximum and minimum elements of an array

    int minElem = *std::min_element(inputArr.begin(), inputArr.end());
    int maxElem = *std::max_element(inputArr.begin(), inputArr.end());
    
    std::vector<int>::iterator itMinElem = std::find(inputArr.begin(),
            inputArr.end(), minElem);
    std::vector<int>::iterator itMaxElem = std::find(inputArr.begin(),
            inputArr.end(), maxElem);
    
    if (itMinElem == itMaxElem) {
        product = 0;
        return;
    }
    
    std::vector<int>::iterator itBeginProduct;
    std::vector<int>::iterator itEndProduct;
    if (itMinElem < itMaxElem) {
        itBeginProduct = itMinElem;
        itEndProduct = itMaxElem;
    }
    else {
        itBeginProduct = itMaxElem;
        itEndProduct = itMinElem;
    }
    
    for (std::vector<int>::iterator it = itBeginProduct+1;
            it < itEndProduct; it++) {
        product *= *it;
    }
}
I'm currently using ::const_iterator instead of ::iterator. Everything is working. Thank you all :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void CHomework::homework(const std::vector<int>& inputArr,
        int& sum,
        int& product) throw (std::out_of_range) {

    ...
    std::vector<int>::const_iterator itMinElem = std::find(inputArr.begin(),
            inputArr.end(), minElem);
    std::vector<int>::const_iterator itMaxElem = std::find(inputArr.begin(),
            inputArr.end(), maxElem);
    ...

    ...
    std::vector<int>::const_iterator itBeginProduct;
    std::vector<int>::const_iterator itEndProduct;
    ...

    ...
    for (std::vector<int>::const_iterator it = itBeginProduct+1;
            it < itEndProduct; it++) {
        product *= *it;
    }
}
Topic archived. No new replies allowed.