classes and objects

Write your question here.

ave tried to compile my code but the compiler keeps saying error: no matching function for call to 'GradeBook::GradeBook(const char [41])
 
  //GradeBook class demonstrate after separating its interface from implementation #include <iostream> #include "GradeBook.h" using namespace std; int main() { //create 2 gradeBook object (you can create more than two) // Testing for the 25th cut the string thing. GradeBook gradeBook1 ( "CS101 Introduction to programming in c++" ); GradeBook gradeBook2 ( "CS102 C++ Data structures" ); GradeBook gradeInstructor ( "David J. Malan" ); // display gradebooks member function cout << "\nGradeBook1's initial courseName is: " << gradeBook1.getCourseName() << "\nGradeBook2's initial courseName is: " << gradeBook2.getCourseName() << "\nInstructors Name: " << gradeInstructor.getInstructorsName() << endl; //Modifying the functions GradeBooks courseName (with a valid-length string) gradeBook1.setCourseName( "CS101 C++ programming" ); gradeInstructor.setInstructorsName ( "David J. Malan" ); //Displaying each gradeBook's courseName cout << "\nGradeBook1's course Name : " << gradeBook1.getCourseName() << "\nGradeBook2's course Name: \n\n" << gradeBook2.getCourseName() << gradeInstructor.displayMessage() << "Instructors Name: " << gradeInstructor.getInstructorsName << endl; }  
Please show the definition of class GradeBook.
#ifndef GRADEBOOK_H_INCLUDED
#define GRADEBOOK_H_INCLUDED
//GradeBook's interface without exposing the implementation
//actually hiding its function Prototype
#include <iostream>
#include <string>


using namespace std;


class GradeBook
{
public:
//construction initializes courseName and instructors name with string supplied as argument
GradeBook( string name, string prof )
{
setCourseName( name );
setInstructorsName( prof );

} //end GradeBook Constructor


void setCourseName( string name )
{
if( name.length() <= 25 ) // if name has 25 or fewer characters
courseName = name; // Store the course name in the object


if ( name.length() > 25 ) // if name has more than 25 characters
{
courseName = name.substr( 0, 25 ); // Start at 0 and end on the 25th string letter

cout << "Name \"" << name << "\" exceeds maximum length (25). \n"
<< "Limiting courseName to first 25 characters. \n" << endl;
} //Function that sets the course name
}//end if statement

string getCourseName()
{
return courseName;
} //Function that returns the course name

// function that collects instructors name
void setInstructorsName ( string prof )
{
InstructorsName = prof;
}

// Function that returns instructors name
string getInstructorsName()
{
return InstructorName;
}

void displayMessage()
{
cout << "Welcome to GradeBook for\n" << getCourseName() << "!" << endl;
} // Function that contains the message

private:
string courseName;

};


#endif // GRADEBOOK_H_INCLUDED
fix your first post

> no matching function for call to 'GradeBook::GradeBook(const char [41])
keep reading
1
2
note: candidate constructor not viable: requires 2 arguments, but 1 was provided
||   GradeBook(string name, string prof) {

that's the only constructor that you've defined, it ask for two strings but you've only provided one
pls elaborate more ...Hw do u mean two strings...
Your class provides only one constructor:
1
2
3
4
5
6
7
class GradeBook
{
public:
  GradeBook( string, string );

  // other members
};

That constructor requires exactly two arguments.

You attempt to create objects with one argument to constructor:
1
2
3
GradeBook gradeBook1 ( "CS101 Introduction to programming in c++" );
GradeBook gradeBook2 ( "CS102 C++ Data structures" );
GradeBook gradeInstructor ( "David J. Malan" );

What is the instructor's name of these three books?
tnx a lot ...the instructors name is David malan but how do I provide another constructor am still not cleared ..
If you wrote one constructor (which takes two parameters: name of cource and name of instructor) then you surely can write a second constructor that takes only the name of course as parameter and sets the name of instructor to some default value.
so ur saying I should give separate constructors to each of the functions?? am I right or you hint me. tnx a lot
> I should give separate constructors to each of the functions??
¿what?

1
2
3
4
5
6
7
8
9
 class GradeBook {
public:
  GradeBook(string name, string prof); // ask for two strings
};

int main() {
  // gives two strings
  GradeBook computer_science("Design Patterns", "GoF");
}


If you want to write GradeBook foo("bar"); when you want to pass just one string
then you need to provide a GradeBook(string name); constructor which only takes just one single parameter


And fix your first post.
so ur suggesting I provide separate constructors for each function... or write me the appropriate code so I understand... am still a novice
What do you mean by "constructors for each function"?
constructors for each member function name of course and instructors name ..write me the correct code so I'll understand.. pls I still don't get u.
Lets look at your class:
1
2
3
4
5
6
7
8
9
10
11
12
class GradeBook
{
public:
  GradeBook( string name, string prof );
  void setCourseName( string name );
  string getCourseName();
  void setInstructorsName( string prof );
  string getInstructorsName();
  void displayMessage();
private:
  string courseName;
};

It has one constructor and five member functions: setCourseName, getCourseName, setInstructorsName, getInstructorsName, and displayMessage. Are those five member functions the thing you call "member functions"?

However, we are not yet at stage to call member functions. We are about to create one GradeBook object. What information must a freshly created GradeBook have? That is your decision.

You have written:
1
2
3
4
5
6
class GradeBook
{
public:
  GradeBook( string name, string prof );
  // ...
};

That says that user has to give two strings, "name" and "prof", for a new GradeBook object.

You said that Malan is the "prof". Therefore, create the GradeBooks as they want to be created:
1
2
3
GradeBook gradeBook1 ( "CS101 Introduction to programming in c++", "David J. Malan" );
GradeBook gradeBook2 ( "CS102 C++ Data structures", "David J. Malan" );
GradeBook gradeInstructor ( "David J. Malan", "complete idiot?" );


If you don't wan't to give new GradeBooks more than one name, then create a constructor that takes only one string.
1
2
3
4
5
6
7
class GradeBook
{
public:
  GradeBook( string name );
  GradeBook( string name, string prof );
  // ...
};

Then you can create books with one argument.
I think am getting it now but why the "complete idiot" ?? so I must give all objects of gradebook a different identifier righ??
Last edited on
Is it more strange than the fact that that GradeBook is about course "David J. Malan"?

Your "display gradebooks member function" does not treat all books equally:
1
2
cout << "GradeBook1's initial courseName is: " << gradeBook1.getCourseName() << '\n';
cout << "Instructors Name: " << gradeInstructor.getInstructorsName() << '\n';

IMHO, it should:
1
2
3
4
cout << "GradeBook1's initial courseName is: " << gradeBook1.getCourseName()
     << " and Instructors Name: " << gradeBook1.getInstructorsName() << '\n';
cout << "GradeInstrutor's initial courseName is: " << gradeInstructor.getCourseName()
     << " and Instructors Name: " << gradeInstructor.getInstructorsName() << '\n';


This is a question about logic. All gradebooks should be gradebooks and therefore used as gradebooks.
Is the instructor a gradebook? Can instructor's name be used as a course?
(I would not be surprised, if GradeBook maybe("Trump", "Trump"); but I digress.)
If every gradebook names an instructor, why do you show her name from one book only (but don't show the name of the course of that book)?


Different identifier? Do you mean "value" when you say "identifier"?
1
2
int x = 7;
int y = 7;

The x and y are separate integers. It is ok that they have same value.
Similarly, one could have GradeBooks with identical value.
However, the logic of the problem that you are solving might say that each book must be unique. That is a property of the problem, not of the programming language.
I just want to add an instructor's name to my grade book program, how do I insert and call a function that displays the instructors name to my program its still telling me no match for function call.
Last edited on
Does every gradebook have an instructor?
The code that you did post:
1
2
3
4
5
class GradeBook
{
private:
  string courseName;
};

... says that none of them has.

If you want a book to store instructor's name, then make it to do so.
If you want to store instructor's name outside of all books, then do so.
sorry for pesting you I'll figure it out myself ...tnx anyway
Topic archived. No new replies allowed.