Class Concept

This is a discussion on Class Concept within the C++ Programming forums, part of the General Programming Boards category; OK, we are being introduced to classes in my C++ class. Here are my .h files: Code: // "str.h" file ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    13

    Class Concept

    OK, we are being introduced to classes in my C++ class.

    Here are my .h files:

    Code:
    // "str.h" file
    
    class Str {													//create class 'str'
    	char s[32];												//define array of 32 characters
    public:
    	Str (char *p="");										//ctor (default)
    	int operator<(Str &str);								//operator 
    	friend istream & operator>>(istream &in, Str &str);
    	friend ostream & operator<<(ostream &out, Str &str);
    };
    #endif
    
    //    "student.h" file
    
    #ifndef STUDENT_H
    #define STUDENT_H
    #include <iostream.h>
    #include "str.h"
    
    
    class student{
    	Str name;
    	int grade;
    public:
    	student(char *n="", int g=0);
    	friend istream &operator >> (istream &in, student &student);
    	friend ostream &operator << (ostream &out, student &student);
    };
    #endif
    Which looks ok.....

    My main .cpp file returns errors, I am posting just one function for brevity:

    Code:
    void Sortbygrade (student s, int count)
    	{
    		for (int i=0; i< count-1; i++) 
    			for (int j=0; j<count-1; j++)
    				if (s[j+1].grade > s[j].grade)    //error here
    					{					
    					student n;
    					n=s[j];    //error here
    					s[j]=s[j+1];    //error here
    					s[j+1]=n;    //error here
    					};
    	}
    My errors are as follows:

    C:\Cpp1.cpp(54) : error C2676: binary '[' : 'class student' does not define this operator or a conversion to a type acceptable to the predefined operator

    Now, where am I going wrong on the operator? I am sorting a list of students that will be read from a file.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    s is a variable of type student, not an array of or pointer to it. I believe you meant to pass a pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    In addition to what CornedBee said:

    You have an unneccessary semicolon at the end of the if block of code.

    You are also using a deprecated version of the iostream header. The old one was iostream.h the new one is just simply iostream without the .h extension. Changing this will require you to become familiar with the concept of namespaces (i.e. the std:: part in the code sample below).

    Also, unless the Sortbygrade function is a friend of the student class, then you won't be able to access the grade member since it is private. You should probably create an operator> member for your student class that compares the grade data members so you can just say something like if( s[j+1] > s[j] ) then you wouldn't have to worry about public/private access.

    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    #include <iostream>
    #include "str.h"
    
    
    class student
    {
        Str name;
        int grade;
    public:
        student(char *n="", int g=0);
        friend std::istream &operator >> (std::istream &in, student &student);
        friend std::ostream &operator << (std::ostream &out, student &student);
        inline bool operator> (const student& rhs) const
        {
            return grade > rhs.grade;
        }
    };
    #endif
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    Quote Originally Posted by CornedBee
    s is a variable of type student, not an array of or pointer to it. I believe you meant to pass a pointer.
    If I am not creating an array, how do I compare two values of the same type in the class?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What do you mean?

    If you want to sort students, you have to have more than one. You only pass a single student to the sorting function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    void Sortbygrade (student * s, int count)

    s can now be used as an array of students. Usually count will be the actual number of students in s, but you can use count however you want.
    _____________________________________

    return grade > rhs.grade;

    I'd be concerned that even that won't work as the > operator is being called on lhs (or *this or the current object or whatever you want to call it) object which allows it to access the private members of lhs but it can't access private members of rhs. I think you'll need to use

    return grade > rhs.getGrade();

    where getGrade() is a public accessor function of student class.

    Likewise, if a public accessor to the grade member of the student class is added, and if you didn't overload the > operator for the student class, then the Sortbygrade() function would need to use the accessor methods for both student objects.

    if (s[j+1].getGrade() > s[j].getGrade())
    ________________________________________

    Straying even a little further off the initial error problem---the constructors posted are default only because there are no other constructors. In usual parlance the default constructor has no parameters.

    Also, if you haven't shortened you post by leaving out the assignment operators and copy constructors, then, given the Str class has an array (actually a C style string) as a member, I would be very concerned about allowing the compiler to define a copy constructor or assignment operator for the class, as it will if you don't declare and define them explicitly. Since you have a Str obect as a member of the student class, the same concern holds for the student class. It becomes pertinent when trying to assign one student to another in the Sortbygrade() function. This function may well cause run time errors if not compile time errors with a compiler defined assignment operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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