Question:
Accessing a vector in c++ in a member function?
mike
2012-10-29 10:03:20 UTC
I have a member function and a public (for now...) data member.
The data member is a vector.
I want to access the values inside the vector from the member function but I keep getting an error saying :

Unhandled exception at 0x002833b9 in classplay.exe: 0xC0000005: Access violation reading location 0x0000000c.

size_type size() const
{ // return length of sequence
return (_Mylast - _Myfirst);
}
I want to use pointers but it keeps failing I've tried everything. Any help would be appreciated! Many thanks.
Three answers:
David
2012-10-29 10:12:25 UTC
[Update]



"Class" should be lowercase. There should be a semi-colon after the ending bracket to the class. And main should be "int", not "void".



Also, make sure you #include



Problems:



• pA is a pointer, so you use the -> operator to access methods. So change:



    pA.SetData();

    pA.SetData();



to



    pA->SetData();

    pA->SetData();



• Next is here in your function implementation of A::GetData():



double A::GetData() {

    double input = &Data[0];

}



&Data[0] is a pointer. So you need to input to be a pointer:



    double *input = &Data[0];



And there you have it. See the full working code here: http://ideone.com/lqCKws
2017-02-24 22:39:40 UTC
a million. Your remark says "loop numData circumstances" however the loop that follows loops a hundred thousand circumstances. for (int i=0; i> thisname >> thisnum; names.push_back(thisname); num.push_back(thisnum); 3. proceed by utilising writing a for loop that steps with the aid of each and each index of the names vector, and if names[i] == member then you incredibly've got here across the checklist. The call is in names[i] (yet you already knew that) and the type is in num[i].
tbshmkr
2012-10-29 10:05:40 UTC
Post more code.

=

We will help you get it right.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...