使用课程协助 [英] Assistance using classes

查看:53
本文介绍了使用课程协助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在使用类和函数的赋值上。当我尝试运行测试程序时,我正在接收

多次错误,看看我是否正确地写了它b / b
。有人可以指出我正确的方向。这是我收到的错误消息的两个




错误C2660:''Invoice :: setPartNumber'':函数不带1个参数


错误C3861:''setItemQuantity'':找不到标识符


这是代码。 Thsnks提前


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

使用std :: string;


//发票类定义

类发票

{

公开:

发票(字符串) ,string,int,int); //初始化构造函数

void setPartNumber(); //设置部件号的功能

string getPartNumber(); //获取部件号的函数

void setPartDescription(); //设置零件描述的功能

string getPartDescription(); //获取零件描述的函数

void setQuantity(); //设置数量的函数

int getQuantity(); //获取数量的函数

void setPrice(); //设置价格的函数

int getPrice(); //获取价格的函数

int getInvoiceAmount(); //计算发票金额的功能

私人:

字符串编号;

字符串描述;

int数量;

int price;

}; //结束类发票


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

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

#include< iostream>

使用std :: cout;

使用std :: endl;


#include" 313.h" //包含类Invoice的定义


//初始化构造函数

Invoice :: Invoice(字符串编号,字符串描述,int数量,int

价格)

{

setPartNumber(number); //调用set函数初始化partNumber

setPartDescription(description); //调用set函数初始化

partDescription

setItemQuantity(quantity); //调用set函数来初始化数量

setItemPrice(price); //调用set函数初始化价格

} //结束发票构造函数


//设置零件编号的功能

void setPartNumber(string number)

{

partNumber = number; //将零件编号存储在对象中

} //结束函数setPartNumber


//获取零件编号的功能

string getPartNumber()

{

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

} //结束函数getPartNumber


//设置零件描述的功能

void setPartDescription(字符串描述)

{

partDescription = description; //将零件描述存储在

对象中

} //结束函数setPartDescription


//获取零件的功能描述

string getPartDescripton()

{

返回partDescription; //返回对象'的部分描述

} //结束函数getPartDescription


//设置数量的函数

void setQuantity(int quantity)

{

itemQuantity = quantity; //将数量存储在对象中

} //结束函数setItemQuantity


//获取数量的函数

int getItemQuantity()

{

return itemQuantity; //返回对象的数量

} //结束函数getItemQuantity


//设置价格的函数

void setPrice(int price)

{

itemPrice = price; //将价格存储在对象中

} //结束函数setItemPrice


//函数获取价格

int getItemPrice()

{

return itemPrice; //返回对象的价格

} //结束函数getItemPrice


//计算发票金额的会员函数

int getInvoiceAmount(int quantity,int price)

{

if(quantity< 0)

quantity = 0;

if(price< 0)

price = 0;


InvoiceAmount =(数量*价格);

返回InvoiceAmount;


} //结束函数getInvoiceAmount

// invoice.cpp

//包括来自313.h的发票用于main。

#include< iostream>

使用std :: cout;

使用std: :endl;


#include" 313.h" //包含类Invoice的定义


//函数main开始程序执行

int main()

{

//创建发票对象

发票invoice1(" 110-2145"," Chainsaw",4,12);


//显示每个成绩簿的courseName的初始值

invoice1.getInvoiceAmount();

cout<< endl;

返回0; //表示成功终止

} //结束主

解决方案

B Williams发布:


void setPartNumber(); //设置部件号的功能



盯着那条线20秒钟。


部分描述void setQuantity(); //设置
的函数



在该行停留20秒钟。

错误消息不言自明。


-


Frederick Gotham


* B Williams:


我被困在使用类和函数的赋值上。当我尝试运行测试程序时,我正在接收

多次错误,看看我是否正确地写了它b / b
。有人可以指出我正确的方向。这是我收到的错误消息的两个




错误C2660:''Invoice :: setPartNumber'':函数不带1个参数



这意味着你用一个参数调用Invoice :: setPartNumber,但

函数要么不带参数,要么不带一个参数。


error C3861:''setItemQuantity'':找不到标识符



这个意味着没有这样的功能。


这是代码。 Thsnks提前


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

使用std :: string;


//发票类定义

类发票

{

公开:

发票(字符串) ,string,int,int); //初始化构造函数

void setPartNumber(); //设置部件号的功能

string getPartNumber(); //获取部件号的函数

void setPartDescription(); //设置零件描述的功能

string getPartDescription(); //获取零件描述的函数

void setQuantity(); //设置数量的函数

int getQuantity(); //获取数量的函数

void setPrice(); //设置价格的函数

int getPrice(); //获取价格的函数

int getInvoiceAmount(); //计算发票金额的功能

私人:

字符串编号;

字符串描述;

int数量;

int price;

}; //结束类发票



从技术上讲,这是一个类,但它用于构建器的唯一C ++类功能是

的优势。 br />

其余的,有了setter和getter,除了一个例外,并假设

