0

I have a std::list of "Ray"-objects, which I want to iterate over.

for (std::list<Ray>::const_iterator it = clRay.begin(), end = clRay.end(); it != end; ++it) { *it->setDir( m_v3Position - *it->getOrigin() ) } 

And the Ray objects have the methods setDir() and getOrigin():

const QVector3D &getOrigin() const { return m_v3Origin; } void setDir( const QVector3D &dir ) { m_v3Dir = dir; } 

The setdir() is no problem for the compiler, but I can't call getOrigin(). Because then I get the error:

src/AreaLight.cpp: In member function ‘virtual void AreaLight::shadowFeeler(std::__cxx11::list<Ray>&, double&, ColorType&) const’: src/AreaLight.cpp:18:17: error: no match for ‘operator*’ (operand type is ‘const QVector3D’) 

I think it is a const-problem, but I don't know how to fix it.

9
  • 1
    *it->setDir( m_v3Position - *it->getOrigin() ) -> it->setDir( m_v3Position - it->getOrigin() ) Commented May 4, 2017 at 23:03
  • 2
    Or for range: for (auto& r : clRay) { r.setDir( m_v3Position - r.getOrigin() ); } Commented May 4, 2017 at 23:05
  • 1
    if I use it->setDir( m_v3Position - it->getOrigin() ) I get the error src/AreaLight.cpp:18:47: error: passing ‘const Ray’ as ‘this’ argument discards qualifiers [-fpermissive] Commented May 4, 2017 at 23:10
  • 3
    If you keep to use iterator, you should not use const_iterator Commented May 4, 2017 at 23:16
  • 1
    When you code, you should understand what you do and not try random things. Obviously, you were using const_iterator and were calling setDir which is not a constant function which does not make much sense. Commented May 4, 2017 at 23:17

1 Answer 1

2

You wrote *it->. You should write just it->. There's only one level of indirection here.

This affects both calls.

In fact, the call to setDir is broken for another reason, which is that it is non-const and you are trying to get to it through a const_iterator. Adding a * doesn't fix that problem; it just masks it with a new problem. Randomly adding yet more *s, without understanding what they mean, isn't the way to go either.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.