我不明白为什么这个简单的代码不会编译!我所要做的就是创建一个指向Set类型对象的指针数组。
int main()
{
size_t n = 0;
size_t ops = 0;
cin >> n >> ops;
Set ** arr = new Set*[n+1];
for (size_t i = 1; i< n+1; ++i) {
Set s = new Set(i);
arr[i] = &s;
}
上面我有我的Set类型定义:
struct Set
{
Set (size_t x) {
this->p = x;
this->rank = 0;
this->index = x;
}
size_t index;
size_t p;
size_t rank;
};
但是当g ++不接受这个代码是正确的时,这就是它所说的:
dss.cpp: In function ‘int main()’:
dss.cpp:57:20: error: invalid conversion from ‘Set*’ to ‘size_t {aka long unsigned int}’ [-fpermissive]
Set s = new Set(i);
^
dss.cpp:17:2: error: initializing argument 1 of ‘Set::Set(size_t)’ [-fpermissive]
Set (size_t x) {
^
它在说什么?什么转换?卡车,但我看不到任何转换!!我做错了什么?
答案 0 :(得分:3)
Set s = new Set(i);
arr[i] = &s;
应该是
Set *s = new Set(i);
arr[i] = s;
或只是
arr[i] = new Set(i);
你可能想从i = 0