Help with Linker error, while using multiple inheritance

This is a discussion on Help with Linker error, while using multiple inheritance within the C++ Programming forums, part of the General Programming Boards category; Hi I have been trying to compile this code, in this I had a class clocktype with its header and ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Help with Linker error, while using multiple inheritance

    Hi
    I have been trying to compile this code, in this I had a class clocktype with its header and implementation file and now I have to extend this class to be able to implement the function timezone in it. This is a program where I have to use two header files in my derived class.
    I am posting my code here:

    Code:
    //header file clocktype
    #ifndef h_clocktype
    #define h_clocktype
    #include<string>
    using namespace std;
    
    
    class clocktype
    {
    public:
    	void settime(int hours, int minutes, int seconds);
    	void gettime(int& hours, int& minutes, int& seconds);
    	void printtime();
    	void incseconds();
    	void incminutes();
    	void inchours();
    
    	clocktype (int hours, int minutes, int seconds);
    	clocktype();
    
    protected:
    	int hr;
    	int min;
    	int sec;
    };
    #endif
    
    // implemnetation file
    #include<iostream>
    #include<conio.h>
    #include "clocktype.h"
    
    using namespace std;
    
    void clocktype::settime(int hours, int minutes, int seconds)
    {
    	if(0<= hours && hours<24)
    		hr=hours;
    	else
    	hr=0;
    
    	if(0<=minutes && minutes<60)
    		min=minutes;
    	else
    		min=0;
    
    	if (0<=seconds && seconds<60)
    		sec=seconds;
    	else
    		sec=0;
    }
    void clocktype::gettime(int& hours, int& minutes, int& seconds)
    {
    	hours=hr;
    	minutes=min;
    	seconds=sec;
    }
    
    void clocktype::printtime()
    {
    	if(hr<10)
    		cout<<"0";
    		cout<<hr<<":";
    	if(min<10)
    		cout<<"0";
    	cout<<min<<":";
    	if(sec<10)
    		cout<<"0";
    	cout<<sec;
    }
    
    void clocktype::inchours()
    {
    	hr++;
    	if(hr<23)
    		hr=0;
    }
    void clocktype::incminutes()
    {
    	min++;
    	if(min<59)
    		min=0;
    	inchours();
    }
    void clocktype::incseconds()
    {
    	sec++;
    	if(sec<59)
    		sec=0;
    	incminutes();
    }
    
    clocktype::clocktype()
    {
    	hr=0;
    	min=0;
    	sec=0;
    }
    clocktype::clocktype(int hours, int minutes, int seconds)
    {
    	if(0<= hours && hours<24)
    		hr=hours;
    	else
    	hr=0;
    
    	if(0<=minutes && minutes<60)
    		min=minutes;
    	else
    		min=0;
    
    	if (0<=seconds && seconds<60)
    		sec=seconds;
    	else
    		sec=0;
    }
    
    // main function
    
    int main()
    {
    	clocktype myclock;
    	myclock.settime( 12, 14, 40);
    	cout<<" The time now is : ";
    		myclock.printtime();
    	myclock.settime( 24, 60, 60);
    	cout<<"\n The time now is : ";
    	myclock.printtime();
    
    	getch();
    }
    And now I have the derived class

    Code:
    #ifndef h_clocktype2
    #define h_clocktype2
    #include<iostream>
    #include"clocktype.h"
    using namespace std;
    
    enum zonetype {GMT, EST, PST, IST, CET};
    class clocktype2: public clocktype
    {
    	public:
    	void settime(int hours, int minutes, int seconds,zonetype timezone);
    	
    	void printtime();
    	
    	clocktype2 (int hours, int minutes, int seconds, zonetype timezone);
    	clocktype2();
    
    private:
    	zonetype tz;
    };
    #endif
    
    #include<iostream>
    #include<conio.h>
    #include"clocktype.h"
    #include"clocktype2.h"
    
    
    using namespace std;
    
    void clocktype2::settime(int hours, int minutes, int seconds, zonetype timezone)
    {
    	hours=hr;
    	minutes=min;
    	seconds=sec;
    	timezone=tz;
    
    }
    void clocktype2::printtime()
    {
    	switch(tz)
    	{
    	case GMT:
    		cout<<"GMT";
    		break;
    	case EST:
    		cout<<"EST";
    		break;
    	case PST:
    		cout<<"PST";
    		break;
    	case IST:
    		cout<<"IST";
    		break;
    	case CET:
    		cout<<"CET";
    		break;
    	}
    }
    clocktype2::clocktype2(){
    hr=0;
    	min=0;
    	sec=0;
    	tz=GMT;
    		   }
    
    clocktype2::clocktype2(int hours, int minutes, int seconds, zonetype timezone)
    {
    hours=hr;
    	minutes=min;
    	seconds=sec;
    	timezone=tz;
    }
    
    int main()
    {
    	clocktype2 clock;
    	clock.settime( 12, 14, 40, GMT);
    	cout<<" The time now is : ";
    		clock.printtime();
    	clock.settime( 24, 60, 60, IST);
    	cout<<"\n The time now is : ";
    	clock.printtime();
    
    	getch();
    }

    Now when I try to run this program it gives me the following error:
    ------ Build started: Project: lab-4, Configuration: Debug Win32 ------
    Linking...
    clocktypeimp2.obj : error LNK2019: unresolved external symbol "public: __thiscall clocktype::clocktype(void)" (??0clocktype@@QAE@XZ) referenced in function "public: __thiscall clocktype2::clocktype2(void)" (??0clocktype2@@QAE@XZ)
    C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\lab-4\Debug\lab-4.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\lab-4\lab-4\Debug\BuildLog.htm"
    lab-4 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    That is inheritance, not multiple inheritance. In multiple inheritance a derived class inherits from multiple base classes.

    As to your error, there's nothing wrong with your code. You have a linker error, probably because you didn't correctly add clocktype to your lab-4 project. It's not enough to just #include the clocktype header. You have to add clocktype to your build configuration so that the clocktype implementation is compiled along with the clocktype2 implementation.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Oh Thanks for the correction.
    But all my files are in the same project, Lab-4. And still it is giving me this Linker error. I can not figure out why ?
    Do I have to add #ifndef and #define and #endif to my .cpp file of my base class?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Lichi View Post
    Do I have to add #ifndef and #define and #endif to my .cpp file of my base class?
    It has nothing to do with header guards. There's something wrong with the way your project is configured. I can't help you there -- I'm a complete novice when it comes to MS Visual Studio, but I can tell you that I just pasted your exact code into a new project (actually the first project that I have ever created with VS2010Express), setting up 5 files: clocks.cpp (for the main function), clocktype.h, clocktype.cpp, clocktype2.h and clocktype2.cpp, and it builds and runs without errors (after I figured out how to set it to NOT use precompiled headers).

    I suggest that you try removing the clocktype and clocktype2 headers and implementation files from your project directory, then go to Project/Add Item/C++ File and Project/Add Item/Header File and create new files and then paste your code into those files.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Oh I just wanted to tell, that my program worked yesterday. And the error I was having was so Silly, I had two mains in my program I did not delete the main from the base class.
    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple inheritance in C#
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 06-27-2008, 05:41 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 03:41 AM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 11:04 PM

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