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. abstract class and virtual function
Forum Updated to NodeBB v4.3 + New Features

abstract class and virtual function

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 4 Posters 2.6k Views 3 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 koahnig
    #1

    Dear all,
    in a header file I've defined an abstract class (SegmentGroup) and a concrete class (Sgz) dirived from the former:

    class SegmentGroup {
    protected:
        QVector<QString> segments;
    public:
        void appendSegment(QString);
        virtual void assignSegment() = 0;
        virtual QVector<QString> getSegments() = 0;
    };
    class Sgz : public SegmentGroup {
    public:
        Segment cnt;
        Segment unt;
    };
    

    In the corresponding source file I've following definition:

    void SegmentGroup::appendSegment(QString str) {
        segments.append(str);
    }
    
    void Sgz::assignSegment() {
        foreach (QString seg, segments) {
            if (seg.startsWith("CNT+"))
                cnt.segStr = seg;
            else if (seg.startsWith("UNT+"))
                unt.segStr = seg;
        }
    }
    QVector<QString> Sgz::getSegments() {
        QVector<QString> segment;
        if (!cnt.segStr.isEmpty())
            segment.append(cnt.segStr);
        if (!unt.segStr.isEmpty())
            segment.append(unt.segStr);
        return segment;
    }
    

    In my main() function I've declared an object of Sgz. But the compiler tells me that I cannot define an object of an abstract class. I think that I've made Sgz a concrete class by defining the both virtual functions in the base class. What's the error? Thanks for any hint!
    Weichao

    [koahnig: code tags added]

    kshegunovK K 2 Replies Last reply
    0
    • W Weichao Wang

      Dear all,
      in a header file I've defined an abstract class (SegmentGroup) and a concrete class (Sgz) dirived from the former:

      class SegmentGroup {
      protected:
          QVector<QString> segments;
      public:
          void appendSegment(QString);
          virtual void assignSegment() = 0;
          virtual QVector<QString> getSegments() = 0;
      };
      class Sgz : public SegmentGroup {
      public:
          Segment cnt;
          Segment unt;
      };
      

      In the corresponding source file I've following definition:

      void SegmentGroup::appendSegment(QString str) {
          segments.append(str);
      }
      
      void Sgz::assignSegment() {
          foreach (QString seg, segments) {
              if (seg.startsWith("CNT+"))
                  cnt.segStr = seg;
              else if (seg.startsWith("UNT+"))
                  unt.segStr = seg;
          }
      }
      QVector<QString> Sgz::getSegments() {
          QVector<QString> segment;
          if (!cnt.segStr.isEmpty())
              segment.append(cnt.segStr);
          if (!unt.segStr.isEmpty())
              segment.append(unt.segStr);
          return segment;
      }
      

      In my main() function I've declared an object of Sgz. But the compiler tells me that I cannot define an object of an abstract class. I think that I've made Sgz a concrete class by defining the both virtual functions in the base class. What's the error? Thanks for any hint!
      Weichao

      [koahnig: code tags added]

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Weichao-Wang said in abstract class and virtual function:

      But the compiler tells me that I cannot define an object of an abstract class.

      Please post the exact error. Also you're missing the functions' declarations in Sgz.

      Read and abide by the Qt Code of Conduct

      W 1 Reply Last reply
      2
      • W Weichao Wang

        Dear all,
        in a header file I've defined an abstract class (SegmentGroup) and a concrete class (Sgz) dirived from the former:

        class SegmentGroup {
        protected:
            QVector<QString> segments;
        public:
            void appendSegment(QString);
            virtual void assignSegment() = 0;
            virtual QVector<QString> getSegments() = 0;
        };
        class Sgz : public SegmentGroup {
        public:
            Segment cnt;
            Segment unt;
        };
        

        In the corresponding source file I've following definition:

        void SegmentGroup::appendSegment(QString str) {
            segments.append(str);
        }
        
        void Sgz::assignSegment() {
            foreach (QString seg, segments) {
                if (seg.startsWith("CNT+"))
                    cnt.segStr = seg;
                else if (seg.startsWith("UNT+"))
                    unt.segStr = seg;
            }
        }
        QVector<QString> Sgz::getSegments() {
            QVector<QString> segment;
            if (!cnt.segStr.isEmpty())
                segment.append(cnt.segStr);
            if (!unt.segStr.isEmpty())
                segment.append(unt.segStr);
            return segment;
        }
        

        In my main() function I've declared an object of Sgz. But the compiler tells me that I cannot define an object of an abstract class. I think that I've made Sgz a concrete class by defining the both virtual functions in the base class. What's the error? Thanks for any hint!
        Weichao

        [koahnig: code tags added]

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @Weichao-Wang

        That would look better:

        class SegmentGroup {
        protected:
            QVector<QString> segments;
        public:
            void appendSegment(QString);
            virtual void assignSegment() = 0;
            virtual QVector<QString> getSegments() = 0;
        };
        class Sgz : public SegmentGroup {
        public:
            void assignSegment();                           // redeclaration required
            QVector<QString> getSegments();    // redeclaration required
        
            Segment cnt;
            Segment unt;
        };
        

        Note: at least that was the syntax some standards ago. In case that became obsolete, I appreciate the hint. I was simply too lazy to check ;)

        Vote the answer(s) that helped you to solve your issue(s)

        W 1 Reply Last reply
        2
        • K koahnig

          @Weichao-Wang

          That would look better:

          class SegmentGroup {
          protected:
              QVector<QString> segments;
          public:
              void appendSegment(QString);
              virtual void assignSegment() = 0;
              virtual QVector<QString> getSegments() = 0;
          };
          class Sgz : public SegmentGroup {
          public:
              void assignSegment();                           // redeclaration required
              QVector<QString> getSegments();    // redeclaration required
          
              Segment cnt;
              Segment unt;
          };
          

          Note: at least that was the syntax some standards ago. In case that became obsolete, I appreciate the hint. I was simply too lazy to check ;)

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

          @koahnig
          You've pointed out the very reason of error. After I've done the correction, the program runs well. Thank you!
          Weichao

          1 Reply Last reply
          1
          • kshegunovK kshegunov

            @Weichao-Wang said in abstract class and virtual function:

            But the compiler tells me that I cannot define an object of an abstract class.

            Please post the exact error. Also you're missing the functions' declarations in Sgz.

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

            @kshegunov
            Now I understand that you mean the same as koahnig. Thank you!
            Weichao

            K 1 Reply Last reply
            0
            • W Weichao Wang

              @kshegunov
              Now I understand that you mean the same as koahnig. Thank you!
              Weichao

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @Weichao-Wang

              Please mark püost as solved then

              Vote the answer(s) that helped you to solve your issue(s)

              W 1 Reply Last reply
              0
              • K koahnig

                @Weichao-Wang

                Please mark püost as solved then

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

                @koahnig
                Done. Thank you for the remind. Are you in Germany (for you input ü besides p)? I'm in Hamburg.
                Weichao

                K 1 Reply Last reply
                0
                • W Weichao Wang

                  @koahnig
                  Done. Thank you for the remind. Are you in Germany (for you input ü besides p)? I'm in Hamburg.
                  Weichao

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @Weichao-Wang

                  South of Germany resp south of Lake Constance (Bodensee)

                  Vote the answer(s) that helped you to solve your issue(s)

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

                    Hi,

                    With C++11, you can add the override keyword after the declaration of each function you re-implement. This will give the compiler a hint that will allow him to scream at you if you try to re-implement a non virtual function.

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

                    1 Reply Last reply
                    4

                    • Login

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