Hi guys,

I'm trying to use a function which returns a pointer to a vector element, but it doesn't really work - and since I'm not so experienced in C++ and especially pointers, hoped that someone could help me out.

The function is defined as

Obj* MyClass::FindObj(std::vector<Obj*>&, string Label_){

return itObj*;

The function is supposed to find the object in the object vector and return a pointer to this element. So I call the function as

MyClass::Obj Object* = TestLesen.FindObj(std::vector<Obj*>&, NameObj_);

This (and multiple variations of this) return a number of errors, started with Object: undeclared identifier.

I suppose it's an error in reasoning I'm having problems with .. Does anyone have a clue?

Thanks in advance

6 Years
Discussion Span
Last Post by jonsca

On line 5 there, you have the dereferencing operator ('*') on the wrong side. Presumably that line is dereferencing an iterator that's not listed...

In the second listing, you need to pass the name of your std::vector<Obj*> into your method (in the same way that you've passed the method "NameObj_" instead of "string").

Edited by jonsca: n/a


Oh, of course ..

But I think my problems lies deeper, here the code for the function:

Obj* MyClass::FindObj(std::vector<Obj*>&, string NameObj_){

	std::vector<Obj*>::iterator itOb = ObjVec.begin();

	if (!ObjVec.empty()){
		itOb = find_if(ObjVec.begin(), ObjVec.end(), ParserDVG::FindNameObj(NameObj_));	
	return *itObj;

The function I'm trying to call then with a new dummy object:

ObjectTest = new MyClass(var, var);
std::vector<Obj*>::iterator itObj;
itObj = ClassObjAnother.FindObj(ObjectTest*, ObjName_);

I don't understand how can I bind std::vector<Obj*>& in the search function itself, so I a pointer at an element is returned. Furthermore, I mess up the types for the initialization - I shouldn't be able to say, iterator = function which returns a pointer at an object - but how do I modify the element then?

If someone bothers to answer, please do not only point at the errors, but please please explain a bit why it is the way it is - at this very moment my head is totally messed up and needs some logic :)

Edited by BimBam: n/a


In your bottom listing, you don't need another iterator in the calling function, as ClassObjAnother.FindObj() will return an item of Obj*. In your method (first listing), you are returning a dereferenced iterator, which is the object contained in the vector at that location. If you want an iterator, have you method return an iterator instead.

You don't need the * after ObjectTest in any case, that is not correct notation, but your method is looking for a vector of such objects anyway.


Ok, I corrected my (well, more your) code. And I got what you wrote, it was pretty well explained - since a pointer to the object of type Obj is returned there's no need for the '*' - but without the '*' after ObjectTest (following code)

MyClass ObjTest(var, var);
Attr1 = ClassObjAnother.FindObj(ObjectTest, ObjName_)->GetAttr1();

The error "cannot convert parameter 1 from MyClass to std::vector<_Ty> &" is dumped. Why is that? Besides, is the access to the Get-method correct?

Thanks a lot :)


I think you'll want something like this:

MyClass * ObjTest1 = new MyClass(var,var1);
MyClass * ObjTest2 = new MyClass(var2,var3);
std::vector<MyClass*> vecTest; //your method requires a vector
Attr1 = ClassObjAnother.FindObj(vecTest,ObjName_)->GetAttr1();

. and -> are of the same level of precedence, so that statement should do what you want it to, since it proceeds left to right. If you want to clarify your intentions you can put the parens around the first part for readability.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.