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.
9 1572
You need to make some typecast! (( Z )((( Y )xVect.at( 0 )).yVect.( 0 ))).x = 5; 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?
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;
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
Banfa 9,065
Recognized Expert Moderator Expert
OK this -
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 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 -
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.
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?
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.
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?
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;
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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 " <<...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |