2015-10-27 1 views
0

Его мой первый запрос на помощь в программировании. Я работаю над программой регистрации для своего класса программирования в течение недель, которая включает в себя классы. Это довольно неприятно для меня. Я должен использовать два класса: StoreItem и Register. StoreItem занимается небольшим списком предметов, которые продает магазин. Класс регистра занимается в основном обработкой элементов, составлением общего счета и запросом у пользователя платить наличными. Вот файл StoreItem.cpp:Как вызвать класс в определении функции в C++

//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.cpp: Обратите внимание, что последние 5 определений функций в этом одном Арент закончил еще ...

// definition of the register header 
#include "Register.h" 
#include "StoreItem.h" 
using namespace std; 

Register::Register() 
{ // sets the initial cash in register to 400 
    CashInRegister = 400; 
} 
Register::Register(double) 
{ //accepts initial specific amount 
    CashInRegister ; 
} 
void Register::NewTransAction() 
{ //sets up the register for a new customer transaction (1 per checkout) 
    int NewTransactionCounter = 0; 
    NewTransactionCounter++; 
} 
void Register::ScanItem(StoreItem) 
{ // adds item to current transaction 
    StoreItem.GetPrice(); 
// this probably isnt correct.... 

} 
double Register::RegisterBalance() 
{ 
    // returns the current amount in the register 
} 
double Register::GetTransActionTotal() 
{ 
    // returns total of current transaction 
} 
double Register::AcceptCash(double) 
{ 
    // accepts case from customer for transaction. returns change 
} 
void Register::PrintReciept() 
{ 
    // Prints all the items in the transaction and price when finsished 

} 
Register::~Register() 
{ 
    // deletes register 
} 

Мой главный вопрос где Register :: ScanItem (StoreItem) ... есть способ правильно вызвать функцию из класса storeItem в функцию Register scanitem?

ответ

0

У вас есть:

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; 
} 
0

Чтобы иметь возможность вызвать функцию-член объекта вы передаете в качестве параметра, необходимо назвать параметр, а не только его тип.

Я подозреваю, что вы хотите что-то вроде

void Register::ScanItem(StoreItem item) 
{ 
    total += item.GetPrice(); 
} 
Смежные вопросы