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

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




Can't get progam to complile Erro 2061

2 Pages V  1 2 >  
Reply to this topicStart new topic

Can't get progam to complile Erro 2061, Develop a set of classes to represent all of the people that would be

snyderchip
9 Jul, 2008 - 03:40 PM
Post #1

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

CODE

class Faculty: public Person  
{  
public:  
    string collegeName;  
    double hourlySalary;  
    int facultyID;  

  
    Faculty(string collegeName, double hourlySalary, int facultyID)  
        : collegeName(collegeName), hourlySalary(hourlySalary),  
            facultyID(facultyID)
    


    void PrintNameAndSalary()  
    {  

       cout << Name << ": " << collegeName << ", "  <<  hourlySalary << ", " << facultyID << endl;  
    }  
};  
  
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;  
    bool Male;  
  
    string GetName();  
    int GetSSNum();
    enum Sex{Male, Female };  
  
  Person(string Name = "", int SSNum = 0, Sex (Sex)
        :Name(), SSNum(),Sex ())  
    {  
    }  
  
};  
class Staff: public Person  
{  
public:  
       double WeeklySalary;    
    int YearsOfService;


    Staff(double WeeklySalary, int YearsOfService)  
        : WeeklySalary(WeeklySalary),  
            YearsOfService(YearsOfService)  
    {  
    }  


    void PrintNameAndService()  
    {  

        cout <<Name << ": " << YearsOfService << ", "  <<  WeeklySalary << ", " << endl;  
    }  
};
class Student : public Person  
{  
public:  
    double GPA;  
    int Grades;  
       Student(double GPA = 0.0, int Grades = 0)  
       :GPA(GPA), Grades(Grades)  
  
    {  
    }  

    void PrintNameAndGrade()  
    {  

    cout << Name << ": " << GPA << ", "  <<  Grades  << endl;  
    }  

    

};  
  
  
#include "Faculty.h"
#include "Person.h"
#include "Staff.h"
#include "Student.h"
#include <string>  
#include <iostream>
#include <iomanip>

using namespace std;

int main()  
{  
Student g(3.8, 'A');
g.PrintNameAndGrade();


Faculty f("Baker College", 30.0, 25);
f.PrintNameAndSalary();

Staff s(12.00, 250);
s.PrintNameAndService();

    return 0;  
}


** Edit ** code.gif
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 03:47 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Please don't create two accounts, just to post the same issue. It is extremely disrespectful. Plus, when you post your code, please use
code.gif

In any case, you have numerous errors.

I'm going to assume each of those classes is in it's own header. In any case, here is your code slightly reordered, and reformatted. I added comments (All towards the top) of some of your issues

cpp
#include <string>
#include <iostream>

using namespace std;

class Person
{
public:
string Name;
int SSNum;
bool Male;

enum Sex // Name of the enum. NOT a member variable name.
{
Male, // Name Conflict. You have bool Male, and enum Male
Female
};

string GetName() { return Name; };
int GetSSNum() { return SSNum; };

// You are missing a ), you have Sex (Sex) at the end... Why?
Person(string Name = "", int SSNum = 0, Sex (Sex)
:Name(), SSNum(),Sex ()) // You specified Initilizers, but you didn't
{ // specify anything to initilze them with
}
};

class Faculty: public Person
{
public:
string collegeName;
double hourlySalary;
int facultyID;

Faculty(string collegeName, double hourlySalary, int facultyID)
: collegeName(collegeName), hourlySalary(hourlySalary), facultyID(facultyID)
// You need to define the body of the constructor here


void PrintNameAndSalary()
{
cout << Name << ": " << collegeName << ", " << hourlySalary << ", " << facultyID << endl;
}
};

class Staff: public Person
{
public:
double WeeklySalary;
int YearsOfService;


Staff(double WeeklySalary, int YearsOfService) : WeeklySalary(WeeklySalary), YearsOfService(YearsOfService)
{
}


void PrintNameAndService()
{
cout <<Name << ": " << YearsOfService << ", " << WeeklySalary << ", " << endl;
}
};

