Vector as a class member
Oct 27, 2013 at 11:31am UTC
I am writing the followinf class to store marks in a assignment. Using a vector of floats.
I keep on running into errors like not of class type while compiling this class what to do??
Also i would like to do the follwing in the main program
vector<assignment> a(5);
a[0].marksvec.push_back(10.5);
will this be fine or i will again run into errors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class assignment
{
public :
vector<float > marksvec();
float weightage;
int totalmarks;
string name;
float mean()
{
float x=0;
for (int i=0; i<marksvec.size(); i++)
{
x=x+marksvec[i];
}
return x/marksvec.size();
}
float sd()
{
float x=0,y=mean();
for (int i=0; i<marksvec.size(); i++)
{
x=x+(marksvec[i]-y)*(marksvec[i]-y);
}
return sqrt(x/marksvec.size());
}
};
Last edited on Oct 27, 2013 at 11:31am UTC
Oct 27, 2013 at 12:23pm UTC
Hi there,
Could you please share:
- Your full code
- Any compiler errors you are getting
That would help us to help you.
Thanks,
NwN
Oct 27, 2013 at 12:33pm UTC
Firstly,
vector<float > marksvec();
The above statement does not declare a member variable but a function that returns a vector of floats.
Instead change it to
vector<float > marksvec;
Secondly,
1 2
vector<assignment> a(5);
a[0].marksvec.push_back(10.5);
seems perfectly fine to me.
However, as NwN suggested better advice can be given if you provide more information.
Last edited on Oct 27, 2013 at 12:34pm UTC
Topic archived. No new replies allowed.