Pure virtual function mixed with regular virtual function problem

This is a discussion on Pure virtual function mixed with regular virtual function problem within the C++ Programming forums, part of the General Programming Boards category; Hi, here's my problem: -I want to declare both a pure virtual function and a normal virtual one in my ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    1

    Pure virtual function mixed with regular virtual function problem

    Hi, here's my problem:
    -I want to declare both a pure virtual function and a normal virtual one in my abstract base class. I want to give the non-pure virtual function a definition.

    FILE: ScreenElement.h
    Code:
    #include <iostream>
    #include "Screen.h"
    using namespace std;
    
    ///This class represents a ScreenElement and can be read from a stream and drawn to the screen. This class is an abstract base class.
    class ScreenElement{
    public:
    virtual void draw(Screen& screen) const; ///< A virtual function for drawing a screen element
    virtual void read(istream &is) = 0; ///< A pure virtual function for reading a screen element
    virtual ~ScreenElement(){}; ///< A virtual destructor
    // Needed so destructors are called in order
    };
    FILE:ScreenElement.cc
    Code:
    #include "invalid_screen_error.h"
    #include <iostream>
    #include "ScreenElement.h"
    #include "Screen.h"
    #include <stdexcept>
    using namespace std;
    
    void ScreenElement::draw(Screen &screen) const{
    if(&screen == NULL)
    throw invalid_screen_error("Screen has not been created. You may not draw to it");
    return;
    }
    I get the error when trying to compile:
    g++ -c -Wall Screen.cc
    g++ -c -Wall Line.cc
    g++ -c -Wall Box.cc
    g++ -c -Wall Text.cc
    g++ -c -Wall TextBox.cc
    g++ -c -Wall draw.cc
    g++ -Wall Screen.o Line.o Box.o Text.o TextBox.o draw.o -o draw
    Line.o: In function `Line::draw(Screen&) const':
    Line.cc.text+0x86): undefined reference to `ScreenElement::draw(Screen&) const'
    Line.o: In function `ScreenElement::ScreenElement()':
    Line.cc.text._ZN13ScreenElementC2Ev[ScreenElement::ScreenElement()]+0x4): undefined reference to `vtable for ScreenElement'
    Line.o: In function `ScreenElement::~ScreenElement()':
    Line.cc.text._ZN13ScreenElementD2Ev[ScreenElement::~ScreenElement()]+0x7): undefined reference to `vtable for ScreenElement'
    Line.o.rodata._ZTI4Line[typeinfo for Line]+0x8): undefined reference to `typeinfo for ScreenElement'
    Box.o.rodata._ZTI3Box[typeinfo for Box]+0x10): undefined reference to `typeinfo for ScreenElement'
    Text.o.rodata._ZTI4Text[typeinfo for Text]+0x10): undefined reference to `typeinfo for ScreenElement'
    collect2: ld returned 1 exit status
    make: *** [draw] Error 1

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    You forgot to compile ScreenElement.cc and link with the generated object file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    oh, you must after definition of every virtual function put " = 0"

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Quote Originally Posted by m_pahlevanzadeh View Post
    oh, you must after definition of every virtual function put " = 0"
    Why would you bump an old thread to post wrong information?
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. virtual or pure virtual
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2001, 06:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21