class Student : public Person
{
public:
double GPA;
int Grades;

Student(double GPA = 0.0, int Grades = 0) : GPA(GPA), Grades(Grades)
{
}

void PrintNameAndGrade()
{
cout << Name << ": " << GPA << ", " << Grades << endl;
}
};

int main()
{
Student g(3.8, 'A');
g.PrintNameAndGrade();


Faculty f("Baker College", 30.0, 25);
f.PrintNameAndSalary();

Staff s(12.00, 250);
s.PrintNameAndService();

return 0;
}


Now, in your main file, you have this

CODE
#include "Faculty.h"
#include "Person.h"
#include "Staff.h"
#include "Student.h"


The problem here is that the class Faculty depends on the class Person. Yet, with the order you specified, the Person class is not defined before the Faculty class. IE, the compiler has no idea what your Person class is. To correct this, just include "Person.h" before "Faculty.h"

Once you correct all the above issues, I'll be able to further assists you.

This post has been edited by Cerolobo: 9 Jul, 2008 - 04:04 PM
User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 04:10 PM
Post #3

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

CODE

#include <string>
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;      
  
    GetName();  
    int GetSSNum();
    enum Sex{Male, Female };  
  
  Person(string (Name), int (SSNum = 0), Sex ():Name(John), SSNum(17423456),Sex (Male));
    {  
    get.Name()
    }  
  
#include <string>
using namespace std;

class Faculty: public Person  
{  
public:  
    string collegeName;  
    double hourlySalary;  
    int facultyID;  

  
    Faculty(string(collegeName), double(hourlySalary), int(facultyID))  
        : collegeName(), hourlySalary(),  
            facultyID()
    


    void PrintNameAndSalary()  
    {  
        cout << Name << ": " << collegeName << ", "  <<  hourlySalary << ", " << facultyID << endl;  
    }  
};  
  

class Staff: public Person  
{  
public:  
       double WeeklySalary;    
    int YearsOfService;


    Staff(double WeeklySalary, int YearsOfService)  
        : WeeklySalary(WeeklySalary),  
            YearsOfService(YearsOfService)  
    {  
    }  


    void PrintNameAndService()  
    {  

        cout <<Name << ": " << YearsOfService << ", "  <<  WeeklySalary << ", " << endl;  
    }  
};
class Student : public Person  
{  
public:  
    double GPA;  
    int Grades;  
       Student(double GPA = 0.0, int Grades = 0)  
       :GPA(GPA), Grades(Grades)  
  
    {  
    }  

    void PrintNameAndGrade()  
    {  

    cout << Name << ": " << GPA << ", "  <<  Grades  << endl;  
    }  

    

};  
  #include "Person.h"
#include "Faculty.h"
#include "Staff.h"
#include "Student.h"
#include <string>  
#include <iostream>


using namespace std;

int main()  
{  
Student g(3.8, 'A');
g.PrintNameAndGrade();


Faculty f("Baker College", 30.0, 25);
f.PrintNameAndSalary();

Staff s(12.00, 250);
s.PrintNameAndService();

    return 0;  
}  
CODE
undefined



I apoligize I logged into a old account and didn't want to use it so I deleted the old one. I am a little confused on the constructor.
};
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 04:36 PM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
OK, starting from the top, and working my way down

CODE
GetName();

Here, you have prototyped a function, but you did not specify the return type. You must specify the return type, for this to compile. From what I can tell, the original return type you specified was correct (string), since that's the type of your member variable, Name.




CODE
enum Sex{Male, Female };

Again, Sex is the name of the enum type you specified. For example

CODE
enum alpha
{
  A,
  B,
  C,
  D,
  // ect...
  Z
};

alpha foo;


Looking at the above code, the enum type is called alpha. IT IS NOT a variable. foo, on the other hand, is a variable. The type of foo is alpha, which is the enum listed. Going back to
CODE
enum Sex{Male, Female };

Sex is a enum type. It is not a variable. IE, you cannot access Sex like a member varible (which you attempt in the constructor).




CODE
Person(string (Name), int (SSNum = 0), Sex ():Name(John), SSNum(17423456),Sex (Male));


This one is all over the place. The proper way to declare a constructor, is as such

CODE
<Class Name>(<Variables>) : <Initilzer List>
{
  // ...
}


Or to be more verbose, here is a example
CODE

class asdf
{
  public:
    int A;
    int B;
    
    asdf(int a, int b) : A(a), B(b)
    {
    }
};


Looking at the above example, asdf is the name of the class. The constructor name is the same name of the class. For the parameters, the class takes in 2 parameters, int a and int b.

In the initializer list, the member variable A is initialized with the parameter a, that was passed in. B is initialized with parameter b.

The body is empty, since nothing else has to be done.

Going back to your code
CODE
Person(string (Name), int (SSNum = 0), Sex ():Name(John), SSNum(17423456),Sex (Male));

You did not correctly specify your parameter list. Plus, your initialize list appears to be mixed in with your parameters.

Sex () Sex (Male)
Again, Sex is NOT A VARIABLE. It is a enum type. So, you cannot access it like a variable.

Name(John)
Name is a string. John is nothing. I assume you wanted to initialize it with the string John, which will look like this

Name("John")





CODE
get.Name()

What is this supposed to do? The compiler and I have no idea. Please provide your logic behind this line.





You are also missing your closing "};" at the end of the class; however I'm willing to assume that was a copy/paste error.

In any case, try to correct the above, and I'll help your further.

User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 04:56 PM
Post #5

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

Thank you so much for explaining this to me. I think I got it this time, fingers crossed.

CODE


#include <string>
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;      
    GetName(string);  
    int GetSSNum();
    enum Sex{Male, Female};  
    Sex type;
  
    Person (string name, int num, string type): Name("John"), SSNum(164523456), type("Male")
    {  
    
    }  
  
};  



I mistakingly put the get the GetName under the wrong class. I want it in the main.


User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 05:19 PM
Post #6

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
That's a lot better, but you still have a few things you need to take care of

CODE
GetName(string);


You still haven't specified the return type.

A function is declared like so
CODE
<Return Type> <Function Name>(<Paramaters>);


So, to be more verbose

CODE
int asdf(int foo, int bar);


So, the name of the above function is asdf. It returns a int. It takes in two ints as parameters.

So,
CODE
GetName(string);

You are still missing your return type, and there really isn't a reason to take a parameter, for that function.






CODE
Person (string name, int num, string type): Name("John"), SSNum(164523456), type("Male")


OK, for the most part, this is correct.

First, you take in 3 parameters, but you don't do anything with them.

Looking at them, I assume you want to set your member variables to the values that were passed in. To do this, you need to replace the hard coded value with the ones that were passed in. This can be done is two ways

Initializer List
CODE
Person (string name, int num, string type): Name(name), SSNum(num), type(type)


or in the body of the constructor
CODE
Person (string name, int num, string type)
{
  Name = name;
  SSNum = num;
  type = type;
}


That way, the values that were passed in will be used.

There are two more issues you need to correct though. First, you should never have a parameter name that is the same as a member variable name, and the same capitalization.

Looking at the above code, Name and name are fine. As is SSNum and num. type, on the other hand, can be extremely confusing and cause you issues.

Other then that you have a mismatch between the parameter type, and the member variable type.

parameter type is a string.
member variable type is a Sex.

So, you should change the parameter type to match the member variable.





Once you do that, you should be able to do something like this

CODE
Person p("John", 123456789, Person::Male);

User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 05:34 PM
Post #7

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17


