0
我正在編譯錯誤在constuctor設置的2D陣列類成員:初始化類的2D陣列構件在構造
#include <queue>
using namespace std;
#define N 11
struct Elem {
Elem(uint32_t row, uint32_t col)
: row_(row), col_(col)
{ }
uint32_t row_, col_;
};
class Mycomp {
public:
Mycomp(int arr[][N])
{
arr_ = arr;
}
bool operator() (const Elem &lhs, const Elem &rhs)
{
return arr_[lhs.row_][lhs.col_] > arr_[rhs.row_][rhs.col_];
}
int arr_[][N];
};
int *mergeKArrays(int arr[][N], int k)
{
Mycomp mycomp(arr);
priority_queue<Elem, vector<Elem>, Mycomp> pq(mycomp);
for (uint32_t i = 0; i < k; ++i) {
pq.push(Elem(i, 0));
}
return (int *) arr;
}
int main() { }
我得到以下錯誤:
./mergek.cc: In constructor ‘Mycomp::Mycomp(int (*)[11])’:
./mergek.cc:23:22: error: incompatible types in assignment of ‘int (*)[11]’ to ‘int [0][11]’
arr_ = arr;
^
我嘗試了不同的變化,例如「& arr_ [0] = arr;」不工作。
謝謝, 艾哈邁德。
要麼弄平陣列或使用'的std :: array' /'的std :: VECTOR'進行整流。否則,你只是爲了頭痛。 – NathanOliver
我被卡住了函數的原型 - mergeKArrays()。我可以創建一個新的2D std :: vector來將函數中的數據傳遞表示爲二維數組,以解決編譯問題。但是,首先嚐試看看我的語法是否正確。 –
零大小數組,'int arr _ [] [N];',在C++中是不允許的。 – user4581301