Cant understand weired problem...

This is a discussion on Cant understand weired problem... within the C++ Programming forums, part of the General Programming Boards category; This compiles fine Code: #include <iostream> #include <string.h> //#include "Course.h" #include "Student.h" using namespace std; Student *sa[3]; int main(){ sa[0] ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Cant understand weired problem...

    This compiles fine

    Code:
    #include <iostream>
    #include <string.h>
    //#include "Course.h"
    #include "Student.h"
    using namespace std;
    
    Student *sa[3];
    
    int main(){   
        sa[0] = new Student("James");
        sa[1] = new Student("Jason");
    }
    But when I un comment the 3rd line
    Code:
    //#include "Course.h"
    g++ gives me...

    Main.cpp:9: error: expected constructor, destructor, or type conversion before '*' token
    Main.cpp:9: error: expected `,' or `;' before '*' token
    Main.cpp: In function `int main()':
    Main.cpp:14: error: `sa' undeclared (first use this function)
    Main.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)
    Main.cpp:14: error: `Student' has not been declared
    Main.cpp:15: error: `Student' has not been declared

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    > Student *sa[3];
    If you want an array, just type Student sa[3] and leave out the *
    I believe that's actually a pointer to an array of Students.
    And then you can use this:

    sa[0] = Student("James");
    sa[1] = Student("Jason");

    If you wanted an array on the heap, you need to declare a pointer to a Student and allocate with new[]

    Student *sa;
    sa = new Student[3];

    and now you can use array indices like in the first example.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Try including Course.h after Student.h. That might do something, otherwise, I'd like to see what's in those libraries.
    Sent from my iPadģ

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    8
    This sounds like a missing-semi-colon-at-end-of-class-declaration problem.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by DrkMatter
    This sounds like a missing-semi-colon-at-end-of-class-declaration problem.
    Likely. I'd say check the last line of Course.h. A missing semicolon there would f-up the defintion of your Student class which is causing the errors in your main.cpp
    Sent from my iPadģ

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Or an unmatched #ifndef / #endif

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Quote Originally Posted by DrkMatter
    This sounds like a missing-semi-colon-at-end-of-class-declaration problem.
    Im not sure if it means anything but class's compile fine on their own.

    Quote Originally Posted by SlyMaelstrom
    Try including Course.h after Student.h. That might do something, otherwise, I'd like to see what's in those libraries.
    That actually works. But now when I do..

    Code:
    #include <iostream>
    #include <string.h>
    #include "Student.h"
    #include "Course.h"
    
    using namespace std;
    
    int main(void);
    Student *sa[3];
    Student *ca[3];
    
    int main(){
        
        sa[0] = new Student("James");
        sa[1] = new Student("Jason");
        
        ca[0] = new Course("Math");
        
        sa[0]->enrollInCourse(2);
    This line
    Code:
    ca[0] = new Course("Math");
    Says - Main.cpp:17: error: `Course' has not been declared
    Although this may be me not using pointer propely or my constructor is wrong..

    In Course.h
    Code:
    inline Course::Course(string cn) {
        CourseName = cn;
        NextCourseNo++;
        CourseNo = NextCourseNo; 
    }

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Ahh never mind... I found it I think

    I had

    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    in Course.h
    Thats what I get for copy and pasteing

    Sorry for all the trouble.. And thanks
    Last edited by (Slith++); 07-11-2006 at 10:35 PM.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I noticed you didn't listen to Salem (I wouldn't recommend it), in

    Student *sa[3];
    Student *ca[3];

    instead of:

    Student sa[3];
    Student ca[3];

    Unless you need it as a pointer, there's no need for it to be (and as he said, you could get rid of the new and just use a regular array).

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    I needed if new. But if I dont need new...

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> I needed if new. But if I dont need new...

    Em, could you repeat that. Did you think you needed new? You shouldn't.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> #include <string.h>
    You're using the string class, so the correct header is <string>.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Quote Originally Posted by twomers
    >> I needed if new. But if I dont need new...

    Em, could you repeat that. Did you think you needed new? You shouldn't.
    Sorry, typo.

    Yes I thought I needed the 'new' keyword.
    I guess I dont understand pointers then... Or maybe Im mixed up in this whole 'new' keyword thing.

    If this is what you meant...
    Code:
    Student sa[3];
    Course ca[3];
    
    int main(){
        
        sa[0] = Student("James");
        sa[1] = Student("Jason");
        
        ca[0] = Course("Math");
    I get... (9 and 10 being the first 2 lines)

    Main.cpp:9: error: no matching function for call to `Student::Student()'
    Student.h:9: note: candidates are: Student::Student(const Student&)
    Student.h:25: note: Student::Student(std::string)
    Main.cpp:10: error: no matching function for call to `Course::Course()'
    Course.h:9: note: candidates are: Course::Course(const Course&)
    Course.h:24: note: Course::Course(std::string)

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    It looks like to me that you messed up your constructor declarations. You really ought to show us the parts of the header files that you get errors for since it looks like we still haven't gotten to the bottom of it, like Sly said.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Course.h

    Code:
    #ifndef COURSE_H
    #define COURSE_H
    
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    class Course{
        private:
            string CourseName;
            int CourseNo;
            vector<int> EnrolledStudents;
            static int NextCourseNo;
            const int MaxNoPlaces;
            int FilledPlaces;
        public:
            Course(string cn, int MaxNoPlaces);
            ~Course();
            bool enrollInStudent(int StudentNo);
            int getMaxNoPlaces();
    };
    
    inline Course::Course(string cn, int MaxNoPlaces = 25) : MaxNoPlaces(MaxNoPlaces){
        MaxNoPlaces = MaxNoPlaces;
        CourseName = cn;
        NextCourseNo++;
        CourseNo = NextCourseNo; 
    }
    
    inline int Course::getMaxNoPlaces(){
        return MaxNoPlaces;
    }
    #endif
    Student.h
    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    class Student{
        private:
            int StudentNo;
            static int NextStudentNo;
            string StudentName;
            vector<int> EnrolledCorses;
            
        public:
            void displayEnrolledCorses();
            Student(string sn);
            ~Student(); 
            string getStudentName();
            int getStudentNo(); 
            void enrollInCourse(int courseNo);  
    };
    
    inline Student::Student(string sn){
        StudentName = sn;
        NextStudentNo++;
        StudentNo = NextStudentNo;        
    }
    
    inline Student::~Student(){}
    
    inline string Student::getStudentName(){
        return StudentName;
    }
    
    inline int Student::getStudentNo(){
        return StudentNo;
    }
    #endif

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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