错误CS0563(二元运算符的参数之一必须是包含类型)
我的源代码(如下)正在生成错误 CS0563,因为我的 CombinedJobs 运算符+(请参阅源代码中的“//Step 5:...”)中的两个参数都被列为 Job(而不是 int、double 等)。我该如何更改此代码以消除此错误?此代码是为了响应下面列出的分配。
作业:
“为 Harold 的家庭服务设计一个 Job 类。该类包含四个数据字段 — Job 描述(例如“清洗窗户”)、完成作业的时间(例如 例如,3.5)、该工作的每小时费率(例如,25.00 美元)以及总费用 工作(每小时费率乘以小时数)。包括用于获取和设置每个字段的属性,除了 总费用 - 该字段将是只读的,并且每次都会计算其值 设定每小时费用或小时数。重载 + 运算符,以便两个作业 可以添加。两个作业的总和是一个新作业,包含两个作业的描述 原始作业(由“和”连接),原始作业的时间总和(以小时为单位),以及 原始职位的平均每小时工资。编写一个 Main() 函数 证明所有方法都能正常工作。将文件另存为 DemoJobs.cs。”
来自:Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell
源代码:
using System;
//Step 1: Creating the Job class and designating its fields
public class Job
{
private string jobDescription;
private int jobTotalTime;
private double jobRate;
private double jobTotalFee;
//Step 2: Using the 'this' object will allow me to designate values for
//"this" instantiation of an object later in the program
public string JobDescription
{
get
{
return this.jobDescription;
}
set
{
this.jobDescription = value;
}
}
public int JobTotalTime
{
get
{
return this.jobTotalTime;
}
set
{
this.jobTotalTime = value;
}
}
public double JobRate
{
get
{
return this.jobRate;
}
set
{
this.jobRate = value;
}
}
public double JobTotalFee
{
get
{
return this.jobTotalFee;
}
set
{
this.jobTotalFee = this.JobRate * this.JobTotalTime;
}
}
//Step 3: Creating a method will allow me to display the different jobs
//with their descriptions and other variable fields later in the program
public void JobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", this.JobDescription, this.JobTotalTime, this.JobRate, this.JobTotalFee);
}
}
//Step 4: Creating two instantiations of the Job Class
public class CreateTwoJobs
{
public static void Main()
{
Job jobA = new Job();
Job jobB = new Job();
jobA.JobDescription = "washing windows";
jobA.JobTotalTime = 5;
jobA.JobRate = 25.00;
jobB.JobDescription = "walking dogs";
jobB.JobTotalTime = 10;
jobB.JobRate = 11.00;
jobA.JobMessage();
jobB.JobMessage();
}
}
//Step 5: Creating a third instantiation of the Job Class that is a combination
//of the first two instantiations using the overloaded + operator
public class CombinedJobs
{
public CombinedJobs(string jobDescription, int jobTotalTime, double jobRate, double jobTotalFee)
{
JobDescription = jobDescription;
JobTotalTime = jobTotalTime;
JobRate = jobRate;
JobTotalFee = jobTotalFee;
}
public static CombinedJobs operator+(Job jobA, Job jobB)
{
string newDescription = jobA.JobDescription + " and " + jobB.JobDescription;
int newTotalTime = jobA.JobTotalTime + jobB.JobTotalTime;
double newJobRate = (jobA.JobRate + jobB.JobRate) / 2;
double newTotalFee = newJobRate * newTotalTime
}
public void CombinedJobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", newDescription, newTotalTime, newJobRate, newTotalTime);
}
public static void Main()
{
CombinedJobMessage();
}
}
My source code (below) is generating Error CS0563 because both of the parameters in my CombinedJobs operator+ (see "//Step 5: ..." in source code) are listed as Job (as opposed to int, double, etc). How can I change this code to eliminate this error? This code is in response to the assignment listed below.
Assignment:
"Design a Job class for Harold’s Home Services. The class contains four data fields—Job
description (for example, “wash windows”), time in hours to complete the Job (for
example, 3.5), per-hour rate charged for the Job (for example, $25.00), and total fee for
the Job (hourly rate times hours). Include properties to get and set each field except
the total fee—that field will be read-only, and its value is calculated each time either
the hourly fee or the number of hours is set. Overload the + operator so that two Jobs
can be added. The sum of two Jobs is a new Job containing the descriptions of both
original Jobs ( joined by “and”), the sum of the time in hours for the original Jobs, and
the average of the hourly rate for the original Jobs. Write a Main()function that
demonstrates all the methods work correctly. Save the file as DemoJobs.cs."
From: Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
Source Code:
using System;
//Step 1: Creating the Job class and designating its fields
public class Job
{
private string jobDescription;
private int jobTotalTime;
private double jobRate;
private double jobTotalFee;
//Step 2: Using the 'this' object will allow me to designate values for
//"this" instantiation of an object later in the program
public string JobDescription
{
get
{
return this.jobDescription;
}
set
{
this.jobDescription = value;
}
}
public int JobTotalTime
{
get
{
return this.jobTotalTime;
}
set
{
this.jobTotalTime = value;
}
}
public double JobRate
{
get
{
return this.jobRate;
}
set
{
this.jobRate = value;
}
}
public double JobTotalFee
{
get
{
return this.jobTotalFee;
}
set
{
this.jobTotalFee = this.JobRate * this.JobTotalTime;
}
}
//Step 3: Creating a method will allow me to display the different jobs
//with their descriptions and other variable fields later in the program
public void JobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", this.JobDescription, this.JobTotalTime, this.JobRate, this.JobTotalFee);
}
}
//Step 4: Creating two instantiations of the Job Class
public class CreateTwoJobs
{
public static void Main()
{
Job jobA = new Job();
Job jobB = new Job();
jobA.JobDescription = "washing windows";
jobA.JobTotalTime = 5;
jobA.JobRate = 25.00;
jobB.JobDescription = "walking dogs";
jobB.JobTotalTime = 10;
jobB.JobRate = 11.00;
jobA.JobMessage();
jobB.JobMessage();
}
}
//Step 5: Creating a third instantiation of the Job Class that is a combination
//of the first two instantiations using the overloaded + operator
public class CombinedJobs
{
public CombinedJobs(string jobDescription, int jobTotalTime, double jobRate, double jobTotalFee)
{
JobDescription = jobDescription;
JobTotalTime = jobTotalTime;
JobRate = jobRate;
JobTotalFee = jobTotalFee;
}
public static CombinedJobs operator+(Job jobA, Job jobB)
{
string newDescription = jobA.JobDescription + " and " + jobB.JobDescription;
int newTotalTime = jobA.JobTotalTime + jobB.JobTotalTime;
double newJobRate = (jobA.JobRate + jobB.JobRate) / 2;
double newTotalFee = newJobRate * newTotalTime
}
public void CombinedJobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", newDescription, newTotalTime, newJobRate, newTotalTime);
}
public static void Main()
{
CombinedJobMessage();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
operator+
方法必须位于其参数之一的类内部。由于在您的情况下,两个参数都是Job
,因此该方法必须位于Job
类内。它不能位于CombinedJobs
内部。另外,您将运算符声明为返回
CombinedJobs
,这意味着您必须实际返回它。这不会导致您所描述的错误,但会导致另一个编译错误。The
operator+
method has to be inside the class of one of its parameters. Since in your case both parameters areJob
, the method has to be inside theJob
class. It can't be insideCombinedJobs
.Also, you declared the operator as returning
CombinedJobs
, so that means you have to actually return it. This is not causing the error you are describing, but it would cause another compile error.