0
votes

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;
}

Error Image

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.

2
You have 2 different signatures for apply_all() between your declaration at the top and implementation at the bottom. const int* is different from int*drescherjm
You can have an int* in the declaration parameter, and an int* const in the definition, and that will be okay. But you can't have int* in the declaration parameter and const int* in the definition parameter. Those don't fit together.Eljay

2 Answers

3
votes

The function signature of declaration vs. definition are different:

Declaration:

int *apply_all(int *array1, size_t size1, int *array2, size_t size2);

Definition:

int *apply_all(const int *const array1, size_t size1, const int *const array2, size_t size2)

Both of them must be matched. It's up to you to choose which one is more helpful for you.

1
votes

The signature seems wrong. const int * in the function definition and int* in the function declaration. apply_all is the name of the function.