0

Not sure where I'm going wrong.. I have two classes like so:

class One
{
    public:
    vector<Object*> myObjects;
};

class Two
{
    public:
    vector<Object*> * pointertoObjects;
};

I then want to create a pointer from pointertoObjects to myObjects and am doing so like this:

pointertoObjects = &myObjects;

But when I try to access to pass through an element:

void doFunction(Object * object);
doFunction(pointertoObjects[i])

it gives me an error:

Error: no suitable conversion function from std::vector<Object *, std::allocator<Object *>>" to "Object *" exists

Where have I gone wrong?


PHP renaming of input file issues

I'm trying to rename the input file to be a .jpg after conversion, but for some reason I'm getting a file.png.jpg when I'm really looking for file.jpg

Here is my code:

$source = $path . $_POST['username']. "-" . $_FILES['t1']['name'];
$destination = $path . $_POST['username']. "-" . basename($_FILES['t1']['name']) . ".jpg";
4

4 回答 4

5
pointertoObjects[i]

这会将指针视为对象数组的起始地址vector,并为您i提供该数组的元素。(由于没有数组,只有一个向量,如果i不为零,您将获得未定义的行为)。

如果你想要i指针指向的向量的元素,那就是:

(*pointertoObjects)[i]

或使用范围检查并减少意外类型错误的范围:

pointertoObjects->at(i)

你应该问问自己是否真的需要这么多指针;他们可能会很困惑。

于 2013-05-28T16:17:14.677 回答
1

当您编写 时pointertoObjects[i],您正在取消引用pointertoObjects并且 C++ 的行为就像它是一个 的数组vector<Object*>,因此pointertoObjects[i]产生一个 (reference to) vector<Object*>

要解决这个问题:

(*pointertoObjects)[i]
于 2013-05-28T16:17:32.823 回答
0
vector<Object*> pointedObjects = *pointertoObjects;
Object* obj = pointedObjects[i]
doFunction(obj);
于 2013-05-28T16:18:17.303 回答
0

你有一个简单的类型不匹配。当您在 上使用数组索引时pointertoObjects,您将获得对 a 的引用vector<Object*>,而不是Object *所需的 by doFunction

你可能想要这样的东西: doFunction((*pointertoObjects)[i]);

于 2013-05-28T16:14:15.567 回答