你修复了你的setter函数中缺少参数,实际上是/>
如果你刚刚完成:


struct Invoice

{

Invoice(string,string ,int,int);

字符串编号;

字符串描述;

int数量;

int price;

};


例外是更简单的版本允许引用

''const''对象中的数据,而你的更复杂的版本是不允许的。


在你的教育的这个阶段,我建议使用更简单的版本,

,以免被欺骗代码代表任何类似的东西:

它只是一组数据使用初始化助手(

构造函数)。


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

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

#include< iostream>

使用std :: cout;

使用std :: endl;


#include" 313.h" //包含类Invoice的定义


//初始化构造函数

Invoice :: Invoice(字符串编号,字符串描述,int数量,int

价格)

{

setPartNumber(number); //调用set函数来初始化partNumber



这里你提供了一个参数但函数声明没有

参数。 br />


setPartDescription(description); //调用set函数初始化

partDescription

setItemQuantity(quantity); //调用set函数来初始化数量

setItemPrice(price); //调用set函数初始化价格

} //结束发票构造函数



读取初始化列表并使用它们。


//设置部件号的功能

void setPartNumber(字符串编号)

{

partNumber = number; //将零件号存储在对象中

} //结束函数setPartNumber



这是一个独立的函数,不是成员功能:它不是与班级相关的
而且不应该编译,因为它指的是

不存在的全局''partNumber''。 />
-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


< blockquote>

" Frederick Gotham" < fg ******* @ SPAM.com写了留言

新闻:sd ******************* @ news.indigo .ie ...


> B Williams发布:


> void setPartNumber(); //设置部件号的功能




盯着那条线20秒钟。


>部分描述void setQuantity(); //设置
的函数




在该行停留20秒钟。


错误消息是不言自明。


-


Frederick Gotham



谢谢非常。我纠正了这些错误。现在错误我是b / b
与实际功能有关。我已经修改过了。


来自311.h

int getInvoiceAmount(int,int); //计算发票的功能

金额


来自311.cpp

//计算发票金额的会员功能

int Invoice :: getInvoiceAmount(int quantity,int price)

{

int InvoiceAmount = 0;


if(数量< 0)

数量= 0;

if(价格< 0)

price = 0;


InvoiceAmount =(数量*价格);

返回InvoiceAmount;


} //结束函数getInvoiceAmount <来自invoice.cpp的


int main()

{

//创建发票对象

发票invoice1(" 110-2145"," Chainsaw",4,12);


//显示发票

invoice1.getInvoiceAmount();

cout<< endl;

错误C2660:''Invoice :: getInvoiceAmount'':函数不带0

参数


哪里有我错了?




I am stuck on an assignment that uses classes and functions. I am receiving
numerous errors when I try to run a test program to see if I wrote it
correctly. Can someone please point me in the right direction. These are two
of the error messages I am receiving.

error C2660: ''Invoice::setPartNumber'' : function does not take 1 arguments

error C3861: ''setItemQuantity'': identifier not found

This is the code. Thsnks in advance

#include <string// class Invoice uses C++ standard string class
using std::string;

// Invoice class definition
class Invoice
{
public:
Invoice( string, string, int, int ); // initializing constructors
void setPartNumber(); // function that sets the part number
string getPartNumber(); // function that gets the part number
void setPartDescription(); // function that sets the part description
string getPartDescription(); // function that gets the part description
void setQuantity(); // function that sets the quantity
int getQuantity(); // function that gets the quantity
void setPrice(); // function that sets the price
int getPrice(); // function that gets the price
int getInvoiceAmount(); // function that calculates the invoice amount
private:
string number;
string description;
int quantity;
int price;
}; // end class Invoice

// Invoice member-function definitions. This file contains
// implementations of the member functions prototyped in 313.h.
#include <iostream>
using std::cout;
using std::endl;

#include "313.h" // include definition of class Invoice

// initializing constructors
Invoice::Invoice( string number, string description, int quantity, int
price )
{
setPartNumber( number ); // call set function to initialize partNumber
setPartDescription( description ); // call set function to initialize
partDescription
setItemQuantity( quantity ); // call set function to initialize quantity
setItemPrice( price ); // call set function to initialize price
} // end Invoice constructors

// function to set the part number
void setPartNumber( string number )
{
partNumber = number; // store the part number in the object
} // end function setPartNumber

// function to get the part number
string getPartNumber()
{
return partNumber; // return object''s partNumber
} // end function getPartNumber

// function to set the part description
void setPartDescription( string description )
{
partDescription = description; // store the part description in the
object
} // end function setPartDescription

// function to get the part description
string getPartDescripton()
{
return partDescription; // return object''s partDescription
} // end function getPartDescription

// function to set the quantity
void setQuantity( int quantity )
{
itemQuantity = quantity; // store the quantity in the object
} // end function setItemQuantity

// function to get the quantity
int getItemQuantity()
{
return itemQuantity; // return object''s Quantity
} // end function getItemQuantity

// function to set the price
void setPrice( int price )
{
itemPrice = price; // store the price in the object
} // end function setItemPrice

// function to get the price
int getItemPrice()
{
return itemPrice; // return object''s Price
} // end function getItemPrice

// member function that calculates the invoice amount
int getInvoiceAmount(int quantity, int price)
{
if (quantity < 0)
quantity = 0;
if (price < 0)
price = 0;

InvoiceAmount = (quantity*price);
return InvoiceAmount;

} // end function getInvoiceAmount

// invoice.cpp
// Including class Invoice from 313.h for use in main.
#include <iostream>
using std::cout;
using std::endl;

#include "313.h" // include definition of class Invoice

// function main begins program execution
int main()
{
// create Invoice object
Invoice invoice1( "110-2145", "Chainsaw", 4, 12 );

// display initial value of courseName for each GradeBook

invoice1.getInvoiceAmount();
cout << endl;
return 0; // indicate successful termination
} // end main

解决方案

B Williams posted:

void setPartNumber(); // function that sets the part number


Stare at that line for twenty seconds.

part description void setQuantity(); // function that sets the


Stare at that line for twenty seconds.
The error messages were self-explanatory.

--

Frederick Gotham


* B Williams:

I am stuck on an assignment that uses classes and functions. I am receiving
numerous errors when I try to run a test program to see if I wrote it
correctly. Can someone please point me in the right direction. These are two
of the error messages I am receiving.

error C2660: ''Invoice::setPartNumber'' : function does not take 1 arguments

This means you''re calling Invoice::setPartNumber with an argument, but
the function either takes no arguments, or more than one.

error C3861: ''setItemQuantity'': identifier not found

This means there''s no such function.

This is the code. Thsnks in advance

#include <string// class Invoice uses C++ standard string class
using std::string;

// Invoice class definition
class Invoice
{
public:
Invoice( string, string, int, int ); // initializing constructors
void setPartNumber(); // function that sets the part number
string getPartNumber(); // function that gets the part number
void setPartDescription(); // function that sets the part description
string getPartDescription(); // function that gets the part description
void setQuantity(); // function that sets the quantity
int getQuantity(); // function that gets the quantity
void setPrice(); // function that sets the price
int getPrice(); // function that gets the price
int getInvoiceAmount(); // function that calculates the invoice amount
private:
string number;
string description;
int quantity;
int price;
}; // end class Invoice

This is technically a class, but the only C++ class feature it uses to
advantage is the constructor.

The rest, with setters and getters, is, with one exception, and assuming
you fix the lack of arguments in your setter functions, effectively as
if you''d just done:

struct Invoice
{
Invoice( string, string, int, int );
string number;
string description;
int quantity;
int price;
};

The exception is that simpler version allows referring to data in a
''const'' object, whereas your more complicated version doesn''t allow that.

At this stage in your education I recommend using the simpler version,
so as not being deluded that the code represents anything class-like:
it''s just a collection of data with an initialization helper (the
constructor).

// Invoice member-function definitions. This file contains
// implementations of the member functions prototyped in 313.h.
#include <iostream>
using std::cout;
using std::endl;

#include "313.h" // include definition of class Invoice

// initializing constructors
Invoice::Invoice( string number, string description, int quantity, int
price )
{
setPartNumber( number ); // call set function to initialize partNumber

Here you''re providing an argument but the function was declared with no
argument.

setPartDescription( description ); // call set function to initialize
partDescription
setItemQuantity( quantity ); // call set function to initialize quantity
setItemPrice( price ); // call set function to initialize price
} // end Invoice constructors

Read up on initialization lists and use them.

// function to set the part number
void setPartNumber( string number )
{
partNumber = number; // store the part number in the object
} // end function setPartNumber

This is a free-standing function, not a member function: it''s not
related to the class and shouldn''t compile since it refers to a
non-existent global ''partNumber''.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:sd*******************@news.indigo.ie...

>B Williams posted:

> void setPartNumber(); // function that sets the part number



Stare at that line for twenty seconds.

> part description void setQuantity(); // function that sets the



Stare at that line for twenty seconds.
The error messages were self-explanatory.

--

Frederick Gotham

Thank you very much. I have corrected those errors. Now the error I am
having is with the actual function. I have modified it as such.

from 311.h
int getInvoiceAmount(int, int); // function that calculates the invoice
amount"

from 311.cpp
// member function that calculates the invoice amount
int Invoice::getInvoiceAmount(int quantity, int price)
{
int InvoiceAmount = 0;

if (quantity < 0)
quantity = 0;
if (price < 0)
price = 0;

InvoiceAmount = (quantity*price);
return InvoiceAmount;

} // end function getInvoiceAmount

from invoice.cpp
int main()
{
// create Invoice object
Invoice invoice1( "110-2145", "Chainsaw", 4, 12 );

// display invoice

invoice1.getInvoiceAmount();
cout << endl;
error C2660: ''Invoice::getInvoiceAmount'' : function does not take 0
arguments

Where am I going wrong?




这篇关于使用课程协助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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