Accessing an element of an array of list of user-defined structure,gives Access Violation

P

PSN

Hallo Everyone,
I have been struggling with issue since 2 days. I hope soemone can
point me here in the right direction. I have the following situation:

struct stTemp
{
int a;
int b;
stTemp() {...}
stTemp(int _a, int _b) {...}
};

typedef std::list<stTemp> lstTemp;

class myClass
{
private:
int format;
int var;
lstTemp listarrays[10];

public:
myClass(int _a, int _b) : format(_a), var(_b) {}
 
P

PSN

More Info: I also tried re-modelling the myClass as in here, but with
no success:

class myClass
{
private:
int format;
int var;
lstTemp* listarrays[10];
public:
myClass(int _a, int _b) : format(_a), var(_b)
{
for (int i=0; i<10; i++)
{
lstTemp *plstTemp = new lstTemp;
listarrays = plstTemp;
}
}
 
P

Paul

PSN said:
Hallo Everyone,
I have been struggling with issue since 2 days. I hope soemone can
point me here in the right direction. I have the following situation:

struct stTemp
{
int a;
int b;
stTemp() {...}
stTemp(int _a, int _b) {...}
};

typedef std::list<stTemp> lstTemp;

class myClass
{
private:
int format;
int var;
lstTemp listarrays[10];

public:
myClass(int _a, int _b) : format(_a), var(_b) {}
.
.
std::list<stTemp> GetListElement(int index) { return
listarrays[index]; }
.
.
};


CreateListArray()
{
myClass *pObj = new myClass();

There is no default constructor in myClass that takes no arguments
<snip>
 
P

PSN

how about fetching the FAQ and reading the "how to post code" section?
I'm sorry. I looked every where for the FAQ. I couldnt find it. A link
would be helpful.

@Paul: It was a typo there. The line should read as below. Please note
that it is a proxy code of my implementation.
myClass *pObj = new myClass(1,2);

Thanks,
Prakash
 
K

Kevin McCarty

Hallo Everyone,
I have been struggling with issue since 2 days. I hope soemone can
point me here in the right direction. I have the following situation: [...]

typedef std::list<stTemp> lstTemp;

class myClass
{
   private:
       int format;
       int var;
       lstTemp listarrays[10];

   public:
       myClass(int _a, int _b) : format(_a), var(_b) {}
       .
       .
       std::list<stTemp> GetListElement(int index) { return
listarrays[index]; }

Why not reuse the lstTemp typedef for the return type of this
function?

I suspect that you may want this function to return by reference.
(and probably to add a const version that returns by const-reference)

Not enough info to tell if that will fix your problem. But unless you
have other, not-shown functions in myClass that modify the elements of
listarrays, it will not ever contain anything aside from ten empty
lists.

- Kevin
 
K

Kevin McCarty

More Info: I also tried re-modelling the myClass as in here, but with
no success:

This does almost exactly the same thing as your original
implementation (except that the elements of listarray are allocated on
the heap rather than the stack, and now you also need to define an
explicit copy ctor, assignment op, and dtor which are hopefully in the
code you do not show) so no surprise there.
class myClass
{
   private:
       int format;
       int var;
       lstTemp* listarrays[10];

Don't do this without a really good reason, it is not exception-safe
and it is non-trivial to do correctly
   public:
       myClass(int _a, int _b) : format(_a), var(_b)
       {
          for (int i=0; i<10; i++)
          {
             lstTemp *plstTemp = new lstTemp;
             listarrays = plstTemp;
          }
       }


Look up the Rule of Three:
http://en.wikipedia.org/wiki/Rule_of_three_(C++_programming)
       .
       .
       std::list<stTemp> GetListElement(int index) { return
*(listarrays[index]); }

This is still returning by value (i.e. by copy) rather than by
reference, which I still think may be (one of) the troubles.

- Kevin
 
P

Paul

how about fetching the FAQ and reading the "how to post code" section?
--I'm sorry. I looked every where for the FAQ. I couldnt find it. A link
--would be helpful.

--@Paul: It was a typo there. The line should read as below. Please note
--that it is a proxy code of my implementation.
--myClass *pObj = new myClass(1,2);


Each array element contains an empty list. Then you copy these empty lists
to a locallist object, which is a pointless excercise.
The code you have posted does not seem to replicate the error you have
described as present in your original code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top