Derived class linking error

This is a discussion on Derived class linking error within the C++ Programming forums, part of the General Programming Boards category; I am working on a programming project from the book I am learning c++ from. I already have a base ...

  1. #1
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

    Derived class linking error

    I am working on a programming project from the book I am learning c++ from.

    I already have a base class and a derived class given to me (and they work properly, I checked). I am too make a derived class from the derived class.
    The derived class I am working on is called Administrator, the class I am deriving it from is called SalariedEmployee.

    I think my constructors are working correctly…Now, I have a simple main function that I am using trying to test my work so far.

    Code:
    #include <iostream>
    #include <string>
    #include "administrator.h"
    
    
    using namespace std;
    using namespace employeessavitch;
    
    int main()
    {
    
    	Administrator shane;
    	shane.set_supervisor();
    	//or shane.print() or shane.info_in()
    
    
    
    
    	return 0;
    }
    It compiles fine, but when I link I get the follow error:
    Code:
    5.obj : error LNK2001: unresolved external symbol "public: void __thiscall employeessavitch::Administrator::set_supervisor(void)"
     (?set_supervisor@Administrator@employeessavitch@@QAEXXZ)
    
    5.obj : error LNK2001: unresolved external symbol "public: __thiscall employeessavitch::Administrator::Administrator(void)"
     
    (??0Administrator@employeessavitch@@QAE@XZ)
    Debug/5.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    And I get the same error for any function I have created (but replaced with the name of that function in the error). And I have made sure that the other classes the variables are under ‘protected’ and not ‘private’.

    Any ideas?
    *edit* I just noticed I am also getting a linking error when I try and call the constructor that takes values).....
    .h and .cpp from classes follow.

    Administrator.h
    Code:
    #ifndef ADMINISTRATOR_H
    #define ADMINISTRATOR_H
    
    #include <string>
    #include "salariedemployee.h"
    
    using namespace std;
    
    
    namespace employeessavitch
    {
    
    
    
    
    	class Administrator : public SalariedEmployee
    	{
    
    	public:
    
    
    		Administrator();
    		Administrator(string the_title, string the_position, double the_salary /*weekly*/, string the_supervisor,
     string the_name, string the_ssn );
    
    		void set_supervisor();
    		void info_in( ); 
    
    		void print();
    		void print_check();
    
    	
    
    
    	protected:
    		
    		
    		string Title;
    		string Position;
    		string Supervisor;
    
    	};
    
    
    }//empolyeessavitch
    
    
    #endif
    administrator.cpp
    Code:
    #include <string>
    #include <iostream>
    #include "administrator.h"
    using namespace std;
    
    
    
    namespace employeessavitch
    {
    
    
    	Administrator::Administrator() : SalariedEmployee(), Title("N/A"), Position("N/A"), Supervisor("N/A")
    	{
    		//Left blank
    	}
    
    
    	Administrator::Administrator(string the_title, string the_position, double the_salary, 
    string the_supervisor, string the_name, string the_ssn) :
    /* : */	SalariedEmployee(the_name, the_ssn, the_salary), Title(the_title), Position(the_position), Supervisor(the_supervisor)
    	{
    		//Left blank
    	}
    
    
    	void Administrator::set_supervisor() 
    	{
    		string temp;
    		cout << "Enter the new supervisor's name: ";
    		cin >> temp;
    
    		Supervisor = temp;
    	}
    
    
    	void Administrator::info_in()
    	{
    		
    		cout << "Enter the administrators title: ";
    		cin >> Title;
    		cout << "\nEnter the administrators position: ";
    		cin >> Position;
    		cout << "\nEnter the administrators weekly salary: ";
    		cin << salary;
    	}
    
    
    
    	void Administrator::print()
    	{
    
    		cout << "Output data to screen"; //finish later
    	}
    
    
    	void Administrator::print_check()
    	{
    
    		cout << "Output data in and info from base classes in check format"; //finish later
    	}
    
    
    }//employeessavitch
    Last edited by Enahs; 11-12-2005 at 08:38 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what compiler are you using? I used VC++ 6.0 and did not have that problem. But there was one error
    Code:
    	void Administrator::info_in()
    	{
    		
    		cout << "Enter the administrators title: ";
    		cin >> Title;
    		cout << "\nEnter the administrators position: ";
    		cin >> Position;
    		cout << "\nEnter the administrators weekly salary: ";
    		cin << salary;
    	}
    cin takes the ">>" operator, not "<<"., which will cause administrator.cpp to not be compiled correctly.

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    did you forget to compile administrator.cpp? It looks to me like you only added the main cpp file to the project.

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    I am also using VC++ 6.0 (With the latest SP).

    Apon further inspection when I try and compile and link the administrator.cpp I get this error:
    Code:
    administrator.obj : error LNK2001: unresolved external symbol "public: __thiscall 
    employeessavitch::SalariedEmployee::SalariedEmployee(void)"
    
     (??0SalariedEmployee@employeessavitch@@QAE@XZ)
    
    administrator.obj : error LNK2001: unresolved external symbol "public: __thiscall employeessavitch::SalariedEmployee::SalariedEmployee
    (class 
    
    std::basic_string<char,struct std::char_traits<char>,
    
    class std::allocator<char> >,class std::basic_string<ch
    ar,struct std::char_traits<char>,class std::allocator<char> >,double)" 
    
    (??0SalariedEmployee@employeessavitch@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0N@Z)
    LIBCD.lib(crt0.obj)
    
     : error LNK2001: unresolved external symbol _main
    
    Debug/administrator.exe : fatal error LNK1120: 3 unresolved externals
    Error executing link.exe.
    When I take out my constructors that call the default constructors for the base class “SalariedEmployee” I do not that the two problematic errors.

    I am just calling the default constructors for salaried employee:
    Code:
    public:
            SalariedEmployee( );
            SalariedEmployee (string the_name, string the_ssn,
                                      double the_weekly_salary);
    And silly me yes I did forget to try and compile the .cpp file, which if I had I would have found the previous info in the post out! :0

    *edit* sorry for all the crazy new lines in the error....just trying to keep it from making the page 5,000 char long!
    Last edited by Enahs; 11-12-2005 at 09:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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