帮助代码无法编译 [英] Help Code won't compile

查看:93
本文介绍了帮助代码无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用此代码进行更好的部分工作,而且我不能确定我在哪里弄错了。我只能想象当我在构造函数中声明多个参数时,因为程序只用一个参数编译

。有人可以看看这个并告诉我在哪里制作

我的错误吗?


这是我在编译时遇到的错误。


错误C2664:''GradeBook :: GradeBook(const GradeBook&)'':无法转换

参数1从''const char [38]''到''' const GradeBook&''


原因:无法从''const char [38]''转换为''const GradeBook''


没有构造函数可以采用源类型或构造函数重载

分辨率不明确

谢谢


// GradeBook.cpp


// GradeBook成员函数定义。这个文件包含


//在GradeBook.h中原型化的成员函数的实现。


#include< iostream>


使用std :: cout;


使用std :: endl;


#include" GradeBook。 H" //包括类GradeBook的定义


//构造函数初始化courseName和instructorName,带字符串

作为参数提供


GradeBook :: GradeBook(字符串名称,字符串指导员)


{


setCourseName(name); //调用set函数初始化courseName


setInstructorName(instructor); //调用set函数初始化

instructorName


} //结束GradeBook构造函数


//设置课程名称的功能


void GradeBook :: setCourseName(string name)


{


courseName = name; //将课程名称存储在对象中


} //结束函数setCourseName


//获取课程名称的函数


string GradeBook :: getCourseName()


{


return courseName; //返回对象的'courseName


} //结束函数getCourseName


//设置教师姓名的功能


void GradeBook :: setInstructorName(string instructor)


{


instructorName = instructor; //将教师名称存储在对象中


} //结束函数setInstructorName


//获取教师姓名的函数


string GradeBook :: getInstructorName()


{


return instructorName; //返回对象'的instructorName


} //结束函数getInstructorName


//向GradeBook用户显示欢迎消息


void GradeBook :: displayMessage()

{


//调用getCourseName和getInstructorName获取courseName和

instructorName


cout<< 欢迎来到'\\ n'的成绩册 << getCourseName()


<< "!" << endl;


cout<< 本课程由:\ n提供: << getInstructorName()


<< "!" << endl;


} //结束函数displayMessage


这是头文件


/ / GradeBook.h


// GradeBook类定义。这个文件提供了GradeBook的公共

//界面,但没有透露GradeBook'的成员


//函数的实现,在GradeBook.cpp中定义。


#include< string> // class GradeBook使用C ++标准字符串类


使用std :: string;


// GradeBook类定义


class GradeBook


{


public:


GradeBook(string ,string); //初始化courseName的构造函数和

instructorName


void setCourseName(string); //设置课程名称的功能


string getCourseName(); //获取课程名称的函数


void setInstructorName(string); //设置讲师姓名的函数


string getInstructorName(); //获取教师姓名的函数


void displayMessage(); //显示欢迎信息的功能


private:


string courseName; //这个GradeBook的课程名称


string instructorName; //此成绩册的讲师姓名


}; //结束班GradeBook


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写道:
B Williams wrote:
我一直在使用这段代码进行更好的部分工作,我无法弄明白我在哪里犯了错误。我只能想象当我在构造函数上声明多个参数时,因为程序只用一个参数编译
。有人可以看看这个并告诉我在哪里制作了我的错误吗?

这是我在编译时遇到的错误。

错误C2664:'' GradeBook :: GradeBook(const GradeBook&)'':无法将参数1从''const char [38]''转换为''const GradeBook&''

原因:无法将''const char [38]''转换为''const GradeBook''

没有构造函数可以采用源类型,或构造函数重载
解决方案不明确
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



在某个地方,你试图复制一个GradeBook对象,而你不需要
有一个复制构造函数。


几个积分风格:


方式太多多余的评论。只需给出有意义的东西

名称并让代码讲述自己的故事。


永远不要在标题中放置using指令。


-

Ian Collins。


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.


B Williams写道:
B Williams wrote:
我一直在工作使用此代码可以获得更好的一天,而且我无法弄清楚我在哪里犯了错误。我只能想象它
当我在构造函数上声明多个参数时,因为
程序只用一个参数编译。有人可以看看这个
并告诉我我的错误在哪里吗?

这是我在编译时遇到的错误。

错误C2664:'' GradeBook :: GradeBook(const GradeBook&)'':无法将参数1从''const char [38]''转换为''const GradeBook&''

原因:无法将''const char [38]''转换为''const GradeBook''


您发布的代码的哪一行与此消息有关?
<没有构造函数可以采用源类型,或构造函数重载
分辨率不明确

谢谢

// GradeBook.cpp
// GradeBook成员函数定义。这个文件包含了在GradeBook.h中原型化的成员函数的实现。

#include< iostream>

使用std :: cout;

使用std :: endl;

#include" GradeBook.h" //包括类GradeBook的定义

//构造函数初始化courseName和instructorName,其中包含字符串


GradeBook :: GradeBook(字符串名称,字符串指示符)

{

setCourseName(name); //调用set函数初始化courseName

setInstructorName(instructor); //调用set函数初始化
instructorName

} //结束GradeBook构造函数
//设置课程名称的功能

void GradeBook :: setCourseName(string name)

{

courseName = name ; //将课程名称存储在对象中

} //结束函数setCourseName

//获取课程名称的功能

string GradeBook: :getCourseName()

{

返回课程名称; //返回对象的'courseName

} //结束函数getCourseName

//设置教师名称的功能

void GradeBook :: setInstructorName(string instructor)



instructorName = instructor; //将教师名称存储在
对象中//结束函数setInstructorName

//获取教师名称的函数

字符串GradeBook: :getInstructorName()



返回instructorName; //返回对象'的instructorName

} //结束函数getInstructorName

//向GradeBook用户显示欢迎消息

void GradeBook :: displayMessage()

//调用getCourseName和getInstructorName来获取courseName和
instructorName

cout<< ; 欢迎来到'\\ n'的成绩册 << getCourseName()

<< "!" << endl;

cout<< 本课程由:\ n提供: << getInstructorName()

<< "!" << endl;

} //结束函数displayMessage

这是头文件

// GradeBook.h

// GradeBook类定义此文件提供了GradeBook的公共
//界面,但没有透露GradeBook的成员
//函数的实现,这些函数在GradeBook.cpp中定义。

#include< string> //类GradeBook使用C ++标准字符串类

使用std :: string;

// GradeBook类定义

类GradeBook

{

公开:

GradeBook(字符串,字符串); //初始化
courseName和instructorName的构造函数

void setCourseName(string); //设置课程名称的函数

string getCourseName(); //获取课程名称的函数

void setInstructorName(string); //设置
教师名称的函数
字符串getInstructorName(); //获取教师姓名的函数

void displayMessage(); //显示欢迎信息的功能

私人:

string courseName; //此GradeBook的课程名称

string instructorName; //本成绩簿的讲师姓名

}; // end class GradeBook
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

-

请删除大写''A'的时候通过电子邮件回复

我没有回复最热门的回复,请不要问



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写道:
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?




请阅读此新闻组的常见问题解答,特别是关于如何

发布。您需要发布完整的,可编译的,最小的代码,

说明了问题。您发布的代码甚至不包含

main()函数。尽管有这个

,我还是花了很多时间来阅读它,并且不要认为你发布了导致

错误的代码行。如果你确定错误是哪一行也很有帮助。


另外,我同意另一张关于你使用评论的海报 -

a评论除了声明调用

getFoo以获取Foo之外什么也没做什么。比无用更糟糕。我将进一步补充说,

为您的私人Foo成员设置getFoo()和setFoo()基本上是

在设计方面没有比仅仅将该成员公开更好,但是

你有时间欣赏这样的设计微妙之处,因为你已经获得了更多的语言基础知识。


Luke



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


这篇关于帮助代码无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