O
O
olkhovich2019-07-29 21:34:01
C++ / C#
olkhovich, 2019-07-29 21:34:01

Why does everything compile successfully in codeblocks, but gives an error in visual studio?

Simple job on function templates
In visual studio 2019, it underlines the last 4 lines and throws an error
What's the problem?

#include<iostream>

using namespace std;

template <typename Data>
void print(Data * a, int size)
{
    for(int i=0; i<size; i++)
    cout << a[i] << " ";
    cout << endl;
}
template <typename Data>
void swap(Data * a,Data * b)
{
    Data temp;
    temp=a;
    a=b;
    b=temp;
}
template<class Data>
void insertSort(Data *arr, long size)
{
    Data temp;
    long i, j;
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = 0; j < size - i - 1; j++)
        {
            if (arr[j] < 0)
                if (arr[j] < arr[j + 1])
                {
                        // меняем элементы местами
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                }
                if (arr[j] > 0)
                    if (arr[j] > arr[j + 1])
                    {
                        // меняем элементы местами
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
        }
        }
    }


int main()
{
    int a=10;
    float b[a]={2,1,4,5,3,-3,-1,-2,-4,-5};
    print(b,a);
    insertSort(b,a);
    print(b,a);
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-07-29
@olkhovich

does it give an error in visual studio?

Variable-length arrays are not supported.
const int a = 10;
or use std::array or std::vector

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question