CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2022
    Posts
    14

    Question use of algorithms in function cause ERROR: no instance of overloaded function

    I'm trying to use an algorithm in a function.

    Should be very simple.

    However, regardless of which algroithm I attempt to use, all of them cause the same error when used in a function.

    E0304 no instance of overloaded function "std::begin" matches the argument list

    E0304 no instance of overloaded function "std::end" matches the argument list

    I am guessing there is some small change that needs to be made.


    Code:
    #include <iostream>
    #include <algorithm>
    
    #include "bool_element_option_03.h"
    #include "storage.h"
    
    int main()
    {
        int arr_value[ELEMENTS]{ 1, 2, 9, 4, 5, 6, 7, 8 };
    
        int arr_copy_value[ELEMENTS];
        
        // array population
        for (int var_create_array_a = 0; var_create_array_a < ELEMENTS; var_create_array_a++)
        {
            arr_copy_value[var_create_array_a] = 0;
        }
    
        //std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
        //std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
        for (int output = 0; output < ELEMENTS; output++)
        {
            std::cout << "copied decimals: " << arr_copy_value[output] << std::endl;
        }
    
        bool_element_option_03(arr_value, arr_copy_value);
    
        return 0;
    }

    Code:
    #ifndef _STORAGE_H
    #define _STORAGE_H
    #define WIN32_LEAN_AND_MEAN
    
    // -----------------------------------------------------------------------------------------------------------------------------------------------------
    //                  Constants
    // -----------------------------------------------------------------------------------------------------------------------------------------------------
    
    //-----------------------------------------------
    const int ELEMENTS = 8;
    //-----------------------------------------------
    
    #endif

    Code:
    #include <iostream>
    #include <algorithm>
    
    #include "storage.h"
    
    void bool_element_option_03(int arr_value[], int* arr_copy_value)
    {
        std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
        std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
        for (int output = 0; output < ELEMENTS; output++)
        {
            std::cout << "copied decimals: " << arr_copy_value[output] << std::endl;
        }
    }
    If I take these alorgithms out of the function and put them in main(), they work as they should.

    Should I intentionally overload this function (so I can use algorithms in it)?

    Overloading this function is not my intnetion. I'm not calling it multiple times with different arguments. This function is only being called once.

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    In the function case, the array sizes are unknown. I suggest you switch to std::vector (or std::array) rather than using old C-style arrays. It will work and is better C++.
    Last edited by wolle; November 25th, 2022 at 02:17 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    std::begin(), std::end() and std::size() requires that the size of the array to be known. When passing an array as function parameter this size is unknown as the array is 'decayed' to a pointer. The size of the number of elements referenced by a pointer can't be determined. If you have to use a c-style array (rather than the C++ usual vector or array as Wolle suggests above, then you can pass by ref as opposed to pointer pass by value. Consider (as 1 file):

    Code:
    #include <iostream>
    #include <algorithm>
    
    constexpr size_t ELEMENTS { 8 };
    
    void show(const int(&arr)[ELEMENTS]) {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    
    void bool_element_option_03(const int (&arr_value)[ELEMENTS], int (&arr_copy_value)[ELEMENTS]) {
    	std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    
    int main() {
    	const int arr_value[ELEMENTS] { 1, 2, 9, 4, 5, 6, 7, 8 };
    	int arr_copy_value[ELEMENTS] {};
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value);
    }
    which displays:

    Code:
    Original order:
    12945678
    
    Reverse sorted:
    98765421
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2022
    Posts
    14

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    yeah, I klnow...c-arrays cause issues.
    many years ago, I just got comfortable using C-style arrays.
    I know it's a crutch :P

    thank you for your help.
    I really appreciate it!

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    Quote Originally Posted by maninthemiddle View Post
    yeah, I klnow...c-arrays cause issues.
    many years ago, I just got comfortable using C-style arrays.
    I know it's a crutch
    Well, as they say on the moon: Switching to std::vector would be one small step for you but one giant leap for the quality of your code.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    Quote Originally Posted by wolle View Post
    Well, as they say on the moon: Switching to std::vector would be one small step for you but one giant leap for the quality of your code.
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    As a first baby step, move from c-array to std::array and then to std::vector:

    with std:;array:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <array>
    
    constexpr size_t ELEMENTS { 8 };
    
    using Arr = std::array<int, ELEMENTS>;
    
    void show(const Arr& arr) {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    
    void bool_element_option_03(const Arr& arr_value, Arr& arr_copy_value) {
    	//std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
            arr_copy_value = arr_value;
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    
    int main() {
    	const Arr arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
    	Arr arr_copy_value;
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value);
    }
    and then with std::vector:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    
    using Arr = std::vector<int>;
    
    void show(const Arr& arr) {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    
    void bool_element_option_03(const Arr& arr_value, Arr& arr_copy_value) {
    	//std::copy(std::begin(arr_value), std::end(arr_value), std::back_insert_iterator<Arr>(arr_copy_value));
            arr_copy_value = arr_value;
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    
    int main() {
    	const Arr arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
    	Arr arr_copy_value;
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value);
    }
    Note that with std::array and std::vector, you can do a direct assignment without using std:copy().
    Last edited by 2kaud; November 26th, 2022 at 11:24 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Nov 2022
    Posts
    14

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    lol, thank guys, I promise I will move to modern containers.

    Once this project is finished, I will go through and change every array to std::array but that goal includes 20+ arrays, 7 functions and 60+ errors (which were caused two days ago when I last tried converting this project).

    At the moment I am not in a hurry do that.
    First I have a separate set of complex set of unlated logic issues to deal with.

    why is std::vector better than std::array?

    from Stroustrup's 2013 C++ book and 'how to program' from Paul & Harvey Deitel, I am under the impression that both have their respective use cases....
    Last edited by maninthemiddle; November 26th, 2022 at 08:14 PM.

  9. #9
    Join Date
    Nov 2022
    Posts
    14

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    I added one more issue to my little problem
    as you can see below I included a 2D array
    and a cout line for it in the function.
    however, I can't figure out howe to dereference. I tried the & and the * but neither worked :/


    Code:
    #include <iostream>
    #include <algorithm>
    
    constexpr size_t ELEMENTS{ 8 };
    constexpr size_t SLOTS{ 32 };
    
    void show(const int(&arr)[ELEMENTS])
    {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    Code:
    void bool_element_option_03(const int(&arr_value)[ELEMENTS], int(&arr_copy_value)[ELEMENTS], int(&arr_elements)[ELEMENTS][SLOTS])
    {
    	std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    
    	for (int var_ones_index = 0; var_ones_index < SLOTS; var_ones_index++)
    	{
    		// returning addresses; not sure how to dereferecne this
    		std::cout << arr_elements[ELEMENTS * SLOTS + var_ones_index] << std::endl;
    	}
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    Code:
    int main()
    {
    	const int arr_value[ELEMENTS]{ 1, 2, 9, 4, 5, 6, 7, 8 };
    	int arr_copy_value[ELEMENTS]{};
    	int arr_elements[ELEMENTS][SLOTS];
    
    	for (int var_create_array_a = 0; var_create_array_a < ELEMENTS; var_create_array_a++)
    	{
    		for (int var_create_array_b = 0; var_create_array_b < SLOTS; var_create_array_b++)
    		{
    			arr_elements[var_create_array_a][var_create_array_b] = 0;
    		}
    	}
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value, arr_elements);
    }
    Last edited by maninthemiddle; November 26th, 2022 at 08:10 PM.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    why is std::vector better than std::array?
    std:;array, like c-style arrays, have their size set at compile time. std::vectors can have their size dynamically changed at run-time - by adding or removing elements.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    Do you mean something like:

    Code:
    #include <iostream>
    #include <algorithm>
    
    constexpr size_t ELEMENTS { 8 };
    constexpr size_t SLOTS { 32 };
    
    void show(const int(&arr)[ELEMENTS]) {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    
    void bool_element_option_03(const int(&arr_value)[ELEMENTS], int(&arr_copy_value)[ELEMENTS], int(&arr_elements)[ELEMENTS][SLOTS]) {
    	std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    	for (const auto& e : arr_elements)
    		for (const auto& v : e)
    			std::cout << v << ' ';
    
    	std::cout << '\n';
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    
    int main() {
    	const int arr_value[ELEMENTS] { 1, 2, 9, 4, 5, 6, 7, 8 };
    	int arr_copy_value[ELEMENTS] {};
    	int arr_elements[ELEMENTS][SLOTS] {};
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value, arr_elements);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Nov 2022
    Posts
    14

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    Close!

    I need to be able to manipulate the contents of the 2D array within the function.
    that is the purpose of the function. sry, I should have mentioned that sooner.

    as far I I know, this style of for loop only allows iteration
    for (const auto& e : arr_elements)
    for (const auto& v : e)
    std::cout << v << ' ';

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    You can access each element simply like this:

    Code:
    for (size_t rw {}; rw < ELEMENTS; ++rw)
        for (size_t co {}; co < SLOTS; ++co)
            std::cout << arr_elements[rw][co] << ' ';
    
    std::cout << '\n';
    Also note that if you remove the const, then you can change the element using a range_for. eg:

    Code:
    for (auto& e : arr_elements)
        for (auto& v : e)
            v = 99;    // Or value required.
    Last edited by 2kaud; November 28th, 2022 at 04:36 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Feb 2017
    Posts
    677

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    I have never tried it before, but std::span of C++ 20 seems a nice way of using C-style arrays with the standard library. An advantage is that it works with std::vector and std::array too.

    https://www.modernescpp.com/index.php/c-20-std-span

    (In C++ 23, there is also the std::mdspan. It will allow you to view a one-dimensional array (std::vector, std::array, or C-style array) as a multidimensional array.)

    https://www.youtube.com/watch?v=aFCLmQEkPUw

    Code:
    #include <iostream>
    #include <algorithm>
    #include <span>
    #include <vector>
    #include <array>
    
    constexpr size_t ELEMENTS { 8 };
    
    void show(std::span<int> arr) {
    	for (const auto& c : arr)
    		std::cout << c;
    
    	std::cout << '\n';
    }
    
    void bool_element_option_03(std::span<int> arr_value, std::span<int> arr_copy_value) {
    	std::copy(std::begin(arr_value), std::end(arr_value), std::begin(arr_copy_value));
    	std::sort(std::rbegin(arr_copy_value), std::rend(arr_copy_value));
    
    	std::cout << "\nReverse sorted:\n";
    	show(arr_copy_value);
    }
    
    void test() {
    	int arr_value[ELEMENTS] { 1, 2, 9, 4, 5, 6, 7, 8 };
    	int arr_copy_value[ELEMENTS] {};
    
    		// works too
    //	std::vector<int> arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
    //	std::vector<int> arr_copy_value(arr_value.size(), {});
    
    		// works too
    //	std::array<int, ELEMENTS> arr_value { 1, 2, 9, 4, 5, 6, 7, 8 };
    //	std::array<int, ELEMENTS> arr_copy_value {};
    
    	std::cout << "\nOriginal order:\n";
    	show(arr_value);
    	bool_element_option_03(arr_value, arr_copy_value);
    }
    Last edited by wolle; November 30th, 2022 at 03:47 PM.

  15. #15
    Join Date
    Nov 2022
    Posts
    14

    Re: use of algorithms in function cause ERROR: no instance of overloaded function

    thank , I go it now!
    Last edited by maninthemiddle; November 29th, 2022 at 10:30 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured