【发布时间】:2020-12-10 10:08:23
【问题描述】:
我想在 C++ 中创建一个名为“losowanie”的函数,这个函数应该在 0-n 之间随机 n 个数字。当我调用这个函数时,我得到:
error: incompatible types in assignment of ‘int’ to ‘int [n]’
这是我的代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int losowanie (int tab[], int n) {
for(int i = 0;i<n; i++) {
tab[i] = rand() % n + 1;
cout<<tab[i]<<endl;
}
}
int main()
{
int n = -1;
while(n<0 || n>50) {
cout<<"Give number ( 0 - 50)"<<endl;
cin>>n;
}
int tab[n];
tab = losowanie(tab, n); //here is error
return true;
}
【问题讨论】:
-
看起来
losowanie应该就地填充tab数组。你认为它应该返回什么? -
正如@BoBTFish 所说,
losowanie就地填充数组,只需删除对函数结果的 tab 变量的附加分配。losowanie(tab, n);同样,该函数应该返回一个 int (当前不返回任何内容),并且您的选项卡是 int 数组
标签: c++ variable-assignment function-declaration variable-length-array incompatibletypeerror