469,097 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

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

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
1 1353
Banfa
9,061 Expert Mod 8TB
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.

Similar topics

4 posts views Thread by Leslaw Bieniasz | last post: by
reply views Thread by chris | last post: by
reply views Thread by Pascal Costanza | last post: by
13 posts views Thread by xian_hong2046 | last post: by
1 post views Thread by None | last post: by
11 posts views Thread by toton | last post: by
1 post views Thread by philin007 | last post: by
reply views Thread by daylond | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.