0

I'm writing a small recursive algorithm. Here is the code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> coins;

int checkchange(int left) {
    vector<int> choices (coins.size());
    if (left == 0)
        return 0;
    else {
        int min;
        for (int i=0;i<coins.size();i++) {
            choices.at(i) = (1 + checkchange(left - coins.at(i)));
        }
        return min_element(choices.front(),choices.back());
    }
}


int main() {
    int N;
    cin >> N;
    for (int i=0;i<N;i++) {
        int c,m,temp,change;
        cin >> c >> m;
        for (int j=0;j<c;j++) {
            cin >> temp;
            coins.push_back(temp);
        }

        for (int j=0;j<m;j++) {
            cin >> temp;
            change = checkchange(temp);
            cout << change;
        }
    }
    return 0;
}

I get the following error:

In file included from
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62,
from burningcoins.cpp:3: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:
In function ‘_FIter std::min_element(_FIter, _FIter) [with _FIter =
int]’: burningcoins.cpp:19: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998:
error: invalid type argument of ‘unary *’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998:
error: invalid type argument of ‘unary *’

I've tried compiling with both g++ and gcc, both give me the same error. What am I doing wrong?

Edit:

New Code:

int checkchange(int left) {
    vector<int> choices (coins.size());
    if (left == 0)
        return 0;
    else {
        for (int i=0;i<coins.size();i++) {
            choices[i] = (1 + checkchange(left - coins.at(i)));
        }
        return *min_element(choices.begin(), choices.end());
    }
}

New error message:

/tmp/ccV3VLsK.o: In function main': <br/> burningcoins.cpp:(.text+0x16a): undefined reference tostd::cin'
burningcoins.cpp:(.text+0x16f): undefined reference to std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x187): undefined reference tostd::cin'
burningcoins.cpp:(.text+0x18c): undefined reference to std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x19b): undefined reference tostd::basic_istream >::operator>>(int&)'
burningcoins.cpp:(.text+0x1b0): undefined reference to std::cin' <br/> burningcoins.cpp:(.text+0x1b5): undefined reference tostd::basic_istream >::operator>>(int&)'
burningcoins.cpp:(.text+0x1ec): undefined reference to std::cin' <br/> burningcoins.cpp:(.text+0x1f1): undefined reference tostd::basic_istream >::operator>>(int&)'
burningcoins.cpp:(.text+0x208): undefined reference to std::cout' <br/> burningcoins.cpp:(.text+0x20d): undefined reference tostd::basic_ostream >::operator<<(int)'
/tmp/ccV3VLsK.o: In function __static_initialization_and_destruction_0(int, int)': <br/> burningcoins.cpp:(.text+0x261): undefined reference tostd::ios_base::Init::Init()'
burningcoins.cpp:(.text+0x266): undefined reference to std::ios_base::Init::~Init()' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_range_check(unsigned long) const':
burningcoins.cpp:(.text._ZNKSt6vectorIiSaIiEE14_M_range_checkEm[std::vector >::_M_range_check(unsigned long) const]+0x2d): undefined reference to std::__throw_out_of_range(char const*)' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)':
burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)]+0x259): undefined reference to __cxa_begin_catch' <br/> burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)]+0x2be): undefined reference to__cxa_rethrow'
burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)]+0x2c8): undefined reference to __cxa_end_catch' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_check_len(unsigned long, char const*) const':
burningcoins.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[std::vector >::_M_check_len(unsigned long, char const*) const]+0x4c): undefined reference to std::__throw_length_error(char const*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::deallocate(int*, unsigned long)':
burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[__gnu_cxx::new_allocator::deallocate(int*, unsigned long)]+0x1c): undefined reference to operator delete(void*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::allocate(unsigned long, void const*)':
burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator::allocate(unsigned long, void const*)]+0x35): undefined reference to std::__throw_bad_alloc()' <br/> burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)]+0x45): undefined reference tooperator new(unsigned long)'
/tmp/ccV3VLsK.o:(.eh_frame+0x12): undefined reference to __gxx_personality_v0' <br/> /tmp/ccV3VLsK.o:(.eh_frame+0x4f): undefined reference to__gxx_personality_v0'
collect2: ld returned 1 exit status

4
  • 2
    min_element(choices.begin(),choices.end()); Commented Oct 17, 2013 at 20:13
  • Careful with the using namespace std;. You have a variable named min and a function named min. It's not quite a conflict, but it's getting close. Commented Oct 17, 2013 at 20:14
  • min_element works properly. You're just not using it properly. You might want to change your question title. Commented Oct 17, 2013 at 20:15
  • Yeah I changed it, using it like you proposed and its still not working. Commented Oct 17, 2013 at 20:31

1 Answer 1

4

std::min_element takes a range. front and back return references to actual values. You should be using the begin and end methods to return an iterator to the corresponding positions in the vector:

min_element(choices.begin(), choices.end());
//                  ^^^^^            ^^^

If you find this tedious, you can create a function which wraps around the standard min_element:

template <class Container>
auto min_element(Container c) -> decltype(std::min_element(c.begin(), c.end()))
{
    return std::min_element(c.begin(), c.end());
}

And use it as:

min_element(choices);
Sign up to request clarification or add additional context in comments.

10 Comments

I've changed it to what you said, and get following error: burningcoins.cpp: In function ‘int checkchange(int)’: burningcoins.cpp:19: error: cannot convert ‘__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >’ to ‘int’ in return I imagine that is because it returns a pointer. But dereferencing it with the star operator gives me even more errors?
@BananaCode Is that the only change you made?
Yes, it is the only change.
@BananaCode What are some of the errors you get when you dereference. Dereferencing is the right thing to do in this case.
burningcoins.cpp: In function ‘int checkchange(int)’: burningcoins.cpp:20: error: expected primary-expression before ‘;’ token. This is the relevant part of the code: return min_element(choices.begin(), choices.end())*;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.