CODE

#include <string>
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;      
  
    string GetName();  
    int GetSSNum();
    enum Sex{Male, Female};  
    Sex Gender;
  
    Person (string name, int num, sex type)
    {
        Name = name;
        SSNum = num;
        Gender = type;

    }

};  


I am going to need to do this with my other classes correct?
User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 05:42 PM
Post #8

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

I have redone my code and want to repost all of it.

CODE

#include <string>
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;      
  
    string GetName();  
    int GetSSNum();
    enum Sex{Male, Female};  
    Sex Gender;
  
    Person (string name, int num, sex type)
    {
        Name = name;
        SSNum = num;
        Gender = type;

    }


};  

#include <string>
using namespace std;

class Faculty: public Person  
{  
public:  
    string collegeName;  
    double hourlySalary;  
    int facultyID;  

  
    Faculty(string cName, double money, int id)
    {
        collegeName = cName;
        hourlySalary = money;
        facultyID = id;
    }

  
    void PrintNameAndSalary()  
    {  
        cout << Name << ": " << collegeName << ", "  <<  hourlySalary << ", " << facultyID << endl;  
    }  
};  
      
using namespace std;

class Staff: public Person  
{  
public:  
       double WeeklySalary;    
    int YearsOfService;


    Staff(double wSalary, int yrsService)
    {
        WeeklySalary = wSalary;
        YearsOfService = yrsService;
    }

        
        void PrintNameAndService()  
    {  

        cout << Name << ": " << YearsOfService << ", "  <<  WeeklySalary << ", " << endl;  
    }  
};

class Student : public Person  
{  
public:  
    double GPA;  
    int Grades;  
       Student(double GPA = 0.0, int Grades = 0)  
       :GPA(GPA), Grades(Grades)  
  
    {  
    }  

    void PrintNameAndGrade()  
    {  

    cout << Name << ": " << GPA << ", "  <<  Grades  << endl;  
    }  

    

};  
  
  
#include "Person.h"
#include "Faculty.h"
#include "Staff.h"
#include "Student.h"
#include <string>  
#include <iostream>


using namespace std;

int main()  
{  
Student g(3.8, 'A');
g.PrintNameAndGrade();




Faculty f("Baker College", 30.0, 25);
f.PrintNameAndSalary();

Staff s(12.00, 250);
s.PrintNameAndService();

    return 0;  
}  

User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 05:47 PM
Post #9

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Looking at Post #3
http://www.dreamincode.net/forums/index.ph...st&p=381616

You need to correct the Faculty constructor

Other then that, you still need to add something to the Faculty, Staff, and Student constructor.


Since you didn't specify any default values for the Person class, you have to call the constructor for Person from the initializer list in the Faculty, Staff, and Student constructor.

For example, take the Staff class

CODE
Staff(double WeeklySalary, int YearsOfService)  
        : WeeklySalary(WeeklySalary),  
            YearsOfService(YearsOfService)


That's good and all; however, since you are deriving your Staff class from the Person class, you have to call a constructor for the Person class. This will look like this

CODE
Staff(double WeeklySalary, int YearsOfService)  
        : Person("John", 123456789, Person::Male), // <- Like This
          WeeklySalary(WeeklySalary),  
            YearsOfService(YearsOfService)


Now, it doesn't really make sense to hard code the values in like that. So, you'll probably want to take in each of those parameters, in the constructor of Staff, and then pass those to the Person constructor. So, it'll be something like this

CODE
Staff(string name, int num, Sex type, double WeeklySalary, int YearsOfService)  
        : Person(name, num, type), // <- Like This
          WeeklySalary(WeeklySalary),  
            YearsOfService(YearsOfService)


You'll have to do that to all your derived classes.
User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 05:58 PM
Post #10

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

After hardcoding it is giving my 19 errors. Most of the errors are this.
Error 14 error C2065: 'cout' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2005\projects\exam 1\exam 1\faculty.h 22
and
Error 8 error C2327: 'Person::string' : is not a type name, static, or enumerator c:\documents and settings\administrator\my documents\visual studio 2005\projects\exam 1\exam 1\faculty.h 8

Attached is my code
CODE


#include <string>
using namespace std;


class Person  
{  
public:  
    string Name;  
    int SSNum;      
  
    string GetName();  
    int GetSSNum();
    enum Sex{Male, Female};  
    Sex Gender;
  
    Person (string name, int num, sex type);
    {
        Name = name;
        SSNum = num;
        Gender = type;

    }


};

#include <string>
using namespace std;

class Faculty: public Person  
{  
public:  
    string collegeName;  
    double hourlySalary;  
    int facultyID;  

  
    Faculty(string cName, double money, int id):Person(name, num, type)
    {
        collegeName = cName;
        hourlySalary = money;
        facultyID = id;
    }

    void PrintNameAndSalary()  
    {  
        cout << Name << ": " << collegeName << ", "  <<  hourlySalary << ", " << facultyID << endl;  
    }  
};  
      

#include <string>
using namespace std;

class Staff: public Person  
{  
public:  
       double WeeklySalary;    
    int YearsOfService;


    Staff(string name, int num, Sex type, double wSalary, int yrsService):Person(name, num, type),
      {
        WeeklySalary = wSalary;
        YearsOfService = yrsService;
    }

        
        void PrintNameAndService()  
    {  

        cout << Name << ": " << YearsOfService << ", "  <<  WeeklySalary << ", " << endl;  
    }  
};


#include <string>

class Student : public Person  
{  
public:  
    double GPA;  
    int Grades;  
      
    Student(double gpa = 0.0, int grades = 0):Person(name, num, type)  
    {  
        GPA = gpa;
        Grades = grades;
    }  

    void PrintNameAndGrade()  
    {  

    cout << Name << ": " << GPA << ", "  <<  Grades  << endl;  
    }  

    

};


This post has been edited by snyderchip: 9 Jul, 2008 - 06:00 PM
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 06:15 PM
Post #11

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
All right, you've just got a bunch of little errors

Two errors here
CODE
Person (string name, int num, sex type);

First, "sex" is not defined. In C/C++, capitalization matters. "sex" is not a type, but "Sex" is.

Second, you need to remove the semicolon at the end. The only time you add a semicolon at the end, is if you are only defining the function, but want to define the actual body somewhere else.

For example

File: foo.h
CODE
int foo(void); // Funciton Prototype


File: foo.cpp
CODE
#include "foo.h"

  // The actual body is declared here
int foo(void)
{
  return 1;
}



Since you have the body in the header, you do not need that semicolon.





CODE
Faculty(string cName, double money, int id):Person(name, num, type)

CODE
Student(double gpa = 0.0, int grades = 0):Person(name, num, type)

You have not defined "name", "num", or "type". You have take these variables in as parameters, or just hard code some value in (not recommended)





For the cout errors, cout is declared in iostream. You need to #include <iostream> in all the files that use it





CODE
Staff(string name, int num, Sex type, double wSalary, int yrsService):Person(name, num, type),

You have a extra comma at the end. You only add a comma if another initializer comes after the initializer you just specified.
User is offlineProfile CardPM
+Quote Post

snyderchip
RE: Can't Get Progam To Complile Erro 2061
9 Jul, 2008 - 06:26 PM
Post #12

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 17

Am I calling out my parameters wrong. Because now it is giving me seven errors. Ah.. I appreciate you teaching me. I have been learning a lot through this.

Here is my updated lines
CODE

Faculty(string cName, double money, int id):Person (string name, int num, Sex type)



And then my staff code

CODE

Staff(string name, int num, Sex type, double wSalary, int yrsService):Person (string name, int num, Sex type)


Hopefully I am almost done smile.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 12:56PM