Solved

Cannot modify QList when using pointer to QList

Posted on 2010-11-15
11
710 Views
Last Modified: 2012-05-10
Hi,
I've got a class with a number of QLists defined as follows:
 QList<Vertex> m_surfaceVertices;
 QList<Vertex> m_windowVertices;
 QList<Vertex> m_holeVertices;

Vertex is simply a class I've defined that has 3 public GLdouble members.

I have three functions that do processing on each of the QLists, they are called:
ProcessSurfaces, ProcessWindows, ProcessHoles and are identical apart from the fact I am using a different QList.

So I'd like to refactor those into a single function that takes an int representing whether m_surfaceVertices, m_windowVertices or m_holeVertices are to be processed.

The way I'm doing this is:

QList<Vertex>* theVertices = NULL;

then a switch statement which simple assigns theVertices with the address of the appropriate QList:

theVertices = &m_surfaceVertices

However, when I try to modify the x, y and z members of one of the Vertex is in the QList, I get compiling errors:

theVertices(i).x = 4;

throws up

Error      1      error C2039: 'x' : is not a member of 'QList<T>'

I've tried everything I can think of (casting, putting brackets in, using at(J) etc) and don't understand why I can't do this.

After all, I have a pointer to the QList and am accessing a Vertex so should be able to modify it?

Any help is greatly appreciated.

Thanks

Note: I had to replace square brackets in code above.
0
Comment
Question by:vedeveloper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 34135486
Did you mean this?

theVertices[i].x = 4;

Open in new window

Note, square not round brackets.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 34135496
Actually, it should probably be this...

(*theVertices)[i].x = 4;

Open in new window

0
 

Author Comment

by:vedeveloper
ID: 34135521
Ta. I had to put round brackets in place of square brackets when posting the message
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vedeveloper
ID: 34135553
Putting in your second changes lets the code compile but it crashes here.

QList<Vertex>::operator[](int i=0)  Line 447 + 0x18 bytes      C++

I'll check that it isn't anything else in that function that is causing the crash.

0
 

Author Comment

by:vedeveloper
ID: 34135559
By the way, it looks like that is an index out of range error.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34135632
>> By the way, it looks like that is an index out of range error.

Well the error itself means the correct operator is probably getting called now so I'd go out on a limb and say I think the original issue is resolved but, as you say, you probably now have a new your you've unravelled.
0
 

Author Comment

by:vedeveloper
ID: 34135653
Thanks.

Ahh, the crash occurs when calling theVertices.size() which is used in an assertion in the [] operator.

Any ideas?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34135827
We'll it sounds to be like it's asserting that the index value you are using is actually valid and within the correct range. You could prove this by using at() rather than operator[] since at will throw and exception you can catch if the index is invalid.

http://www.cplusplus.com/reference/stl/vector/at/

try
{
   (*theVertices)[i].x = 4;
}
catch(out_of_range const & e)
{
    // if we get here our index value is invalid
}

Open in new window

0
 

Author Comment

by:vedeveloper
ID: 34136053
As soon as I modify the vertex, it throws the whole QList off.

So to get round this I just created a copy of the vertex I wanted to modify and then replaced it with QList::replace call.

Thanks for your help, it put me on the right track.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34136059
You are very welcome.
0
 

Author Closing Comment

by:vedeveloper
ID: 34136062
The solution solved the initial problem in that I couldn't get the code to compile.

This lead to another problem I was able to fix myself.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question