Trouble with vectors...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • InNeedOfHelp
    New Member
    • Jun 2006
    • 27

    Trouble with vectors...

    I'm having a problem with vectors. I have a vector of type class and that class also has a vector in it and I am having problems assigning values to the vectors, I am using VC++ ver6.0

    _______________ _________


    class X{
    public:
    vector<Y> yVect;
    }

    class Y{
    public:
    vector<Z> zVect;
    }

    class Z{
    public:
    double x;
    double y;
    }

    when I say something like:
    xVect.at(0).yVe ct.(0).x = 5;

    I get problems! Any help please. Thank you.
  • lottalava
    New Member
    • Jul 2006
    • 13

    #2
    You need to make some typecast!

    (( Z )((( Y )xVect.at( 0 )).yVect.( 0 ))).x = 5;

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      No those type casts will not help

      xVect.at(0).yVe ct.at(0).x = 5;

      You have not declared xVect in the posted source but I assume it is

      vector<X> xVect;

      In which case


      xVect.at(0) is of type X

      xVect.at(0).yVe ct.at(0) is of type Y

      xVect.at(0).yVe ct.at(0).x is an error since Y has no member x and would produce a compile error

      Originally posted by InNeedOfHelp
      I get problems!
      This is a singularly unuseful statement, if you want help then describe the problem properly.

      Problems at run time or compile time? If at compile time what where the compiler output errors and warnings? If at run time what was the input data what happen/was the output data?

      Comment

      • InNeedOfHelp
        New Member
        • Jun 2006
        • 27

        #4
        Sorry Banfa,

        that was a mistake it should have been yVect.at(0).zVe ct.at(0).x = 5; there is no xVect! The problem is at run time when I try the following statement it throws an exception...

        yVect.at(pos).z Vect.at(pos).x = 5;

        Comment

        • InNeedOfHelp
          New Member
          • Jun 2006
          • 27

          #5
          I will give the full code as it may be more descriptive.

          I have 3 different classes that are linked, MaterialClass, StressClass and tempDesClass. Each instance of the MaterialClass (stored in materialDatabas e vector) has numerous different stresses (represented by vector of StressClass called allowableStress ) and within each stress there are numerous tempDes properties (vector of tempDesClass called tempLife) as below;

          class MaterialClass : public CWnd
          {
          public:
          .......
          vector<StressCl ass> allowableStress ;
          };
          static vector<Material Class> materialDatabas e;

          class StressClass : public CWnd
          {
          public:
          ........
          vector<TempLife Class> tempLife;
          }

          class TempLifeClass : public CWnd
          {
          public:
          double temperature;
          double lifeTime;
          double tempLife;
          }

          The static vector materialDatabas e holds the materials, which should contain stresses which in turn should contain TempLifeClass variables. I try to add the stresses like so,

          void MaterialClass:: addStress(Stres sClass stress){
          materialDatabas e.at(i).allowab leStress.at(0) = stress;
          }

          but this gives an exception - in the debugger stress contains the values it should but hWnd=0x00000000 for both StressClass and CWnd which I think may be the problem? Not sure how to solve it though.
          Any help would be greatly appreciated,
          Thankx

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            OK this

            Code:
            void MaterialClass::addStress(StressClass stress){
            materialDatabase.at(i).allowableStress.at(0) = stress;
            }
            Looks a little strange to me. Where did i (not me I know where I came from :-) ) come from. Also although this is a member function of MaterialClass you do not access any of the class data, you access an external valiable materialDatabas e. And finally you are passing stress by value but stress could be very big since it contains a vector.

            Something like this would be more usual/better

            Code:
            void MaterialClass::addStress(StressClass &stress){
                allowableStress.at(0) = stress;
            }
            
            ...
            
            materialDatabase.at(i).addStress(stress);
            
            ...
            Why are you subclassing all your classes from CWnd, does your program really have a window for every instance of all these classes?


            I will leave it there, I think this is going to be a long haul program.

            Comment

            • InNeedOfHelp
              New Member
              • Jun 2006
              • 27

              #7
              Banfa,
              I made the changes you suggested however an exception is still thrown at the line;

              allowableStress .at(0) = stress;

              before this line is executed the values of allowable stress are (from debugger):

              allowableStress .allocator._Fir st = 0x00000000 {StressClass hWnd = ???}
              Could this be causing the problem?

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by InNeedOfHelp
                Banfa,
                I made the changes you suggested however an exception is still thrown at the line;

                allowableStress .at(0) = stress;

                before this line is executed the values of allowable stress are (from debugger):

                allowableStress .allocator._Fir st = 0x00000000 {StressClass hWnd = ???}
                Could this be causing the problem?
                Hmmm I didn't think that change would cure your problem, it is just a better way of doing it.

                You could be right about the cause of the problem, taht is way I asked why you were subclass everything from CWnd.

                Comment

                • InNeedOfHelp
                  New Member
                  • Jun 2006
                  • 27

                  #9
                  Sorry,
                  I have made my classes subclasses of generic CWnd through the class wizard - I thought that was what should be done if your class didn't require any specialised functionality (from classes like CDialog etc!?!) I am new to MFC and don't really understand it, am I totally wrong in doing that?

                  Comment

                  • npchoubey
                    New Member
                    • Jul 2006
                    • 4

                    #10
                    Here is something that works, Not sure if it solves ur problem.

                    #include <iostream.h>
                    #include <vector.h>
                    class Z
                    {
                    public:
                    double x;
                    double y;
                    };

                    class Y
                    {
                    public:
                    vector<Z> zVect;
                    };

                    class X
                    {
                    public:
                    vector<Y> yVect;
                    };

                    int main()
                    {

                    X xVect;
                    xVect.yVect.at( 0).zVect.at(0). x = 5;
                    cout<<" done "<<endl;
                    return 0;
                    }

                    Comment

                    Working...