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.
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++.
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
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!
Re: use of algorithms in function cause ERROR: no instance of overloaded function
Quote:
Originally Posted by
maninthemiddle
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. :)
Re: use of algorithms in function cause ERROR: no instance of overloaded function
Quote:
Originally Posted by
wolle
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. :)
:thumb::thumb::thumb:
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().
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....
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);
}
Re: use of algorithms in function cause ERROR: no instance of overloaded function
Quote:
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.
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);
}
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
Quote:
for (const auto& e : arr_elements)
for (const auto& v : e)
std::cout << v << ' ';
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.
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);
}
Re: use of algorithms in function cause ERROR: no instance of overloaded function
Re: use of algorithms in function cause ERROR: no instance of overloaded function
I don't understand what you're trying to do in bool_element_option_03() above. You seem to be trying to access a 2-d array as a 1-d array??
If yes, then something like:
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));
const auto it { (int*)&arr_elements };
for (size_t i {}, ie { ELEMENTS * SLOTS }; i < ie; ++i)
std::cout << it[i] << ' ';
// std::cout << *(it + i) << ' ';
std::cout << '\n';
std::cout << "\nReverse sorted:\n";
show(arr_copy_value);
}
or:
Code:
const auto* it { (int*)&arr_elements };
for (size_t i {}, ie { ELEMENTS * SLOTS }; i < ie; ++i, ++it)
std::cout << *it << ' ';
but this is 'c' like code and not considered 'good' C++ code.
Also note that this memory 'hack' for treating a 2d-array as a 1-d isn't valid for std::vector and std::array.