Help Code won't compile

Discussion in 'C++' started by B Williams, Jun 30, 2006.

  1. B Williams

    B Williams Guest

    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
     
    B Williams, Jun 30, 2006
    #1
    1. Advertising

  2. B Williams

    Ian Collins Guest

    B Williams wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Jun 30, 2006
    #2
    1. Advertising

  3. B Williams wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 30, 2006
    #3
  4. B Williams

    Luke Meyers Guest

    B Williams wrote:
    > 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
     
    Luke Meyers, Jun 30, 2006
    #4
  5. B Williams

    B Williams Guest

    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" <> wrote in message
    news:e848l7$c1s$...
    >B Williams wrote:
    >> 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
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    >
     
    B Williams, Jul 1, 2006
    #5
  6. B Williams

    B Williams Guest

    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.

    "Luke Meyers" <> wrote in message
    news:...
    >B Williams wrote:
    >> 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 Williams, Jul 1, 2006
    #6
  7. B Williams wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 1, 2006
    #7
  8. B Williams schrieb:
    > 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
     
    Thomas J. Gritzan, Jul 1, 2006
    #8
  9. B Williams

    Luke Meyers Guest

    B Williams wrote:
    > 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
     
    Luke Meyers, Jul 1, 2006
    #9
  10. B Williams

    B Williams Guest

    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.

    "Luke Meyers" <> wrote in message
    news:...
    >B Williams wrote:
    >> 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 Williams, Jul 1, 2006
    #10
  11. B Williams

    Luke Meyers Guest

    B Williams wrote:
    > 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
     
    Luke Meyers, Jul 1, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TdJ
    Replies:
    1
    Views:
    558
    Mike Moore [MSFT]
    Nov 12, 2003
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    linked list code won't compile

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 6, 2006, in forum: C++
    Replies:
    10
    Views:
    581
    Marcus Kwok
    May 8, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    883
    Lionel B
    Mar 1, 2007
  4. DerekBaker

    Moved code won't compile

    DerekBaker, Nov 3, 2007, in forum: C++
    Replies:
    2
    Views:
    1,138
    DerekBaker
    Nov 3, 2007
  5. Angus
    Replies:
    1
    Views:
    452
    Frank Birbacher
    May 24, 2008
Loading...

Share This Page