Results 1 to 4 of 4

Thread: C++ compile error, inheritance related (I think)

  1. #1
    Join Date
    Aug 2006
    Beans
    128
    Distro
    Ubuntu Development Release

    C++ compile error, inheritance related (I think)

    I'm having trouble fixing a compile error I'm getting. I'm using the Qt library, and in my project I've subclassed QVector2D with my own Vector class. Another class I have, Foo, has this constructor:

    Code:
    Foo(const Vector &, qreal, Bar*)
    The error comes when I try to instantiate one of them like this:

    Code:
    foo = new Foo(Vector(1, 0.3).normalized(), 1, this);
    I get the error

    Code:
    no matching function for call to ‘Foo::Foo(QVector2D, int, Bar* const)’
    candidates are: Foo::Foo(Vector, qreal, Bar*)
    Foo::Foo(const Foo&)
    The normalized() function returns a QVector2D, but since Vector inherits QVector2D, shouldn't it work? Is there some type of copy constructor I need to write? I also notice that it looks like I'm trying to pass a "Bar* const" to the constructor, but it takes "Bar*". I don't think this is the source of the error, since I'd expect a "discards qualifiers" error if it was.
    Dell XPS 410, Core 2 Duo 1.86 GHz, 2 Gigs RAM, NVIDIA Geforce 8800 GTS

  2. #2
    Join Date
    Jul 2008
    Beans
    105
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C++ compile error, inheritance related (I think)

    If you have to classes Base and Derived and Derived inherits from Base then a function that accepts a parameter of type Base will accept Deriver, not the other way around.
    Trying to help ... I hope it works ...
    Click here for my linux site
    Click here for some flash games

  3. #3
    Join Date
    Nov 2009
    Beans
    1,081

    Re: C++ compile error, inheritance related (I think)

    What is the 'greal' type, and have you tried casting to it?

  4. #4
    Join Date
    Apr 2008
    Beans
    101

    Re: C++ compile error, inheritance related (I think)

    Quote Originally Posted by Some Penguin View Post
    What is the 'greal' type, and have you tried casting to it?
    "Typedef for double on all platforms except for those using CPUs with ARM architectures. On ARM-based platforms, qreal is a typedef for float for performance reasons." according to the Qt documentation.

    Anyway, what GreenN00b said. You can't pass a base class where a derived one is expected (what if the called function wants to use some derived functionality that the base doesn't implement?) You'll need to either change the Foo constructor to take a QVector2D, or construct a Vector of your own to pass it.

    You also seem to have a const problem. If the Foo constructor doesn't need the Bar passed it to be mutable, you can just make it take it as const. Otherwise, you'll have to get rid of the const-ness. (Not by using const_cast.)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •