-
Classes Compiler Errors
Code:
#include <iostream.h>
#include <conio.h>
class Student
{
public:
Student();//Constructor
~Student();//Deconstructor
void SetName(char fName, char lName);//Sets student's name.
void SetAge(int age);//Sets student's age.
void SetClass(int inGrade, int inClass);//Sets student's name.
char GetName();//Returns student's name.
int GetAge();//Returns student's age.
int GetClass();//Returns students class (sGrade sClass)
protected:
char FirstName[10];//Student's first name.
char LastName[10];//Student's last name.
int sAge;//Age of student.
int sGrade;//Student's grade (1-12).
int sClass;//Student's class (1/2).
};
Student::Student()
{
}
Student::~Student()
{
}
void Student::SetName(char fName, char lName)
{
FirstName = fName;
LastName = lName;
}
void Student::SetAge(int age)
{
sAge = age;
}
void Student::SetClass(int inGrade, int inClass)
{
sClass = inClass;
sGrade = inGrade;
}
char Student::ReturnName()
{
return sName;
}
int Student::ReturnAge()
{
return sAge;
}
int Student::GetClass
{
return sGrade, " ", sClass;
}
int main()
{
return 0;
}
Loads of errors in DevC++. I'm totally new to classes and the error reports aren't very helpful. There seem to be a few centered on the char ReturnName() function.
-
Neither of your return functions are declared as prototypes under public: with the rest of your functions.
A bigger problem is that ReturnName() returns a char, where it seems you want to return a string, or at least a char[], unless the name is only one letter long...
-
>FirstName = fName;
>LastName = lName;
Both these two are char arrays. You can't directly assign them values in this manner. Also, your parameters on this function are single chars:
>void Student::SetName(char fName, char lName)
so they will hold only one character.
You could do something like
Code:
void Student::SetName(char *fName, char *lName)
{
strcpy(FirstName, fName);
strcpy(LastName , lName);
}
but there are better ways to hold strings in c++.
The functions Student::ReturnName() and Student::ReturnAge() are no defined in the class. You need to add them in.
And this:
>int Student::GetClass
is missing the () from the end. As in:
>int Student::GetClass()
-
Code:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
class Student
{
public:
Student(); //Constructor
~Student(); //Deconstructor
void SetName(string fName, string lName); //Sets student's name.
void SetAge(int age); //Sets student's age.
void SetClass(int inGrade, int inClass);//Sets student's name.
string GetName(); //Returns student's name.
int GetAge(); //Returns student's age.
int GetClass(); //Returns students class (sGrade sClass)
void PrintName();
private:
string FirstName; //Student's first name.
string LastName; //Student's last name.
int sAge; //Age of student.
int sGrade; //Student's grade (1-12).
int sClass; //Student's class (1/2).
};
Student::Student()
{
}
Student::~Student()
{
}
void Student::SetName(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
void Student::SetAge(int age)
{
sAge = age;
}
void Student::SetClass(int inGrade, int inClass)
{
sClass = inClass;
sGrade = inGrade;
}
string Student::GetName()
{
string temp;
temp = temp + FirstName + " " + LastName;
return temp;
}
int Student::GetAge()
{
return sAge;
}
int Student::GetClass()
{
return sClass;
}
void Student::PrintName()
{
cout << FirstName << " " << LastName << endl;
}
int main()
{
Student A;
A.SetName("First","Last");
A.SetAge(30);
A.SetClass(10, 2);
A.PrintName();
return 0;
}
As the other mentioned there were many errors. I renamed your functions ( Get and Return) , changed to strings and changed you #includes.
Also had main do a lil something to test.
The error messages clearly said what you have to do.
-
Well, does it work now? You didn't post the error messages.
Anyway, you might want to consider a constructor that takes the parameters, lastName, firstName, age, etc, so you can declare them all in one statement.
Some people would have the constructor just set the variables directly, while others would have the constructor call the Set functions. It's up to you.
-
Stupid mistakes, I know... but it was 02:00 when I posted it... :)