C++ error!

Post Reply
Ehsanizadi
Posts: 72
Joined: Mon Dec 02, 2013 4:13 pm

C++ error!

Post by Ehsanizadi »

Hi all,

I understand that this error is C++-related, however, I think I get the answer sooner here.

I wrote a function which writes the positions of an array of rigid bodies (array of rigid bodies were generated by btAlignedObjectArray<btRigidBody*>):

Code: Select all

void beadPositionWriter(btRigidBody* rigidBody[], int NumberOfRigidBodies, int index){

	std::ofstream 	beadsPositions("beadsPositions/beadsPositions." + std::to_string(index) + ".csv");

	beadsPositions << "xCoord" << "," << "yCoord" << "," << "zCoord" << std::endl;

	for (int j =0; j < NumberOfRigidBodies; j++){
		beadsPositions << rigidBody[j]->getWorldTransform().getOrigin().getX() <<"," << rigidBody[j]->getWorldTransform().getOrigin().getY() << "," << rigidBody[j]->getWorldTransform().getOrigin().getZ() << std::endl;
	}
	beadsPositions.close();

}
When I use this function in my code like this:

Code: Select all

beadPositionWriter(metalBeadRB*, exactNumBead, time1);
I get this error on the first parameter I passed to the function in my code (here: beadPositionWriter(metalBeadRB*, exactNumBead, time1);) .:
Expected primary-expression before ‘,’ token
Does anyone know what is missing?

Thanks
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: C++ error!

Post by drleviathan »

I think the error is in the first argument to beadPositionWriter(). Specifically it should be btRigidBody* rigidBody rather than btRigidBody* rigidBody[].
Ehsanizadi
Posts: 72
Joined: Mon Dec 02, 2013 4:13 pm

Re: C++ error!

Post by Ehsanizadi »

But isn't it needed to tell my compiler that my btRIgidboy is an array ?
Ehsanizadi
Posts: 72
Joined: Mon Dec 02, 2013 4:13 pm

Re: C++ error!

Post by Ehsanizadi »

Even if I do so, in my function (removing the brackets), I get the same error:
directShearTest.cpp:308:37: error: expected primary-expression before ‘,’ token
beadPositionWriter(metalBeadRB*, exactNumBead, time1);
^
make: *** [bullet3] Error 1
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: C++ error!

Post by drleviathan »

No, you don't need the brackets. If you create an array variable then its type is a pointer. The following code produces two variables (foo and baz) that are morphologically identical. The only difference is that bar is allocated in the heap memory (it must be manually deleted later), and baz is allocated on the stack (it will be deleted automatically when it goes out of scope).

Code: Select all

Foo* bar = new Foo[10];
Foo baz[10];
Now your problem is a little clearer: your error is caused by the asterisk at the end of metalBead*. If metalBead is a pointer (or an array, same thing), then just pass it unadulterated by any punctuation in the call to beadPositionWriter() like this:

Code: Select all

beadPositionWriter(metalBeadRB, exactNumBead, time1);
BTW, your asterisk is on the wrong side of metalBead anyway -- if you were trying to dereference a pointer it would go in front.

Code: Select all

float bar; // bar is a float
float* foo = &bar; // foo is a pointer to float and happens to point at bar
*bar; // syntax error -- nonsensical attempt to get the thing that bar points to... but bar is not a pointer!

bar = *foo; // bar is set to the value of the thing to which foo points
bar = foo*; // syntax error... the dereference asterisk should go in front of the pointer
float** pointerToFoo = &foo; // pointerToFoo is a pointer to a pointer to a float (type float**)
Ehsanizadi
Posts: 72
Joined: Mon Dec 02, 2013 4:13 pm

Re: C++ error!

Post by Ehsanizadi »

Thanks,
But still my problem is not solved.

I defined my function like:

Code: Select all

void beadPositionWriter(btRigidBody* rigidBody, int NumberOfRigidBodies, int index){

	std::ofstream 	beadsPositions("beadsPositions/beadsPositions." + std::to_string(index) + ".csv");

	beadsPositions << "xCoord" << "," << "yCoord" << "," << "zCoord" << "," << "rotation(deg)" << std::endl;

	for (int j =0; j < NumberOfRigidBodies; j++){
		beadsPositions << rigidBody[j]->getWorldTransform().getOrigin().getX()<< std::endl;
	}
	beadsPositions.close();

}
and I used it in my code like:

Code: Select all

beadPositionWriter(metalBeadRB, exactNumBead, time1);

I get this error:

Code: Select all

In file included from directShearTest.cpp:8:0:
writers.h: In function ‘void beadPositionWriter(btRigidBody*, int, int)’:
writers.h:14:33: error: base operand of ‘->’ has non-pointer type ‘btRigidBody’
   beadsPositions << rigidBody[j]->getWorldTransform().getOrigin().getX()<< std::endl;
                                 ^
directShearTest.cpp: In function ‘int main()’:
directShearTest.cpp:319:57: error: cannot convert ‘btAlignedObjectArray<btRigidBody*>’ to ‘btRigidBody*’ for argument ‘1’ to ‘void beadPositionWriter(btRigidBody*, int, int)’
      beadPositionWriter(metalBeadRB, exactNumBead, time1);

It seems when I make rigid bodies by

Code: Select all

btAlignedObjectArray<btRigidBody*>
this error happens
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: C++ error!

Post by drleviathan »

That's right. **rigidBody** is an array (aka a btRigidBody pointer) but when you use rigidBody it automatically increments and dereferences the pointer. In other words these lines do the same thing:

Code: Select all

rigidBody[i];    // increment then get value
*(rigidBody + i);    // increment then get value
So these three line also do the same thing:

Code: Select all

rigidBody[i].getWorldTransform();    // increment and get value then call method
*(rigidBody + i).getWorldTransform();    // increment and get value then call method
(rigidBody + i)->getWorldTransform();    // increment then dereference pointer to call method
Ehsanizadi
Posts: 72
Joined: Mon Dec 02, 2013 4:13 pm

Re: C++ error!

Post by Ehsanizadi »

Thanks for your comments.

My problem solved
Post Reply