-
multi file project error
Ugg, I do not know why I keep getting errors when I run this project. It ran perfectly before. If anyone can take a look and tell me why I get these errors, I would really appreciate it. Thanks.
I'm going to post the files one by one, then the errors at the very bottom. This is a long multiple file project so anyone who is willing
to help me, you rock and thank you so much.
Code:
// sale.h -- header file for the Sale class
//
// An object of type Sale will store information about a single sale.
// The variable "item" stores the type of item being sold (one of the four
// items in the enumerated type ItemType). Books, music, and software are
// the types of items sold. CREDIT stands for store credit, which incurs
// a negative monetary charge.
// The variable "price" stores the base price of the item
// The variable "tax" is used to store the 7% sales tax
// The variable "total" is used to store the final charge (price + tax)
enum ItemType {BOOK, DVD, SOFTWARE, CREDIT};
class Sale
{
public:
Sale(); // default constructor,
// sets numerical member data to 0
void MakeSale(ItemType x, double amt);
// the MakeSale function loads data into the Sale object. The
// item and price are passed in. Compute tax and total.
// (For store credits, tax is 0).
ItemType Item(); // Returns the type of item in the sale
double Price(); // Returns the price of the sale
double Tax(); // Returns the amount of tax on the sale
double Total(); // Returns the total price of the sale
void Display(); // outputs sale info (described below)
// for the Display function, output the sale item, the price, the tax,
// and the total (all on one line). For store credit, the amounts
// should be enclosed in < > symbols, to indicate a negative charge.
// All monetary output should be in dollars and cents format, to two
// decimal places.
// Examples: Book $ 20.00 Tax: $ 1.40 Total: $ 21.40
// Credit <$ 15.00> Tax: $ 0.00 Total: <$ 15.00>
private:
double price; // price of item or amount of credit
double tax; // amount of sales tax (does not apply to credit)
double total; // final price once tax is added in.
ItemType item; // transaction type
};
Code:
//definitions for Sales.h
#include <iostream>
#include "sale.h"
#include <iomanip>
#include <cctype>
using namespace std;
Sale::Sale()
{
price=0.0;
tax=0.0;
total=0.0;
}
void Sale::MakeSale(ItemType x, double amt)
{
item=x;
price=amt;
if (item==CREDIT)
{
tax=0.0;
total=price;
return;
}
tax= price *.07;
total= price +tax;
}
ItemType Sale::Item()
{
return item;
}
double Sale::Price()
{
return price;
}
double Sale::Tax()
{
return tax;
}
double Sale::Total()
{
return total;
}
void Sale::Display()
{
//return everything else, simply displays
if (item==BOOK)
cout<<"Book \t"<<price<<"\tTax:\t"<<tax<<"\tTotal:\t"<<total<<endl;
if (item==DVD)
cout<<"DVD \t"<<price<<"\tTax:\t"<<tax<<"\tTotal:\t"<<total<<endl;
if (item==SOFTWARE)
cout<<"Software\t"<<price<<"\tTax:\t"<<tax<<"\tTotal:\t"<<total<<endl;
if (item==CREDIT)
cout<<"Credit \t"<<"<"<<price<<">"<<"\tTax:\t"<<tax<<"\tTotal:\t"<<"<"<<total<<">"<<endl;
}
Code:
//header file for Register class
#include "sale.h"
//register header file
class Register
{
public:
Register(int i, double amt);//allows ID number and starting amount
//in register to be initialized when
//object is created
int GetID();//returns current ID number in the register to the
//caller, accessor function
double GetAmount();//return current amount in the register to
//caller, accessor function
void RingUpSale(ItemType c, double amt);//stores sale in sale list
//also updates money amount
//in register.
//allows item type and sale
//base price to be passed in
void ShowLast();//displays only the info about last sale made
void ShowAll();//Shows all of sales currently in sale list, one per line
void Cancel();//cancels last sale in list
double SalesTax(int n);//calculates and returns total amount of sales tax
//for last n sales (i.e. most recent ones)
private:
int ID;
double Amount;
double sales_taxxx;
double base_price;
int numsales;
Sale *sales;
};
Code:
//definitions for register class
#include <iomanip>
#include <iostream.h>
#include "Register.h"
#include "sale.h"
using namespace std;
//Register definitions
Register::Register(int i, double amt)
{
ID=i;
Amount=amt;
numsales=0;
//parameters in register are intialized to 0
}
int Register::GetID()
{
return ID;//returning current ID in register
}
double Register::GetAmount()
{
return Amount;//returning current amount in refrigerator
}
void Register::RingUpSale(ItemType c, double amt)
{
Sale *s;
//create a new array
//memcpy the old array into the new array byte by byte
//set the old array to the new array.
numsales++;
s = new Sale[numsales];
memcpy(s, sales, sizeof(Sale)*(numsales-1));
s[numsales-1].MakeSale(c, amt);
sales=s;
if (c==CREDIT) {
Amount-=amt;
}
else {
Amount+=amt;
}
}
void Register::ShowAll(void) {
if (numsales==0) {
cout<<"No Sales Have Been Made Silly....";
return;
}
for (int i=0;i<numsales;i++) {
sales[i].Display();
}
}
void Register::ShowLast(void) {
if (numsales==0) {
cout<<"No Sales Have Been Made Silly....";
return;
}
sales[numsales-1].Display();
}
void Register::Cancel(void) {
{
Sale *s;
if (numsales==0) {
cout<<"No Sales Have Been Made Silly....";
return;
}
//create a new array
//memcpy the old array into the new array byte by byte
//set the old array to the new array.
if (s[numsales].Item()==CREDIT) {
Amount+=s[numsales].Price();
}
else {
Amount-=s[numsales].Price();
}
numsales--;
s = new Sale[numsales];
memcpy(s, sales, sizeof(Sale)*(numsales));
sales=s;
}
}
double Register::SalesTax(int n) {
int t_n=0;
double tax_total=0;
if (n<0) {
cout<<"Invalid Number..."<<endl;
return 0;
}
//figure out how many to count
if (n > numsales) {
t_n=numsales;
}
else {
t_n=n;
}
//count t_n recent items
for (int i=(numsales-1);i>=(numsales-t_n);i--)
tax_total+=sales[i].Tax();
return tax_total;
}
Code:
//main file that tests both Sales and Register class
#include "sale.h"
#include "Register.h"
#include <iostream>
void main(void) {
using namespace std;
char command;
char stype;
ItemType saletype;
double bprice;
int pastitemstotax;
Register r(1234, 10.00);
r.RingUpSale(SOFTWARE, 1.00);//passing in item type and sale base price
r.RingUpSale(DVD, 2.00);
r.RingUpSale(BOOK, 3.00);
cout<<"Jessica's Register: Enter Command: ";
cin>>command;
if (command<97) {command+=32;}//character setting
while (command != 'x') {//error checking
if (command=='s') {
cout<<"Current ID : "<<r.GetID()<<endl;//requesting current id
cout<<"Current Balance: "<<r.GetAmount()<<endl<<endl;//requesting current
//amount in register
}
if (command=='l') {
r.ShowAll();//requesting all current sales in register
}
if (command=='d') {
r.ShowLast();//requesting info about last sale made
}
if (command=='m') { //requesting menu
cout<<"----------------------Menu-------------------------"<<endl;
cout<<" S: Show Current Amount in Register"<<endl;
cout<<" R: Ring Up A Sale "<<endl;
cout<<" D: Display the Last Sale "<<endl;
cout<<" L: Display the Entire Sale List "<<endl;
cout<<" C: Cancel the Last Stored Sale "<<endl;
cout<<" T: Find the Total Sales Tax for Recent Sales"<<endl;
cout<<" M: Show This Menu "<<endl;
cout<<" X: Exit the Program "<<endl;
cout<<"---------------------------------------------------"<<endl;
.
}
if (command=='r') { //ringing up a sale
stype='0';//sale type
while (stype!='b' && stype!='d' && stype!='s' && stype!='c') {
cout<<"Enter Sale Type: (B - Book, D - DVD, S - Software, C - Credit)";
cin>>stype;
if (stype<97) {stype+=32;}//character setting
if ((stype!='b' && stype!='d' && stype!='s' && stype!='c')) {
cout<<"Invalid Sale Type. Please ReEnter."<<endl;
}
}
bprice=0;
cout<<endl<<"Enter Base Price: $";
cin>>bprice;
while (bprice<0) {
cout<<"Error: Base Price < 0 "<<endl<<endl<<"ReEnter Base Price: $";
cin>>bprice;
}
if (stype=='s') {
saletype=SOFTWARE;
}
if (stype=='b') {
saletype=BOOK;
}
if (stype=='d') {
saletype=DVD;
}
if (stype=='c') {
saletype=CREDIT;
}
r.RingUpSale(saletype, bprice); //asking for storage of sale
//and update of money in register
r.ShowLast();//requesting info on last sale
}
if (command=='c') {
r.Cancel();//voids out last transaction
cout<<"Last Sale Cancelled..."<<endl<<endl;
}
if (command=='t') {
cout<<"Enter Past Items to Calculate Tax:";
cin>>pastitemstotax;
while ((pastitemstotax)<0) {
cout<<"Invalid Number..."<<endl;
cin>>pastitemstotax;
}
r.ShowAll();
cout<<endl<<endl;
cout<<r.SalesTax(pastitemstotax);
}
cout<<endl<<endl;
cout<<"Jessica's Register: Enter Command: ";
cin>>command;
if (command<97) {command+=32;}//character setting
}//end while
}//end main
These are the errors I get. I'm using visual c++.
Code:
sale.h(14) error C2011: 'ItemType' : 'enum' type redefinition
sale.h(17) : error C2011: 'Sale' : 'class' type redefinition main.cpp
sale.h(14) : error C2011: 'ItemType' : 'enum' type redefinition
sale.h(17) : error C2011: 'Sale' : 'class' type redefinition
main.cpp(74) : error C2143: syntax error : missing ';' before '.'
Error executing cl.exe.
-
register.h includes sale.h, and then you include it again in your source - that makes it included twice.
Typically, each header file begins
Code:
#ifndef NAME_OF_HEADER_FILE_H_INCLUDED
#define NAME_OF_HEADER_FILE_H_INCLUDED
// all your stuff here
#endif
Just check out any standard header for comparision
-
So i have to include that in all my files?
-
In all your header files.
Replace the name so it is unique to each header file
sale.h would be SALE_H_INCLUDED for example
-
that could work but for this task, i'm supposed to leave the names as they are. sale.cpp, register.h, register.cpp
-
Who said anything about renaming files?
Those #defines go inside the .h files!
You have actually looked at some pre-written examples already haven't you?
-
yes i have. I'm a bit slow because i'm a newbie at c++. But i did what you suggested and i get over a hundred errors....multi file projects are a challenge for me.
-
Well post something to look at :)
-
Well i get all these errors:
Code:
main.cpp(26) : error C2065: 'ItemType' : undeclared identifier
main.cpp(26) : error C2146: syntax error : missing ';' before identifier 'saletype'
\main.cpp(26) : error C2065: 'saletype' : undeclared identifier
main.cpp(32) : error C2065: 'Register' : undeclared identifier
main.cpp(32) : error C2146: syntax error : missing ';' before identifier 'r'
main.cpp(32) : error C2065: 'r' : undeclared identifier
main.cpp(34) : error C2228: left of '.RingUpSale' must have class/struct/union
Error executing cl.exe.
sales.exe - 126 error(s), 1 warning(s) and so forth, AHHHHHHH'
-
-
...I wasn't determined to ignore you. I just started dissecting the files piece by piece. I thought I had it perfect and was getting bogus errors and then I discovered that there were some slips ups so i decided to comb through it and now I get those errors. My intention wasn't to ignore you. I'm not too familiar with the statements you wanted me to include in the files so i took them out since they were causing over a hundred errors.
-
>i did what you suggested and i get over a hundred errors
Get used to it. Often it's not as bad as it seems. if your original errors went unreported when you put the inclusion gaurds in as per Salem's recommendations then that was the appropriate solution to the original error messages. Adding inclusion gaurds to your header files may seem like a hassle, but they save many more hassles than they cause.
I suspect the new error messages said something completely different than the originals. You can easily generate 100s of "errors" in a full program compile by forgetting a semicolon or a curly brace or something else equally banal. So fix the first one and recompile. Rather than 99 errors instead of 100, maybe you will have 73 or maybe 4. And those 73 or 4 errors after the second fix may not even have been listed in the list of 99. So fix the first one and recompile. Keep doing the sequence until there are no errors left and you get a clean build. Alternatively, just write and debug one function at a time or even just one action/declaration at a time. Either way, once you get a clean compile you have the hard part left, runtime errors, where the compiler can't help you find them. Hopefully you won't have any, but don't be surprised if you do.
-
well now I only get 2 and i have no idea why. I'm stuck.
-
Post the errors, and the code that causes them.
-
See also cross-posts now closed
http://cboard.cprogramming.com/showthread.php?t=55043
http://cboard.cprogramming.com/showthread.php?t=55029
Not to mention a FAQ
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
And next time dantestwin, don't write so much code before pressing compile for the first time, then dumping the whole mess on the board looking for a way out.