Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Constructor of a derived class
Forum Updated to NodeBB v4.3 + New Features

Constructor of a derived class

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Weichao Wang
    wrote on last edited by
    #1

    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:
    public:
    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!
    Weichao

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      4
      • SGaistS SGaist

        Hi,

        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.

        W Offline
        W Offline
        Weichao Wang
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Unh(constr QString& str)

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply
          3
          • SGaistS SGaist

            Unh(constr QString& str)

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

            W Offline
            W Offline
            Weichao Wang
            wrote on last edited by
            #5

            @SGaist
            Many thanks!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved