Class related problem

This is a discussion on Class related problem within the C++ Programming forums, part of the General Programming Boards category; I have an interesting problem. I'll word this the best I can. Ok... I have a class. Lets just say ...

  1. #1
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40

    Question Class related problem

    I have an interesting problem. I'll word this the best I can.


    Ok... I have a class. Lets just say its called CPart. CPart has 3 variable members: M1, M2, M3. There are 5 CPart objects. We need a way to go through all 5 objects and compare M1 to a global variable - lets just call it I.

    Here is an example that I tried that didn't work. Apparently, you can't create and array of user-defined objects.
    Code:
    int I = 10;
    void setVariables();
    int numberOfParts;
    
    class CPart{
    public:
    
    int M1;
    int M2;
    int M3;
    
    };
    
    CPart p1
    Cpart p2
    Cpart p3
    Cpart p4
    Cpart p5
    numberOfParts = 5;
    
    void setVariables(){
    
    (lets just pretend all three variables for each of the five objects is set to 5 in this function)
    
    }
    
    setVariables();
    
    /*this is the part that I think is the problem*/
    CPart listOfParts[numberOfParts];
    
    listOfParts[1] = p1;
    /*...and so on thru p5*/
    
    for (i=0; i<numberOfParts; i++){
    
        int i = 1;
    
        if (listOfParts[i] >= I){
    
            /* DO Something */
    
        }
    
    i++
    
    }
    I hope you can understand my code. I really need some help. (I know I made this post before but I don't think I made it understandable so I decided to make another try).

    Thanks a ton!
    Last edited by Gnoober; 10-07-2002 at 10:14 PM.
    - Grady (:

  2. #2
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    First off, in your for loop you set i to 1, that means that each time it will become 1, add one at end to make 2, but become 1 again at the beginning, resulting i an infinite loop.

    When using a for loop you should try not to do any assignments to i, or whatever iterator is called. The for loop takes care of this.

    If you incrment i yourself at the end, then the for loop will increment it again, meaning every loop increases it by 2 instead of just 1.

    Second, in order to compare your global variable to your class instance, you can only use the == or != operators. >, <, etc operators would have to be written that handle each data member and set up rules.



    Your class has an M1, M2, and M3, if you compared a CPart to another CPart with the > you'd have to make an > operator function to define whether all the parts have to be > in order for the entire thing to come up true.



    PHP Code:
    #include <iostream>
    using namespace std;

    class 
    triangle
    {
    public:
        
    int side1;
        
    int side2;
        
    int side3;
    };



    int main()
    {
        
    triangle yourGlobalTriangle;//this is the global you spoke of

        
    yourGlobalTriangle.side1=5;//it has 3 parts, intialize each part
        
    yourGlobalTriangle.side2=4;
        
    yourGlobalTriangle.side3=3;



        
    triangle bunchOfTriangles[5];//an array of 5 triangles, each triangle has 3 sides, so you have a total of 15 numbers stored here
        
        
    for(int index=0;index<5;index++)//once done with all five triangles then break
        
    {
            
    cout<<"Please enter the length of each side for triangle number "<<(index+1)<<", hit enter after each length:\n";
            
    cin>>bunchOfTriangles[index].side1;//use the . operator to access the data member
            
    cin>>bunchOfTriangles[index].side2;//use the . operator to access the data member
            
    cin>>bunchOfTriangles[index].side3;//use the . operator to access the data member
        
    }

        for(
    index=0;index<5;index++)
        {
            if( (
    bunchOfTriangles[index].side1<=yourGlobalTriangle.side1) && (bunchOfTriangles[index].side2<=yourGlobalTriangle.side2) && (bunchOfTriangles[index].side3<=yourGlobalTriangle.side3) )
                
    cout<<"All sides in triangle number "<<index+1<<", are less than corresponding sides of yourGlobalTriangle\n";
        }
        return 
    0;

    I compared each side of the global triangle and the curent indexed triangle. Only if global.side1<indexed[#]side1 &&(and) side2 &&(and) side3 are all true then it does whatever.

    You can't compare a triangle to a triangle, or a Cpart to a Cpart using < because they are user defined classes.
    In order to do that you'd have to write your own < operator for the class.

    That way you the programmer get to setup the rules. You may decide that only if the sum of the parts are less than the sum of the parts of the other one then it comes up true.

    Or only one of the parts has to be less than, out of all of them. You would do it just like I did except you would use ||(or) instead of the &&(and) operator.

    However, before you try to write an operator function try writing regular member functions. Once you have mastered them then you can move on to operator functions.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  3. #3
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    WOW! You sure did put me in my place. I see the problem with the "i". That was just a stupid mistake I made. The rest is all helpful! Thanks so much!!!
    - Grady (:

  4. #4
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I kinda thought it mighta been a mistake, you kinda did it like a while loop. Sometimes I write while loops like that and realize I shoulda just done a for loop.

    FYI, For loops have a couple bugs in VC++.

    For example you notice in my second for loop I don't put an int in front of it, this is because the i still exist. Really it should disapear after the first for loop requiring me to recreate it, but due to a bug in VC++ it's still there so all I have to do is set it to 0 again.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. problem with class
    By Mr.Pink in forum C++ Programming
    Replies: 26
    Last Post: 07-10-2005, 11:24 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  4. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 04:27 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21