Virtual functions not working

This is a discussion on Virtual functions not working within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a class entitled "Layer" and a derived class entitled "Point". I have a pure virtual function in ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Unhappy Virtual functions not working

    Hello,

    I have a class entitled "Layer" and a derived class entitled "Point". I have a pure virtual function in "Layer" entitled printLayer(). It is not defined in "Layer" although it is defined in "Point".

    The program compiles OK, but when I try to build it I get "Unresolved External" errors.

    Here is my code for the printLayer() in the "Layer" header:

    Code:
    virtual void printLayer(int);	//pure virtual function
    and "Point" header:

    Code:
    virtual void printLayer(int);			//virtual function with definition
    Here is the code for the printLayer() function in the "Point" defintion:

    Code:
    //begin printLayer
    void Point::printLayer(int intArg)
    {
    	cout << "Point printLayer with argument " << intArg << " !" << endl;
    
    }	//end printLayer
    Here is my main program utilizing the classes:

    Code:
    #include <iostream.h>
    #include "Layer.h"
    #include "Point.h"
    
    int main()
    {
    	//create a layer object and assign it to a layer pointer
    	Layer * LayerPtr = new Point("aluspts_0.shp", "C:\\pec\\complete\\vector");
    	
    	int i = 2;
    	LayerPtr->printLayer(i);
    
    	delete LayerPtr;
    
    	return 0;
    
    }	//end main
    Here are the error messages I receive upon using the build command. Note my main program is entitled "test.cpp":

    Code:
    test error LNK2001: unresolved external symbol "public: virtual void __thiscall Layer::printLayer(int)" (?printLayer@Layer@@UAEXH@Z)
    test fatal error LNK1120: 1 unresolved externals
    It appears the compiler is searching the "Layer" definitions for the printLayer() function. According to my documentation, a pure virtual function of an abstract base class requires no definition. The definition belongs in the derived class, which is "Point" in my case.

    Why am I getting these error messages?
    Last edited by VanJay011379; 07-25-2002 at 11:34 AM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    To declare a pure vitual function, you need to define it as NULL.

    In your Layer class, define printLayer as:

    virtual void printLayer(int) = 0;

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    That's it Thanks
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 12:08 PM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  4. what's the purpose of virtual functions
    By vfunction1 in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2003, 09:32 PM
  5. Virtual Functions
    By Lazy Student in forum C++ Programming
    Replies: 7
    Last Post: 10-04-2002, 01:55 PM

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