C++ 如何在c+中调用函数定义中的类+;

C++ 如何在c+中调用函数定义中的类+;,c++,function,class,constructor,destructor,C++,Function,Class,Constructor,Destructor,这是我第一次在编程方面寻求帮助。几周来,我一直在为我的编程课程编写一个注册程序,其中包括课程。这对我来说相当令人沮丧。我必须使用两个类:StoreItem和Register。StoreItem处理商店销售的小商品列表。register类主要处理处理项目、生成总账单以及要求用户用现金支付。 以下是StoreItem.cpp文件: //function definition #include <string> #include <iostream> #include "Sto

这是我第一次在编程方面寻求帮助。几周来,我一直在为我的编程课程编写一个注册程序,其中包括课程。这对我来说相当令人沮丧。我必须使用两个类:StoreItem和Register。StoreItem处理商店销售的小商品列表。register类主要处理处理项目、生成总账单以及要求用户用现金支付。 以下是StoreItem.cpp文件:

//function definition
#include <string>
#include <iostream>
#include "StoreItem.h"
#include "Register.h"
using namespace std;

StoreItem::StoreItem(string , double)
{
    //sets the price of the current item
    MSRP;
}
void StoreItem::SetDiscount(double)
{
    // sets the discount percentage
    MSRP * Discount;
}
double StoreItem::GetPrice()
{   // return the price including discounts
    return Discount * MSRP;
}
double StoreItem::GetMSRP()
{
    //returns the msrp
    return MSRP;
}
string StoreItem::GetItemName()
{
    //returns item name
    return ItemName;
}
StoreItem::~StoreItem()
{
    //deletes storeitem when done
}
我的主要问题是Register::ScanItem(StoreItem)。。。是否有方法将storeItem类中的函数正确调用到Register scanitem函数中?

您有:

void Register::ScanItem(StoreItem)
{   // adds item to current transaction
    StoreItem.GetPrice();
// this probably isnt correct....

}
这意味着
ScanItem
函数接受一个类型为
StoreItem
的参数。在C++中,可以只指定类型并使编译器快乐。但是如果你想使用这个论点,你必须给它起个名字。例如:

void Register::ScanItem(StoreItem item)
{
    std::cout << item.GetItemName() << " costs " << item.GetPrice() << std::endl;
}
void Register::ScanItem(StoreItem)
{

要能够调用作为参数传递的对象的成员函数,您需要命名参数,而不仅仅是它的类型

我猜你想要像这样的东西

void Register::ScanItem(StoreItem item)
{
    total += item.GetPrice();
}
void Register::ScanItem(StoreItem item)
{
    total += item.GetPrice();
}