Constructor of a derived class

  • Dear all,

    I've defined a class Unh derived from Segment. The base class has a constructor Segment(QString str), and the derived class has a constructor Unh(QString str):
    In the header file:
    Unh(QString str);
    In the source file:
    Unh::Unh(QString str):Segment(QString str) { ... }
    I get the following error for the line in the source file while compiling:
    expected primary-expression before 'str'
    How can this error be got rid of? What is primary-expression anyway? Thanks for any hint!

  • Lifetime Qt Champion


    It's : Segment(str). That's a call to the base class constructor, so you should pass it the variables it needs.

    On a side note, you should rather use const references when passing QString around like that, this will minimise the number of copies.

  • @SGaist
    Thank you! This is the reason. I've ignored it.
    Where should I insert const to do that as you suggested? Thanks!

  • Lifetime Qt Champion

    Unh(constr QString& str)

    By the way, you should make that constructor explicit, otherwise you might encounter funky stuff due to implicit conversion.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.