Hello...... i have a doubt,
cpp file
#include <iostream>
#include <vector>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int *apply_all(int *array1, size_t size1, int *array2, size_t size2);
void print(const int *const int_ptr, size_t size);
int main()
{
int array1[]{1, 2, 3, 4, 5};
int array2[]{10, 20, 30};
int *int_ptr1{nullptr};
int *int_ptr2{nullptr};
int_ptr1 = array1;
int_ptr2 = array2;
const size_t ArrSize1{5};
const size_t ArrSize2{3};
constexpr size_t size = ArrSize1 * ArrSize2;
int *int_ptr3 = apply_all(int_ptr1, ArrSize1, int_ptr2, ArrSize2);
cout << "Array 1 contains : ";
print(int_ptr1, ArrSize1);
cout << "Array 2 contains : ";
print(int_ptr2, ArrSize2);
cout << "And Array 3 contains : ";
print(int_ptr3, size);
cout << endl;
delete[] int_ptr3;
return 0;
}
void print(const int *const int_ptr, size_t size)
{
for (size_t i{0}; i < size; i++)
cout << *(int_ptr + i) << " ";
cout << endl;
}
int *apply_all(const int *const array1, size_t size1, const int *const array2, size_t size2)
{
size_t size = size1 * size2;
int *int_ptr{nullptr};
int_ptr = new int[size];
size_t position{0};
for (size_t i{0}; i < size2; ++i)
for (size_t j{0}; j < size1; ++j)
{
int_ptr[position] = array1[j] * array2[i];
++position;
}
return int_ptr;
}
Can you tell me what's the problem in this solution ?, When i build the program it says "undefined reference to 'apply_all(int*, unsigned int ,int*, unsigned int ) collect2.exe : error Id returned 1 exit status" it seems like a linker error.
const int*
is different fromint*
– drescherjmint*
in the declaration parameter, and anint* const
in the definition, and that will be okay. But you can't haveint*
in the declaration parameter andconst int*
in the definition parameter. Those don't fit together. – Eljay