473,511 Members | 14,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with vectors...

27 New Member
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).yVect.(0).x = 5;

I get problems! Any help please. Thank you.
Jul 26 '06 #1
9 1572
lottalava
13 New Member
You need to make some typecast!

(( Z )((( Y )xVect.at( 0 )).yVect.( 0 ))).x = 5;
Jul 26 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
No those type casts will not help

xVect.at(0).yVect.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).yVect.at(0) is of type Y

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

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?
Jul 26 '06 #3
InNeedOfHelp
27 New Member
Sorry Banfa,

that was a mistake it should have been yVect.at(0).zVect.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).zVect.at(pos).x = 5;
Jul 27 '06 #4
InNeedOfHelp
27 New Member
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 materialDatabase 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<StressClass> allowableStress;
};
static vector<MaterialClass> materialDatabase;

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

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

The static vector materialDatabase 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(StressClass stress){
materialDatabase.at(i).allowableStress.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
Jul 27 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
OK this

Expand|Select|Wrap|Line Numbers
  1. void MaterialClass::addStress(StressClass stress){
  2. materialDatabase.at(i).allowableStress.at(0) = stress;
  3. }
  4.  
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 materialDatabase. 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

Expand|Select|Wrap|Line Numbers
  1. void MaterialClass::addStress(StressClass &stress){
  2.     allowableStress.at(0) = stress;
  3. }
  4.  
  5. ...
  6.  
  7. materialDatabase.at(i).addStress(stress);
  8.  
  9. ...
  10.  
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.
Jul 27 '06 #6
InNeedOfHelp
27 New Member
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._First = 0x00000000 {StressClass hWnd = ???}
Could this be causing the problem?
Jul 27 '06 #7
Banfa
9,065 Recognized Expert Moderator Expert
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._First = 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.
Jul 27 '06 #8
InNeedOfHelp
27 New Member
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?
Jul 27 '06 #9
npchoubey
4 New Member
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;
}
Jul 27 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3396
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
2306
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
3228
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
2002
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
18096
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
8664
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
2038
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
3386
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
9
2037
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.