CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jul 2007
    Posts
    11

    C++ Newbie - Derived Class Issue

    I'm doing an assignment for a C++ class and I'm having some serious issues understanding the topic. The assignment was to make a class called Employee..then make a class called Payroll that is derived from Employee. I think I am doing something wrong with my syntax but I can't figure out what it is. I have the employee class working.....but when I try to do anything with the derived class it gives a compile error. What am I doing wrong?

    I know that the employee class is working and it doesn't seem to matter what I put into the payroll class (constructor or otherwise...it all gives the same error).

    Here are my class declarations...

    Thanks in advance!

    Phil

    p.s. The error I'm getting says "Unresolved External Symbol"
    -------------------

    class Employee{

    protected:
    char name[50];
    char ssn[11];
    char empId[5];
    char hireDate[10];

    public:
    Employee();
    Employee(char social[], char EmpId[], char HireDate[], char Name[]){
    setSocial(social);
    setEmpId(EmpId);
    setHireDate(HireDate);
    setName(Name);
    }
    bool setSocial(char[]);
    void printSocial();
    bool setEmpId(char[]);
    void printEmpId();
    bool setHireDate(char[]);
    void printHireDate();
    bool setName(char[]);
    void printName();
    };

    class Payrollublic Employee{

    protected:
    float payRate;
    float overtimeRate;
    int hoursworked;

    public:
    Payroll(float rate,float overtime,int hours){
    setPayRate(rate);
    setovertime(overtime);
    sethours(hours);
    }

    bool setPayRate(float);
    bool setovertime(float);
    bool sethours(int);

    };

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    1) Please use code tags so that your post is readable.

    2) Given the unreadable nature of your current post, can you at least share the EXACT SPECIFIC questions/problems you are having.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jul 2007
    Posts
    11

    Re: C++ Newbie - Derived Class Issue

    I'm sorry, this is my first post and I didn't realize that there were code tags.

    Here is the class declaration posted again. The issue that I'm having is that something seems to be wrong with my derived class declaration and I can't figure out what it is. I have other examples that I've done before with smaller programs that look to me to be the exact same syntax...but they worked there and not here. I can't figure out what is different though.

    Code:
    class Employee{
    
    protected:
    	char name[50];
    	char ssn[11];
    	char empId[5];
    	char hireDate[10];
    
    public:
    	Employee();
    	Employee(char social[], char EmpId[], char HireDate[], char Name[]){
    		setSocial(social);
    		setEmpId(EmpId);
    		setHireDate(HireDate);
    		setName(Name);
    	}
    	bool setSocial(char[]);
    	void printSocial();
    	bool setEmpId(char[]);
    	void printEmpId();
    	bool setHireDate(char[]);
    	void printHireDate();
    	bool setName(char[]);
    	void printName();
    };
    
    class Payroll:public Employee{
    
    protected:
      float payRate;
      float overtimeRate;
      int hoursworked;
    
    public:
      Payroll(float rate,float overtime,int hours){
    	   setPayRate(rate);
    	   setovertime(overtime);
    	   sethours(hours);
      }
    
    	bool setPayRate(float);
    	bool setovertime(float);
    	bool sethours(int);
    	  
    };

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    Looks fine to my (except that putting the opening brace at the end of a line is a Java convention, not C++. Braces should line up and bee on their own lint).

    Again I ask, what EXACT SPECIFIC question/problem to You have??
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jul 2007
    Posts
    11

    Re: C++ Newbie - Derived Class Issue

    The issue is that I'm getting the error

    error LNK2019: unresolved external symbol "public: __thiscall Employee::Employee(void)" (??0Employee@@QAE@XZ) referenced in function "public: __thiscall Payroll::Payroll(float,float,int)" (??0Payroll@@QAE@MMH@Z) project5.obj

    when I try to compile the project. I'm not sure what I'm doing wrong but I've tried it about 20 different ways and nothing is working.

    I'm calling the Payroll class using the following:

    Code:
    Payroll stevesCheck(10,15,10);

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    That is not a declaration problem, it is am implementation problem. In some .cpp file your need


    Code:
    Employee::Employee()
    { // What to do for a default employee
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Jul 2007
    Posts
    11

    Re: C++ Newbie - Derived Class Issue

    The employee class works fine with the inline constructor that was in my header file. It isn't until I introduce the Payroll class that the error pops up. Do I need another employee constuctor for the payroll class?

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by Gipionocheiyort
    The employee class works fine with the inline constructor that was in my header file. It isn't until I introduce the Payroll class that the error pops up. Do I need another employee constuctor for the payroll class?

    PRobably because you were calling the parameterized version of the Employee class. Simply put a call to "Employee test;" in your code, and you will see it fail. Even if you totally remove the payroll class.

    This problem has nothing to do with the interface.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jul 2007
    Posts
    11

    Re: C++ Newbie - Derived Class Issue

    Ah I get it now. I had actually tried to fix that but I made a syntax error. When I switched

    Code:
    Employee();
    to [code]Employee(){}code]

    it worked.

    Thanks for your help!

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by Gipionocheiyort
    Ah I get it now. I had actually tried to fix that but I made a syntax error. When I switched

    Code:
    Employee();
    to
    Code:
    Employee(){}
    it worked.

    Thanks for your help!
    Of course none of the fields of the employee are going to be initialized....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by TheCPUWizard
    Looks fine to my (except that putting the opening brace at the end of a line is a Java convention, not C++. Braces should line up and bee on their own lint).

    and all this time i thought it was a personal preference thing, hmmmm

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by ka3ak
    and all this time i thought it was a personal preference thing, hmmmm
    So is the following:
    Code:
    #include<istrream>
    class X {int a;int b;X():a(1),b(2){};int f(){return(a+b);}void g(int n){a-n;);void h(int m){b=m}};void main(){X x; x.f(4);x.g(10);cout<<x.f();}
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by TheCPUWizard
    So is the following:
    Code:
    #include<istrream>
    class X {int a;int b;X():a(1),b(2){};int f(){return(a+b);}void g(int n){a-n;);void h(int m){b=m}};void main(){X x; x.f(4);x.g(10);cout<<x.f();}
    and this:

    Code:
    class X
    
    {
               int a;
    
               int b;
    
               X()        :         a(1)      ,     b(1)      
              {
    
              }
    
    
            int f(       )
            {
    
                    return a    + b;
    
            }
    
    
    
          ...
    };

  14. #14
    Join Date
    May 2007
    Posts
    811

    Re: C++ Newbie - Derived Class Issue

    Just an observation, but I'm always surprise to see code from students learning programming (specially C++) to use char/char[] rather then std::string. Why do professors still teach that in Universities?

    Last question was more rhetorical one.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ Newbie - Derived Class Issue

    Quote Originally Posted by ka3ak
    and all this time i thought it was a personal preference thing, hmmmm
    It is, but I agree with the wizard that code where braces don't line up is very difficult to read to me.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured