Help Code won't compile

B

B Williams

I have been working with this code for a better part of the day and I can't
figure out where I am making a mistake. I can only imagine it is when I
declare multiple paramaters on the constructor because the program compiles
with just one parameter. Can someone look at this and tell me where I made
my error?

This is the error I get while trying to compile.

error C2664: 'GradeBook::GradeBook(const GradeBook &)' : cannot convert
parameter 1 from 'const char [38]' to 'const GradeBook &'

Reason: cannot convert from 'const char [38]' to 'const GradeBook'

No constructor could take the source type, or constructor overload
resolution was ambiguous


Thanks

// GradeBook.cpp

// GradeBook member-function definitions. This file contains

// implementations of the member functions prototyped in GradeBook.h.

#include <iostream>

using std::cout;

using std::endl;

#include "GradeBook.h" // include definition of class GradeBook

// constructor initializes courseName and instructorName with string
supplied as argument

GradeBook::GradeBook(string name, string instructor)

{

setCourseName( name ); // call set function to initialize courseName

setInstructorName( instructor );// call set function to initialize
instructorName

} // end GradeBook constructor

// function to set the course name

void GradeBook::setCourseName( string name )

{

courseName = name; // store the course name in the object

} // end function setCourseName

// function to get the course name

string GradeBook::getCourseName()

{

return courseName; // return object's courseName

} // end function getCourseName

// function to set the instructor name

void GradeBook::setInstructorName( string instructor )

{

instructorName = instructor; // store the instructor name in the object

} // end function setInstructorName

// function to get the instructor name

string GradeBook::getInstructorName()

{

return instructorName; // return object's instructorName

} // end function getInstructorName

// display a welcome message to the GradeBook user

void GradeBook::displayMessage()

{

// call getCourseName and getInstructorName to get the courseName and
instructorName

cout << "Welcome to the grade book for\n" << getCourseName()

<< "!" << endl;

cout << "This course is presented by:\n" << getInstructorName()

<< "!" << endl;

} // end function displayMessage



This is the header file

// GradeBook.h

// GradeBook class definition. This file presents GradeBook's public

// interface without revealing the implementations of GradeBook's member

// functions, which are defined in GradeBook.cpp.

#include <string> // class GradeBook uses C++ standard string class

using std::string;

// GradeBook class definition

class GradeBook

{

public:

GradeBook( string, string ); // constructor that initializes courseName and
instructorName

void setCourseName( string ); // function that sets the course name

string getCourseName(); // function that gets the course name

void setInstructorName( string ); // function that sets the Instructor name

string getInstructorName(); // function that gets the Instructor name

void displayMessage(); // function that displays a welcome message

private:

string courseName; // course name for this GradeBook

string instructorName; // Instructor name for this GradeBook

}; // end class GradeBook
 
I

Ian Collins

B said:
I have been working with this code for a better part of the day and I can't
figure out where I am making a mistake. I can only imagine it is when I
declare multiple paramaters on the constructor because the program compiles
with just one parameter. Can someone look at this and tell me where I made
my error?

This is the error I get while trying to compile.

error C2664: 'GradeBook::GradeBook(const GradeBook &)' : cannot convert
parameter 1 from 'const char [38]' to 'const GradeBook &'

Reason: cannot convert from 'const char [38]' to 'const GradeBook'

No constructor could take the source type, or constructor overload
resolution was ambiguous
Somewhere you are attempting to copy a GradeBook object and you don't
have a copy constructor.

A couple points of style:

Way way too many superfluous comments. Just give things meaningful
names and let the code tell its own story.

Never ever put a using directive in a header.
 
V

Victor Bazarov

B said:
I have been working with this code for a better part of the day and I
can't figure out where I am making a mistake. I can only imagine it
is when I declare multiple paramaters on the constructor because the
program compiles with just one parameter. Can someone look at this
and tell me where I made my error?

This is the error I get while trying to compile.

error C2664: 'GradeBook::GradeBook(const GradeBook &)' : cannot
convert parameter 1 from 'const char [38]' to 'const GradeBook &'

Reason: cannot convert from 'const char [38]' to 'const GradeBook'

Which line of the code you have posted does this message relate to?
No constructor could take the source type, or constructor overload
resolution was ambiguous


Thanks

// GradeBook.cpp

// GradeBook member-function definitions. This file contains

// implementations of the member functions prototyped in GradeBook.h.

#include <iostream>

using std::cout;

using std::endl;

#include "GradeBook.h" // include definition of class GradeBook

// constructor initializes courseName and instructorName with string
supplied as argument

GradeBook::GradeBook(string name, string instructor)

{

setCourseName( name ); // call set function to initialize courseName

setInstructorName( instructor );// call set function to initialize
instructorName

} // end GradeBook constructor

// function to set the course name

void GradeBook::setCourseName( string name )

{

courseName = name; // store the course name in the object

} // end function setCourseName

// function to get the course name

string GradeBook::getCourseName()

{

return courseName; // return object's courseName

} // end function getCourseName

// function to set the instructor name

void GradeBook::setInstructorName( string instructor )

{

instructorName = instructor; // store the instructor name in the
object
} // end function setInstructorName

// function to get the instructor name

string GradeBook::getInstructorName()

{

return instructorName; // return object's instructorName

} // end function getInstructorName

// display a welcome message to the GradeBook user

void GradeBook::displayMessage()

{

// call getCourseName and getInstructorName to get the courseName and
instructorName

cout << "Welcome to the grade book for\n" << getCourseName()

<< "!" << endl;

cout << "This course is presented by:\n" << getInstructorName()

<< "!" << endl;

} // end function displayMessage



This is the header file

// GradeBook.h

// GradeBook class definition. This file presents GradeBook's public

// interface without revealing the implementations of GradeBook's
member
// functions, which are defined in GradeBook.cpp.

#include <string> // class GradeBook uses C++ standard string class

using std::string;

// GradeBook class definition

class GradeBook

{

public:

GradeBook( string, string ); // constructor that initializes
courseName and instructorName

void setCourseName( string ); // function that sets the course name

string getCourseName(); // function that gets the course name

void setInstructorName( string ); // function that sets the
Instructor name
string getInstructorName(); // function that gets the Instructor name

void displayMessage(); // function that displays a welcome message

private:

string courseName; // course name for this GradeBook

string instructorName; // Instructor name for this GradeBook

}; // end class GradeBook

V
 
L

Luke Meyers

B said:
I have been working with this code for a better part of the day and I can't
figure out where I am making a mistake. I can only imagine it is when I
declare multiple paramaters on the constructor because the program compiles
with just one parameter. Can someone look at this and tell me where I made
my error?

Please read the FAQ for this newsgroup, specifically the section on how
to post. You need to post complete, compilable, minimal code which
illustrates the problem. The code you posted does not even include a
main() function. I took the time to read through it in spite of this
and do not think you've posted the line of code which is causing the
error. It would be helpful if you identified which line the error was
talking about, too.

Also, I concur with the other poster regarding your use of comments --
a comment which does nothing more than state something like "call
getFoo to get the Foo" is worse than useless. I'll further add that
having getFoo() and setFoo() for your private Foo member is essentially
no better in terms of design than just making that member public, but
you'll have time to appreciate such design subtleties after you've
gained more mastery of the language fundamentals.

Luke
 
B

B Williams

This is the code that contains the main. The error is occuring at both of
these lines.

GradeBook courseName( "CS101 Introduction to C++ Programming" );

GradeBook instructorName( "Mr Johnson");


#include <iostream>

using std::cout;

using std::endl;

#include "GradeBook.h" // include definition of class GradeBook

// function main begins program execution

int main()

{

// create two GradeBook objects

GradeBook courseName( "CS101 Introduction to C++ Programming" );

GradeBook instructorName( "Mr Johnson");

// display initial value of courseName for each GradeBook

cout << "gradeBook1 created for course: " << courseName.getCourseName()

<< "\nThe instructor for this course is: " <<
instructorName.getInstructorName()

<< endl;

return 0; // indicate successful termination

} // end main

Victor Bazarov said:
B said:
I have been working with this code for a better part of the day and I
can't figure out where I am making a mistake. I can only imagine it
is when I declare multiple paramaters on the constructor because the
program compiles with just one parameter. Can someone look at this
and tell me where I made my error?

This is the error I get while trying to compile.

error C2664: 'GradeBook::GradeBook(const GradeBook &)' : cannot
convert parameter 1 from 'const char [38]' to 'const GradeBook &'

Reason: cannot convert from 'const char [38]' to 'const GradeBook'

Which line of the code you have posted does this message relate to?
No constructor could take the source type, or constructor overload
resolution was ambiguous


Thanks

// GradeBook.cpp

// GradeBook member-function definitions. This file contains

// implementations of the member functions prototyped in GradeBook.h.

#include <iostream>

using std::cout;

using std::endl;

#include "GradeBook.h" // include definition of class GradeBook

// constructor initializes courseName and instructorName with string
supplied as argument

GradeBook::GradeBook(string name, string instructor)

{

setCourseName( name ); // call set function to initialize courseName

setInstructorName( instructor );// call set function to initialize
instructorName

} // end GradeBook constructor

// function to set the course name

void GradeBook::setCourseName( string name )

{

courseName = name; // store the course name in the object

} // end function setCourseName

// function to get the course name

string GradeBook::getCourseName()

{

return courseName; // return object's courseName

} // end function getCourseName

// function to set the instructor name

void GradeBook::setInstructorName( string instructor )

{

instructorName = instructor; // store the instructor name in the
object
} // end function setInstructorName

// function to get the instructor name

string GradeBook::getInstructorName()

{

return instructorName; // return object's instructorName

} // end function getInstructorName

// display a welcome message to the GradeBook user

void GradeBook::displayMessage()

{

// call getCourseName and getInstructorName to get the courseName and
instructorName

cout << "Welcome to the grade book for\n" << getCourseName()

<< "!" << endl;

cout << "This course is presented by:\n" << getInstructorName()

<< "!" << endl;

} // end function displayMessage



This is the header file

// GradeBook.h

// GradeBook class definition. This file presents GradeBook's public

// interface without revealing the implementations of GradeBook's
member
// functions, which are defined in GradeBook.cpp.

#include <string> // class GradeBook uses C++ standard string class

using std::string;

// GradeBook class definition

class GradeBook

{

public:

GradeBook( string, string ); // constructor that initializes
courseName and instructorName

void setCourseName( string ); // function that sets the course name

string getCourseName(); // function that gets the course name

void setInstructorName( string ); // function that sets the
Instructor name
string getInstructorName(); // function that gets the Instructor name

void displayMessage(); // function that displays a welcome message

private:

string courseName; // course name for this GradeBook

string instructorName; // Instructor name for this GradeBook

}; // end class GradeBook

V
 
B

B Williams

Thanks guys,
I included the code that included the main. What is really confusing me is
that if I remove all lines refering to the instructor, it compiles fine.

Thanks again for your assistance.
 
V

Victor Bazarov

B said:
This is the code that contains the main. The error is occuring at
both of these lines.

GradeBook courseName( "CS101 Introduction to C++ Programming" );

GradeBook instructorName( "Mr Johnson");
[..]

Your class 'GradeBook' has a constructor that takes 2 strings. You
only provide a single string literal in both of those cases.

V
 
T

Thomas J. Gritzan

B said:
This is the code that contains the main. The error is occuring at both of
these lines.

GradeBook courseName( "CS101 Introduction to C++ Programming" );

GradeBook instructorName( "Mr Johnson");

[snipped some code]

Why do you write a class, that holds a course name and an instructor
name and then instantiate two objects, one using only the course name
and the other using only the instructor name?

Thats like buying two cars: one to get to work, one to get home.

Another thing: Do you know about whitespaces and indentation? Its very
usefull to understand your code.

Thomas
 
L

Luke Meyers

B said:
Thanks guys,
I included the code that included the main. What is really confusing me is
that if I remove all lines refering to the instructor, it compiles fine.

Thanks again for your assistance.

You're welcome. As others have indicated, the problem is that you're
not calling your constructor correctly. Your constructor is declared
with two arguments, so you must pass two arguments (of appropriate
types).

Also, don't top-post.

Also, though it was entirely obvious, when asking for help with
homework you should explicitly state that this is the case, to avoid
the appearance of impropriety and the chance that someone will provide
so much information that you fail to learn what you're supposed to.

Luke
 
B

B Williams

I would like to thank everyone for their assistance. I didn't know the rules
to this news group. I would really like to read them because taking an
online programming class can be painful when you try to ask for assistance
and have to wait hours/days for the instructor to respond. I have corrected
my code to pass two arguments. It now compiles. I am off to the next
program.
 
L

Luke Meyers

B said:
I would like to thank everyone for their assistance. I didn't know the rules
to this news group. I would really like to read them because taking an
online programming class can be painful when you try to ask for assistance
and have to wait hours/days for the instructor to respond. I have corrected
my code to pass two arguments.

You ignored my imperative, so let me state it more strongly.

DON'T TOP-POST.

If this terminology mystifies you, STFW.

As for the rules of this newsgroup, they're discoverable by the same
means as for any other newsgroup -- read the FAQ first. You should be
able to just STFW and find it, but I'll be nice:
parashift.com/c++-faq-lite
It now compiles. I am off to the next program.

This statement is a little terrifying... let me give you some advice
that will save you tribulation beyond measure: you're not done when it
compiles, you're done when it works. How do you know when it works?
Write a test and run it. When the test passes, you're done. I highly
recommend CppUnit for your testing needs. This is something to do
*now*, while you're learning, not later on when you're hoping to get
paid for it.

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top