Thread: Undefined reference to vtable

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Undefined reference to vtable

    Base class Employee with some derived classes like HourlyWorker, PieceWorker, etc. One virtual function: void print.
    Problem is that with only just the method declarations I keep getting error messages about "undefined reference to vtable" when I am trying to define the various constructors. From what little I know, I understand vtable is an array of pointers to virtual functions that cpp has in the background and the only way I have found so far to get around this error message is to define the virtual functions alongside their declarations but obviously its not a very robust or long-term solution.
    So any suggestions about how I can keep the all function declarations, including virtual functions, separate and then define them together later would be most welcome. My code is below. Many thanks.

    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    class Employee{
    	private:
    		enum {LIM = 20};
    		char m_first_name[LIM];
    		char m_last_name[LIM];
    	public:
    		Employee(const char* first_name = "none", const char* last_name = "none");
    		virtual ~Employee(){};
    		virtual void print(){cout<<m_first_name<<" "<<m_last_name<<endl;};		
    };
    class HourlyWorker: public Employee{
    	private:
    		float m_hw_weekly_hours;
    		float m_hw_hourly_rate;
    	public:
    		HourlyWorker(const char* first_name = "none", const char* last_name = "none", float hw_weekly_hours = 0.0, float hw_hourly_rate = 0.0);
    		virtual ~HourlyWorker(){};
    		virtual void print(){};
    };
    class PieceWorker: public Employee{
    	private:
    		int m_pw_num_items;
    		float m_pw_item_rate;
    	public:
    		PieceWorker(const char* first_name = "none", const char* last_name = "none", int pw_num_items = 0, float pw_item_rate = 0.0);
    		virtual ~PieceWorker(){};
    		virtual void print(){};
    };
    class BossWorker: public Employee{
    	private:
    		float m_bw_weekly_salary;
    	public:
    		BossWorker(const char* first_name = "none", const char* last_name = "none", float bw_weekly_salary = 0.0);
    		virtual ~BossWorker(){};
    		virtual void print(){};
    };
    class CommissionWorker: public Employee{
    	private:
    		float m_cw_weekly_salary;
    		float m_cw_gross_sales;
    		float m_cw_comm_percent;
    	public:
    		CommissionWorker(const char* first_name = "none", const char* last_name = "none", float cw_weekly_salary = 0.0,
    						 float cw_gross_sales = 0.0, float cw_comm_percent = 0.0);
    		virtual ~CommissionWorker(){};
    		virtual void print(){};
    };
    int main(){
    	Employee emp("John", "Smith");
    	emp.print();
    	
    }
    Employee::Employee(const char* first_name, const char* last_name){
    	strncpy(m_first_name,first_name,LIM-1);
    	m_first_name[LIM-1]='\0';
    	strncpy(m_last_name,last_name,LIM-1);
    	m_last_name[LIM-1]='\0';
    }
    HourlyWorker::HourlyWorker(const char* first_name, const char* last_name, float hw_weekly_hours, float hw_hourly_rate)
    	:Employee(first_name, last_name){
    		m_hw_weekly_hours = hw_weekly_hours;
    		m_hw_hourly_rate = hw_hourly_rate;
    }
    PieceWorker::PieceWorker(const char* first_name, const char* last_name, int pw_num_items, float pw_item_rate)
    	:Employee(first_name, last_name){
    		m_pw_num_items = pw_num_items;
    		m_pw_item_rate = pw_item_rate;
    	}
    BossWorker::BossWorker(const char* first_name, const char* last_name, float bw_weekly_salary)
    	:Employee(first_name, last_name){
    		m_bw_weekly_salary = bw_weekly_salary;
    }
    CommissionWorker::CommissionWorker(const char* first_name, const char* last_name, float cw_weekly_salary, float cw_gross_sales, float cw_comm_percent)
    	:Employee(first_name, last_name){
    		m_cw_weekly_salary = cw_weekly_salary;
    		m_cw_gross_sales = cw_gross_sales;
    		m_cw_comm_percent = cw_comm_percent;
    	}

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You need to make sure that all of your non-pure virtual functions have an implementation. You have one in each class that is not.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Yes, and I also see that the implementations can be separated from the declarations once they are in place and so any header file can thus be without any implementation details. Many thanks.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should also use std::string instead of const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker error: undefined reference to `vtable in Observer`
    By heatblazer in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2016, 10:54 AM
  2. New to C; Undefined reference
    By neduag in forum C Programming
    Replies: 3
    Last Post: 06-29-2015, 02:52 PM
  3. undefined reference to
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2010, 03:06 PM
  4. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  5. Undefined reference
    By Buckshot in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 03:28 PM

Tags for this Thread