Functions. Dynamic transmission to function. No matching function for call to



  • Hello!

    I just started studying C++, so I apologize in advance for stupid questions. So far, I'm trying to figure out how the bodies interact with functions.

    First, he wrote a simple software that reads as follows: first function dec2bin(number) translates the user-introduced 10 (from 0 to 255) into a double, records the result in a size 8 mass and returns it. The second function of printBiNum_size(biNum, size) turns this mass on screen.

    It was then interesting to perform a function that would lead to an unknown mass on the screen. On the Internet, a view was found using a template to guide it by writing its own printBiNum. But in an attempt to transfer the compilator's function, it makes a mistake:

    error: no matching function for call to 'printBiNum(int* trigger)'

    I understand that this is because I'm trying to hand over the dynamic mass, because if you transfer the species int ar[] = {1, 2, 3, 4, 5, 6} it works.

    Question: How do you need to change the function of printBiNum to transfer the dynamic body without giving it the size?

    Thank you so much for any help.

    main.cpp:

    #include <iostream>
    #include <cmath>
    #include "binfun.h"
    

    template <typename T, size_t N>

    void printBiNum(const T(&biNum)[N])
    {
    for (const auto i: biNum){
    std::cout << i << " ";
    }
    std::cout << std::endl;
    }

    template<class T, size_t N>

    void view(const T(&arr)[N], const char* name)
    {
    std::cout << name <<'[' << N << "] = {";
    for (const auto i: arr)
    std::cout << i << ", ";
    std::cout << " >;\n";
    }
    int main()
    {
    // Ask for entering a number between 0 and 255, read it
    int number = 193;
    //std::cout << "Please enter a number between 0 and 255: " << factorial(4);
    //int number;
    //std::cin >> number;

    // Convert number to a binary form
    int *myBiNum = new int[8];
    myBiNum = dec2bin(number);
    // Print binary number
    printBiNum_size(myBiNum, 8);
    int ar[] = {1, 2, 3, 4, 5, 6};
    printBiNum(ar);
    view(ar, "source array: ar");
    printBiNum(myBiNum);
    

    }

    binfun.cpp:

    #include <iostream>
    #include <cmath>

    int* dec2bin(int decNum)
    {
    int num_k = decNum;
    int *biNum = new int[8];

    for (int k = 0; k &lt;= 7; ++k){
        int pow2k = 128/pow(2,k);
        if (num_k &gt;= pow2k){
            biNum[k] = 1;
            num_k -= pow2k;
        }
        else{
            biNum[k] = 0;
        }
    }
    return biNum;
    delete [] biNum;
    

    }

    void printBiNum_size(int biNum[], int arrSize)
    {
    for (int k = 0; k < arrSize; ++k){
    std::cout << biNum[k] << " ";
    }
    std::cout << std::endl;
    }

    binfun.h:

    #ifndef BINFUN_H
    #define BINFUN_H

    int* dec2bin(int decNum);
    void printBiNum_size(int biNum[], int arrSize);

    #endif // BINFUN_H



  • You can use this function only if the size of the mass is known at the compilation stage. According to what you're writing, you're really aware of this size at the compilation stage, and the problem is the loss of the kind of masssiv when using the operator. new []

    Yes, operator. new[]Unfortunately, the original "mass" type will not be preserved as a result. This operator always returns the index to component The mass, not the whole body.

    So you're gonna have to squeeze out "massy" like this, or you're gonna have to get out of this or something.

     int (*myBiNum)[8] = (int (*)[8]) new int[8];
    

    for (unsigned i = 0; i < 8; ++i)
    (*myBiNum)[i] = i;

    printBiNum(*myBiNum);

    delete [] (int *) myBiNum;

    or by type

     int (*myBiNum)[8] = new int[1][8];

    for (unsigned i = 0; i < 8; ++i)
    (*myBiNum)[i] = i;

    printBiNum(*myBiNum);

    delete [] myBiNum;

    If you want to use a standard marshmallow instead of a rock, and the size is really known at the compilation stage, it will be more appropriate. std::arrayNot std::vector




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2