Classes Compiler Errors

This is a discussion on Classes Compiler Errors within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    taiDasher
    Guest

    Question 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.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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...
    Truth is a malleable commodity - Dick Cheney

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest
    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.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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.
    Truth is a malleable commodity - Dick Cheney

  6. #6
    taiDasher
    Guest
    Stupid mistakes, I know... but it was 02:00 when I posted it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Error with Classes
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 12-02-2008, 09:36 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Help with a couple compiler errors
    By s_ny33 in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 08:46 PM
  4. Errors compiling classes with VC++ 6.0
    By xErath in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2004, 07:58 AM
  5. Pointers, Classes, and Errors o my!
    By Scottc1988 in forum C++ Programming
    Replies: 12
    Last Post: 03-13-2003, 09:14 PM

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