just wondering how you can scroll through class object? Ex:
class obj
{
double objid;
}
obj one, two, three;
one.objid = 0;
two.objid = 1;
three.objid = 2;
how would I see all obj id's? something like a for loop that will show 0 1 2.
just wondering how you can scroll through class object? Ex:
class obj
{
double objid;
}
obj one, two, three;
one.objid = 0;
two.objid = 1;
three.objid = 2;
how would I see all obj id's? something like a for loop that will show 0 1 2.
You'll need to put the obj's into an array. For example:
The assignment of the id's can be put into a loop, as I'm sure you know how to do; the displaying of all object id's can also be put into a loop, in very much the same way. I'll leave it to you to figure out exactly how the loop goesCode:obj objects[3]; objects[0].objid = 0; objects[1].objid = 1; objects[2].objid = 2;
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Code:vector<obj> myClasses; myClasses.push_back(one); myClasses.push_back(two); myClasses.push_back(three); for(int i=0; i<myClasses.size(); i++) myClasses[i].objid = i;
Okay, I am really confusing myself . I cant even think of what to tell you the problem is. um give me a sec.......
First:
objSteelBall' : undeclared identifier
Second:
This wont work because of the things i need right? The different variables that need to be plugged in.
/*
Objects[i].objName = "Glass Cup";
Objects[i].objID = 1;
Objects[i].objMass = 5;
Objects[i].objWeight = 4;
Objects[i].objDensity = 5;
Objects[i].objRoll = 0;
*/
Code:#include <math.h> #include <vector> #include <iostream> #include <string> using namespace std; const double gravity = 9.8; //Object Class class obj { public: string objName; double objID[2]; double objMass[2]; double objWeight[2]; double objDensity[2]; bool objRoll[2]; }; int main() { vector<obj> Objects; Objects.push_back(objSteelBall); Objects.push_back(objGlassCup); for(int i=0; i<myClasses.size(); i++) { Objects[i].objName = "Steel Ball"; Objects[i].objID = 0; Objects[i].objMass = 5; Objects[i].objWeight = 10; Objects[i].objDensity = 100; Objects[i].objRoll = 1; /* Objects[i].objName = "Glass Cup"; Objects[i].objID = 1; Objects[i].objMass = 5; Objects[i].objWeight = 4; Objects[i].objDensity = 5; Objects[i].objRoll = 0; */ } for(int j=0; j<Objects.size(); j++) { cout << Objects[j].objName << endl; cout << Objects[j].objID << endl; cout << Objects[j].objMass << endl; cout << Objects[j].objWeight << endl; cout << Objects[j].objDensity << endl; cout << Objects[j].objRoll << endl; } int poop; cin >> poop; return 0; }//end main()
Last edited by Bitphire; 01-29-2005 at 06:28 PM.
It's because objSteelBall doesn't exist; you'll either have to create it or forego the name:
Code:vector<obj> Objects; obj objSteelBall; //Create it objSteelBall.objName = "Steel Ball"; //... Objects.push_back(objSteelBall); //Add a copy of it to the vector. Usually bad idea.* //or vector<obj> Objects; Objects.push_back(obj()); //Create an obj and add a copy of it to the vector. Objects[0].objName = "Steel Ball"; //...
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
It's because objSteelBall doesn't exist; you'll either have to create it or forego the name:
Code:vector<obj> Objects; obj objSteelBall; //Create it objSteelBall.objName = "Steel Ball"; //... Objects.push_back(objSteelBall); //Add a copy of it to the vector. Usually bad idea.* //or vector<obj> Objects; Objects.push_back(obj()); //Create an obj and add a copy of it to the vector. Objects[0].objName = "Steel Ball"; //...
You say coping into a vector is bad idea, why? Also the second option Objects.push_back(obj()); How does this work and it sounds like the same as first option just using a function somehow? Is this still a bad idea?
The problem with the first idea is that the object gets copied into the vector, and you run the risk of forgetting that the object in the vector is completely different from the object called objSteelBall. As such, you're likely to end up modifying objSteelBall and then wondering why your code that uses the object stored in the vector doesn't work.
However, if you simply create objSteelball temporarily for the sake of easier understanding of what you're doing, but you never intend to use it again after you've added it to the vector (well you can, but it won't do anything to what's in the vector), then by all means use method (1). But if you think you might forget about the classic "this is just a copy" gotcha, then you should probably avoid creating a separately named object called objSteelBall as an intermediate. There are ways around this that allow you to have both (such as pointers, there's a tutorial about them on this site if you don't know how to use them yet), but it's a little confusing and you have to worry about things going out of scope and whatnot, so it's usually safer just to stick to method (2).
>>How does this work and it sounds like the same as first option just using a function somehow?
Yes, it's similar in that you create an 'obj' object and store a copy of it in the vector; but in this case, what obj() does is simply call obj's constructor, therefore creating an 'obj' object that doesn't have a name. This object is then copied into the vector, and the copy that isn't stored in the vector is destroyed immediately, so you never even really touch it - and therefore you don't have to worry about modifying it instead of the object in the vector by accident. You then directly modify the object stored in the vector, by accessing it through the vector's [] operator (it works sort of like an array).
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Oops, just saw that you edited your post:
>>This wont work because of the things i need right? The different variables that need to be plugged in.
No, it won't work, because the values are hardcoded - you can't calculate them from some set formula (like, how do you create "Glass Cup" from the value 1?). Well, you could store the values in arrays and then copy the array values into the structures.. but that totally defeats the purpose in the first place. Alternately, you could save the values in a text file and then read the file into the objects using a loop - and that also makes the program "user-configurable", because even after the program is compiled and made into a .exe, you can modify its behaviour just by changing the textfile that contains the settings. Of course, this introduces security vulnerabilities etc. etc. *puke*, but if you're just doing this for learning purposes then you might want to try it out
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Thanks for all the reply's. This is what I have and I am pretty happy with it . It works and so far its what I need. Now if anyone wants to help figure out how to find or set coords for x,y, and maybe z that would be great. Or how to find the overrall mass of an object...you know of the top of your head. I am about to google for it
Code:#include <math.h> #include <vector> #include <iostream> #include <string> using namespace std; const double gravity = 9.8; //Forward Declaration class mat; //Object Class class obj { public: string objName; double objMass; double x,y,z; mat* objMaterial; }; //Material Class class mat { public: double matWeight; double matDensity; }; int main() { /////////////////////////////////// //Define All Variables /////////////////////////////////// //Object Vector Variables vector<obj> objList; obj steelBall, glassCup; objList.push_back(steelBall); objList.push_back(glassCup); //Material Variables mat matSteel, matGlass; /////////////////////////////////// //Define All Materials /////////////////////////////////// //Steel matSteel.matWeight = 10; matSteel.matDensity = 70; //Glass matGlass.matWeight = 4; matGlass.matDensity = 10; /////////////////////////////////// //Define All Objects /////////////////////////////////// //Steel Ball objList[0].objName = "Steel Ball"; objList[0].objMaterial = &matSteel; objList[0].objMass = 5; //Glass Cup objList[1].objName = "Glass Cup"; objList[1].objMaterial = &matGlass; objList[1].objMass = 5; cout << "Object Name: " << objList[0].objName << endl << "Object Density: " << objList[0].objMaterial->matDensity << endl << "Object Mass: "<< objList[0].objMass << endl << "Object Weight:" << objList[0].objMaterial->matWeight << endl; int poop; cin >> poop; return 0; }//end main()
>>how to find or set coords for x,y, and maybe z
You mean like this?
>>#include <math.h>Code:objMyObjObject.x = 5; cout << objMyObjObject.x << endl;
-> #include <cmath>
>>cin >> poop;
An easier method is:
cin.get();
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.