Thread: class errors

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    class errors

    I cant figure out why I am getting these class erros....can someone help..I know this alot of code here but I would appreciate any kind of help. here are my errors:

    Code:
    Compiling...
    checkout.cpp
    c:\unzipped\13sep\grocery.h(28) : error C2629: unexpected 'class Grocery ('
    c:\unzipped\13sep\grocery.h(28) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    c:\unzipped\13sep\grocery.h(32) : error C2061: syntax error : identifier 'istream'
    c:\unzipped\13sep\grocery.h(33) : error C2061: syntax error : identifier 'ostream'
    c:\unzipped\13sep\grocery.h(37) : error C2146: syntax error : missing ';' before identifier 'Description'
    c:\unzipped\13sep\grocery.h(37) : error C2433: 'string' : 'inline' not permitted on data declarations
    c:\unzipped\13sep\grocery.h(37) : error C2501: 'string' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(37) : warning C4183: 'Description': member function definition looks like a ctor, but name does not match enclosing class
    c:\unzipped\13sep\grocery.h(48) : error C2146: syntax error : missing ';' before identifier 'description'
    c:\unzipped\13sep\grocery.h(48) : error C2501: 'string' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(48) : error C2501: 'description' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(61) : error C2061: syntax error : identifier 'ostream'
    c:\unzipped\13sep\checkout.cpp(29) : error C2660: 'ReadGrocery' : function does not take 1 parameters
    c:\unzipped\13sep\checkout.cpp(36) : error C2660: 'WriteStock' : function does not take 1 parameters
    c:\unzipped\13sep\checkout.cpp(37) : error C2660: 'WriteStock' : function does not take 1 parameters
    c:\unzipped\13sep\checkout.cpp(73) : error C2440: 'initializing' : cannot convert from 'int' to 'class std::basic_string,class std::allocator >'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    c:\unzipped\13sep\checkout.cpp(111) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    grocery.cpp
    c:\unzipped\13sep\grocery.h(28) : error C2629: unexpected 'class Grocery ('
    c:\unzipped\13sep\grocery.h(28) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    c:\unzipped\13sep\grocery.h(32) : error C2061: syntax error : identifier 'istream'
    c:\unzipped\13sep\grocery.h(33) : error C2061: syntax error : identifier 'ostream'
    c:\unzipped\13sep\grocery.h(37) : error C2146: syntax error : missing ';' before identifier 'Description'
    c:\unzipped\13sep\grocery.h(37) : error C2433: 'string' : 'inline' not permitted on data declarations
    c:\unzipped\13sep\grocery.h(37) : error C2501: 'string' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(37) : warning C4183: 'Description': member function definition looks like a ctor, but name does not match enclosing class
    c:\unzipped\13sep\grocery.h(48) : error C2146: syntax error : missing ';' before identifier 'description'
    c:\unzipped\13sep\grocery.h(48) : error C2501: 'string' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(48) : error C2501: 'description' : missing storage-class or type specifiers
    c:\unzipped\13sep\grocery.h(61) : error C2061: syntax error : identifier 'ostream'
    c:\unzipped\13sep\grocery.cpp(21) : error C2511: 'ReadGrocery' : overloaded member function 'void (class std::basic_istream > &)' not found in 'Grocery'
            c:\unzipped\13sep\grocery.h(21) : see declaration of 'Grocery'
    c:\unzipped\13sep\grocery.cpp(28) : error C2511: 'WriteGrocery' : overloaded member function 'void (class std::basic_ostream > &)' not found in 'Grocery'
            c:\unzipped\13sep\grocery.h(21) : see declaration of 'Grocery'
    c:\unzipped\13sep\grocery.cpp(81) : error C2511: 'WriteStock' : overloaded member function 'void (class std::basic_ostream > &)' not found in 'Stockroom'
            c:\unzipped\13sep\grocery.h(55) : see declaration of 'Stockroom'
    Error executing cl.exe.
    grocery.h file
    Code:
    #ifndef GROCERYH
    #define GROCERYH
    
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    const double TAXRATE = 0.075;
    
    // Item in grocery inventory
    class Grocery
    {
    public:
    	// Constructors
    	//Grocery ( ) : product(-1), description(""), price(0.0), taxable(' ') { }
    	Grocery ( ) { }
    	//Grocery ( int prod, string desc, double cost, char tax ) : 
    	//	product(prod), description(desc), price(cost), taxable(tax) { }
    	Grocery ( int prod, string desc, double cost, char tax )
    	{ product = prod; description = desc; price = cost; taxable = tax; }
    
    	// Input/output
    	void ReadGrocery ( istream& in );
    	void WriteGrocery ( ostream& out );
    
    	// Access functions
    	inline int Product ( ) { return(product); }
    	inline string Description ( ) { return(description); }
    	inline double Price ( ) { return(price); }
    	inline char Taxable ( ) { return(taxable); }
    
    	// Calculations
    	inline double Sale ( int quantity ) { return(price * quantity); }
    	inline double Tax ( int quantity ) {
    		return(taxable != 'T' ? 0.00 : Sale(quantity) * TAXRATE); }
    
    private:
    	int product;			// product code
    	string description;		// description
    	double price;			// unit price
    	char taxable;			// T = Taxable, N = Non-taxable
    };
    
    // Grocery stockroom (all inventory)
    class Stockroom
    {
    public:
    	// Constructor
    	Stockroom (int shelves);
    
    	// Output
    	void WriteStock ( ostream& out );
    
    	// Enter food into stock
    	bool BuyStock (Grocery food);
    
    	// Search for prod.
    	Grocery* GroceryClerk (int prod);
    
    private:
    	Grocery *pantry;		// groceries in stock
    	int groceries,			// # groceries in stock
                stockshelves;		// max. groceries in stock
    };
    
    #endif
    grocery.cpp file
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    #include "grocery.h"
    
    using namespace std;
    
    // Grocery
    
    // Input Grocery from in.
    void Grocery::ReadGrocery ( istream& in )
    {
    	in >> product >> description >> price >> taxable;
    	char eol; in >> eol; in.unget();	// Position after eol.
    }
    
    // Output Grocery to out.
    void Grocery::WriteGrocery ( ostream& out )
    {
    	out << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
    	out.precision(2);			// 2 decimals
    	string desc = description;
    	int descblanks = 12 - desc.length() + 1;
    	for (int d = 0; d < descblanks; d++) desc += ' ';
    	out << setw(5) << product << " " << desc << setw(4) << price 
    		<< " " << taxable;
    }
    
    // Stockroom
    
    // A Stockroom for shelves items
    Stockroom::Stockroom (int shelves)
    {
    	pantry = new Grocery[shelves];
    	stockshelves = shelves;
    	groceries = 0;
    }
    
    // BuyStock: Include Grocery in Stockroom
    // Returns true if food added to stock; false otherwise
    bool Stockroom::BuyStock (Grocery food)
    {
    	if ((groceries+1) >= stockshelves) return(false);
    	for (int g = 0; g < groceries; g++)
    		if (food.Product() < pantry[g].Product()) {
    			for (int g1 = groceries++; g1 >= g; g1--)
    				pantry[g1+1] = pantry[g1];
    			pantry[g] = food;
    			return(true);
    		} else if (food.Product() == pantry[g].Product()) return(true);
    	pantry[groceries++] = food;
    	return(true);
    }
    
    // GroceryClerk: Search Stockroom for prod
    // Returns: Grocery* except NULL when prod not in inventory
    Grocery* Stockroom::GroceryClerk (int prod)
    {
    	int low = 0;
    	int high = groceries-1;
    	while (low <= high) {
    		int mid = (low + high) / 2;
    		if (pantry[mid].Product() == prod) return(&pantry[mid]);
    		if (pantry[mid].Product() < prod) low = mid + 1;
    		else high = mid - 1;
    	}
    	return(NULL);
    }
    
    // Output all items in Stockroom to out.
    void Stockroom::WriteStock ( ostream& out )
    {
    	for (int g = 0; g < groceries; g++) {
    		pantry[g].WriteGrocery(out);
    		out << endl; }
    	out << endl;
    }
    checkout.cpp
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    #include "grocery.h"
    
    using namespace std;
    
    const int STOCKROOM = 30;		// Max. items in inventory
    const char *INVENT = "Invent.in";	// inventory file
    const char *RECEIPTS = "Receipts.out";	// file of receipts
    
    int main ( ) 
    {
    	Stockroom stockroom(30);
    
    	ifstream invent(INVENT);	// Read inventory
    	while (!invent.eof()) {
    		Grocery food;
    		food.ReadGrocery(invent);
    		if (stockroom.BuyStock(food)) continue;
    		cerr << "Stockroom is full!" << endl;
    		exit(-1);
    	}
    
    	ofstream receipts(RECEIPTS);
    	stockroom.WriteStock(receipts);	// List inventory on receipt
    	stockroom.WriteStock(cout);	// and onto screen
    
    	int customer = 1;
    	string more;
    	do {
    		double bought(0.00), tax(0.00);
    		receipts << endl << "Customer " << customer << endl;
    		cout << "Enter purchases for customer " << customer++ << endl
    			<< "Enter product code 0 to end purchases." << endl << endl;
    
    		cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
    		cout.precision(2);
    		receipts << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
    		receipts.precision(2);
    
    		int prod, quantity;
    		do {
    			//cout << "Enter product code and quantity: ";
    			cin >> prod;
    			if (prod > 0) {
    				Grocery *product = stockroom.GroceryClerk(prod);
    				cin >> quantity;
    
    				if (product == NULL) {	// product not found?
    		receipts << "*** item " << prod << " not in inventory ***" << endl;
    		cout << "*** item " << prod << " not in inventory ***" << endl;
    
    				} else if ((quantity < 1) || (10 < quantity)) {
    		receipts << "*** " << quantity << " of item " << prod << 
    			" are not for sale ***" << endl;
    	cout << "*** " << quantity << " of item " << prod << 
    			" are not for sale ***" << endl;
    
    				} else {		// sale
    					bought += product->Sale(quantity);
    					tax += product->Tax(quantity);
    					string desc = product->Description();
    					int descblanks = 12 - desc.length() + 1;
    					for (int d = 0; d < descblanks; d++)
    						desc += ' ';
    		receipts << desc << setw(2) << quantity << " @ " << setw(4) <<
    			product->Price() << setw(6) << product->Sale(quantity) 
    			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
    		/*
    		cout << desc << setw(2) << quantity << " @ " << setw(4) <<
    			product->Price() << setw(6) << product->Sale(quantity) 
    			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
    		*/
    				}
    			}
    		} while (prod != 0);
    
    		if (bought > 0.00) {		// bought anything?
    			receipts << endl << "              Subtotal " 
    				<< setw(5) << bought << endl << 
    				"                   Tax " 
    				<< setw(5) << tax << endl << endl << 
    				"                 Total " 
    				<< setw(5) << (bought + tax) << endl << endl;
    			/*
    			cout << endl << "              Subtotal " 
    				<< setw(5) << bought << endl << 
    				"                   Tax " 
    				<< setw(5) << tax << endl << endl << 
    				"                 Total " 
    				<< setw(5) << (bought + tax) << endl << endl;
    			*/
    		}
    
    		cout << "Is there another customer? (Y/N) ";
    		cin >> more;
    	} while (more == "Y");
    	if (more == "N")
    		cout << "Close the store for the night. Bye." << endl;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In your header file, grocery.h, you include and use things like string, istream and ostream that are in the standard namespace, but there is no "using namespace std;" under your includes. In general it is usually not a good idea to use that using directive in a header, which is why I prefer to just add std:: to everything always. Either way that should fix your build.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Indata.open file wotn work....

    Ok, I am attaching my two .cpp and .h file..can someone take a look, it compiles, runs but the Invent.txt file wont open so people can see what is in my store?? thanks

    Bryan

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Make sure you check to see if it is open:
    Code:
    if (inData.good())
     ...
    //or 
    if (inData.fail())
     ...
    //exit() or return probably

    Maybe you just misspelled the file name or its not in the right directory...check to see if thats not true first
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Class Errors (LNK2005)
    By NMZ in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 03:52 PM
  3. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 03:22 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM