Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,220 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,167 people online right now. Registration is fast and FREE... Join Now!




Program using inheritance - compilation not working

 
Reply to this topicStart new topic

Program using inheritance - compilation not working

rachael_lm
21 Nov, 2007 - 05:17 AM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 1


My Contributions
I have been working on this code for an assignment for my OOP C++ class. I cannot get it to compile. This is what I am getting when I compile:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\OOP C++\S3Exam\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\OOP C++\S3Exam\Makefile.win" all
g++.exe -c mainProg.cpp -o mainProg.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

In file included from mainProg.cpp:3:
student.h:12: error: expected class-name before '{' token

In file included from mainProg.cpp:3:
student.h:4:1: unterminated #ifndef
In file included from mainProg.cpp:4:
staff.h:12: error: expected class-name before '{' token

In file included from mainProg.cpp:4:
staff.h:4:1: unterminated #ifndef
In file included from mainProg.cpp:5:
faculty.h:12: error: expected class-name before '{' token
In file included from mainProg.cpp:5:
faculty.h:4:1: unterminated #ifndef
mainProg.cpp: In function `int main()':
mainProg.cpp:11: error: `person' undeclared (first use this function)
mainProg.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.)
mainProg.cpp:11: error: expected `;' before "pLName"
mainProg.cpp:12: error: expected `;' before "pOccu"

mainProg.cpp:18: error: `pLName' undeclared (first use this function)
mainProg.cpp:20: error: `pFName' undeclared (first use this function)
mainProg.cpp:21: error: no match for 'operator>>' in 'std::cout >> std::endl'
mainProg.cpp:23: error: `lName' undeclared (first use this function)
mainProg.cpp:23: error: `fName' undeclared (first use this function)
mainProg.cpp:27: error: `pOccu' undeclared (first use this function)
mainProg.cpp:31: error: `gOccu' undeclared (first use this function)

make.exe: *** [mainProg.o] Error 1

Execution terminated

Now here is my code: (note that the person header file and implementation files are both separate from the mainProg.cpp file. This goes for all the class header and implementation files. Maybe this has something to do with my problem too?)

CODE

//person.cpp
//person header file

#ifndef H_person
#define H_person

#include <iostream>

using namespace std;

class person
{
      public:
             void getName(char lastName, char firstName);
             void getOccu(int gOccu);
             void setOccu(char occupation);
      private:
              char lName[40];
              char fName[40];
              int gOccupation;
};
#endif

// person.cpp
#include <iostream>
#include "person.h"

using namespace std;

void person::getName(char lastName, char firstName)
{
     lastName = lName;
     firstName = fName;
}

void person::getOccu(int gOccu)
{
     gOccu = gOccupation;
}

void person::setOccu(char occupation)
{
     if (gOccu == 1)
        occupation = 'student';
     if (gOccu == 2)
        occupation = 'staff member';
     if (gOccu == 3)
        occupation = 'faculty member';
}

//student.cpp
//student header file

#ifndef H_student
#define H_student

#include <iostream>

using namespace std;

class student: public person
{
      public:
             void getName(char lastName, char firstName);
             void getOccu(int gOccu);
             void setOccu(char occupation, char major);
             void print() const;
      private:
              char studentMajor[50];
};

// student.cpp
#include <iostream>
#include "student.h"

using namespace std;

void student::getName(char lastName, char firstName)
{
     person::getName(lastName, firstName);
}

void student::getOccu(int gOccu)
{
     person::getOccu(int gOccu);
}

void student::setOccu(char occupation, char major)
{
     person::setOccu(char occupation);
     cout << "Enter your major: " <<
     cin >> studentMajor;
     cout << endl;
    
     major = studentMajor;
}

void student::print() const
{
     cout << lastName << ", " << firstName << " is a " << occupation << " majoring in " << major << endl;
}

//staff.cpp
//staff header file

#ifndef H_staff
#define H_staff

#include <iostream>

using namespace std;

class staff: public person
{
      public:
             void getName(char lastName, char firstName);
             void getOccu(int gOccu);
             void setOccu(char occupation, char position);
             void print() const;
      private:
              char positionTitle[40];
};

// staff.cpp
#include <iostream>
#include "staff.h"

using namespace std;

void staff::getName(char lastName, char firstName)
{
     person::getName(lastName, firstName);
}

void student::getOccu(int gOccu)
{
     person::getOccu(int gOccu);
}

void staff::setOccu(char occupation, char position)
{
     person::setOccu(char occupation);
     cout << "Enter your position title including your department: " <<
     cin >> positionTitle;
     cout << endl;
    
     position = positionTitle;
}

void staff::print() const
{
     cout << lastName << ", " << firstName << " is a " << occupation << " working as the " << position << endl;
}

//faculty.cpp
//faculty header file

#ifndef H_faculty
#define H_faculty

#include <iostream>

using namespace std;

class faculty: public person
{
      public:
             void getName(char lastName, char firstName);
             void getOccu(int gOccu);
             void setOccu(char occupation, char fDept, char fTitle);
             void print() const;
      private:
              char facultyDepartment[40];
              char facultyTitle[40];
};

// faculty.cpp
#include <iostream>
#include "faculty.h"

using namespace std;

void facutly::getName(char lastName, char firstName)
{
     person::getName(lastName, firstName);
}

void student::getOccu(int gOccu)
{
     person::getOccu(int gOccu);
}

void faculty::setOccu(char occupation, char fDept, char fTitle)
{
     person::setOccu(char occupation);
     cout << "Enter your department: " <<
     cin >> facultyDepartment;
     cout << "Enter your title: " <<
     cin >> facultyTitle;
     cout << endl;
    
     fDept = facultyDepartment;
     fTitle = facultyTitle;
}

void faculty::print() const
{
     cout << lastName << ", " << firstName << " is a " << occupation << " working in the " << fDept
     << " as " << fTitle << endl;
}

// mainProg.cpp
#include <iostream>
#include "person.h"
#include "student.h"
#include "staff.h"
#include "faculty.h"

using namespace std;

int main()
{
    person pLName[40], pFName[40];
    person pOccu;
    student sOccu;
    staff stOccu;
    faculty fOccu;
        
    cout << "Enter the last name (up to 40 characters): ";
    cin >> pLName;
    cout << "Enter the first name (up to 40 characters): ";
    cin >> pFName;
    cout >> endl;
    
    pLName, pFName.getName(lName, fName);
    
    cout << "Enter the number of one of the following choices as it applies to: "
    << pLName, pFName << "     1: Student, 2: Staff, 3: Faculty: ";
    cin >> pOccu;
    
    if (pOccu == 1)
       sOccu = pOccu;
       sOccu.getOccu(gOccu);
    if (pOccu == 2)
       stOccu = pOccu;
       stOccu.getOccu(gOccu);
    if (pOccu == 3)
       fOccu = pOccu;
       fOccu.getOccu(gOccu);
    
    return 0;
}


I have also attached a zipped folder of my work.


Attached File(s)
Attached File  S3Exam.zip ( 5.08k ) Number of downloads: 17
User is offlineProfile CardPM
+Quote Post

Bench
RE: Program Using Inheritance - Compilation Not Working
21 Nov, 2007 - 09:56 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(rachael_lm @ 21 Nov, 2007 - 01:17 PM) *


student.h:4:1: unterminated #ifndef



The error that your compiler appears to be giving you is that you have a preprocessor #ifndef directive at the top of your student.h header file, but you haven't closed that block anywhere.
Try adding #endif to the bottom of that file.


QUOTE(rachael_lm @ 21 Nov, 2007 - 01:17 PM) *


cout << "Enter your department: " <<
cin >> facultyDepartment;
cout << "Enter your title: " <<
cin >> facultyTitle;


Some "rogue" << operators laying around here...


QUOTE(rachael_lm @ 21 Nov, 2007 - 01:17 PM) *


mainProg.cpp:21: error: no match for 'operator>>' in 'std::cout >> std::endl'


Careful with your input/output operators. You've got the wrong one here on Line 21 of mainProg.cpp


There might be some other mistakes I didn't pick up, I've only read the code you posted and not tried compiling it myself.


I would make one suggestion to you, that is, to take the approach of testing very often. In other words, write a few lines of code, then compile & run it to pick up any typing errors or other mistakes that have snuck in accidentally. Do this until you're at least confident enough in yourself about the syntax and grammar of C++ that you can pick up the mistakes as you go along. Whilst you're still at the early stage of the learning curve, you'll find your life gets alot easier when you're not trying to fix dozens of compiler errors all at once.

This post has been edited by Bench: 21 Nov, 2007 - 10:13 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:41AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month