By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Dynamic Creation of objects... and some errors i cant figure out

P: 94
Any help here is appreciated folks.

First in my Person class the comments = errors visual basics is giving me and I am not sure why. Also when i try and set up my array of pointers to Student class I get the error that is in the comment. This is really bothering me as I spent the last hour and a half with a classmate working on this and we can not figure out whats up.

Expand|Select|Wrap|Line Numbers
  1. class Person
  2. {
  3. public:
  4.     Person(int sinNumber, char studentName[]);
  5.     int compareByBirthday(Student & s);            //syntax error identifier Student
  6.     int compareByEmailAddress(Student & s);        //syntax error identifier Student
  7.     int compareByMajor(Student & s);            //syntax error identifier Student
  8.     int compareByName(Student & s);                //syntax error identifier Student
  9.     int compareBySin(Student & s);                //syntax error identifier Student
  10. private:
  11.     int sin;
  12.     char name[30];
  13. };
  14. Person::Person(int sinNumber, char studentName[])
  15. {
  16.     sin = sinNumber;
  17.     strcpy (name, studentName);
  18. }
  19. int Person::compareByBirthday()
  20. {
  21.     return 0;
  22. }
  23. int Person::compareByEmailAddress()
  24. {
  25.     return 0;
  26. }
  27. int Person::compareByMajor()
  28. {
  29.     return 0;
  30. }
  31. int Person::compareByName()
  32. {
  33.     return 0;
  34. }
  35. int Person::compareBySin()
  36. {
  37.     return 0;
  38. }
  39.  
  40. class Student : Person
  41. {
  42. public:
  43.     Student(int sinNumber, char studentName[], int bDay, char email[], char mjr[]);
  44. private:
  45.     int birthday;
  46.     char emailAddress[30];
  47.     char major[10];
  48. };
  49. Student::Student(int sinNumber, char studentName[], int bDay, char email[], char mjr[]) : Person(sinNumber, studentName)
  50. {
  51.     birthday = bDay;
  52.     strcpy (emailAddress, email);
  53.     strcpy (major, mjr);
  54. }
  55.  
  56.  
  57. int _tmain(int argc, _TCHAR* argv[])
  58. {
  59.     Student *stu = new Student[6];    //No appropriate default constuctor available
  60.     char recordIn[255];
  61.     ifstream infile;
  62.     infile.open("studentFile.txt");
  63.     if(!infile)
  64.     {
  65.         cout<<"Cannot open file."<<endl;
  66.     }
  67.     else 
  68.         while(infile)
  69.         {
  70.             infile.getline(recordIn, 255);
  71.             cout<<" "<<recordIn<<endl;
  72.         }
  73.     return 0;
  74. }
  75.  
Here is an overview of my project.
Using 2 classes (with inheritance) compare students (these functions in my program were given by prof). My main program is supposed to read in a .txt file and dynamically create my student objects (txt file contains sin#, birthday, etc..) and the user should be able to sort by each of the attributes found in the txt file. I can only assure you that I have spent the better part of my day trying to write this dam thing and have failed miserably. If someone could please explain to me why I am getting these errors and a possible point in the right direction I would appreciate it.

Also I am unclear about the whole dynamic creation of the Student objects. I tried reading the file into an array and creating it but that doesn't seem to be possible.
Jan 27 '07 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. // Declaration
  2.     int compareByBirthday(Student & s);    
  3.  
  4. // Definition
  5. int Person::compareByBirthday()
  6. {
  7.     return 0;
  8. }
  9.  
Student sub-class person, it then seems odd, wrong even, that person should have functions that take student as a parameter. If the method is declared in Person then it should take a reference to a Person not a student. If you need the data declared in Student then the method should be a member of Student not Person.

Also note from quote code that your declaration and definition of compareByBirthday do not match (and similarly for all the other compare functions).

And finally, birthday and email address appear to be members of student, however I think that all people not just students have birthdays and email addresses, the only additional piece of data that a Student has is their major.

major and compareByMajor should be in Student, the rest should be in Person.

Reading the file shouldn't be too hard, however it will be easier if you create a sensible constructor on Student and Person that allows you to pass initialisation values into it.

Don't use a fixed size array, use a STL list or one of the other STL containers.
Jan 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.