Click to See Complete Forum and Search --> : help with three errors(2 unqualified id, and void before token.)


kevinskrazyklub
July 14th, 2007, 10:25 PM
/Users/Kevin/Documents/Programming/Xcode/C++/Inventoryclass/main.cpp:57: error: expected unqualified-id before 'void'
/Users/Kevin/Documents/Programming/Xcode/C++/Inventoryclass/main.cpp:67: error: expected unqualified-id before 'void'
/Users/Kevin/Documents/Programming/Xcode/C++/Inventoryclass/main.cpp:90: error: expected declaration before '}' token

i know this is kinda long but please look at these three functions:

the first error is at: Inventory::void init_inv()
the second is at: Inventory::void menu();
the last error is at the last curly brace of the menu function.



#include <iostream>
using namespace std;

const int SIZE = 100;
class Item {
char *item;
double cost;
double retail;
int on_hand;
int lead_time;

public:
Item(char *item, double cost, double retail, int on_hand, int lead_time);
Item(const Item &obj);
Item();
void setItem(char *c){item = c;}
void setCost(double *d){cost = *d;}
void setRetail(double *d){retail = *d;}
void setOnHand(int *hand){on_hand = *hand;}
void setLeadTime(int *time){lead_time = *time;}
char *getItem(){return item;}
double getCost(){return cost;}
double getRetail(){return retail;}
int getOnHand(){return on_hand;}
int getLeadTime(){return lead_time;}
};
Item::Item(char *item, double cost, double retail, int on_hand, int lead_time){
this->item = item;
this->cost = cost;
this->retail = retail;
this->on_hand = on_hand;
this->lead_time = lead_time;
}
Item::Item(){
*item = '\0';
}
/*Item::Item(char *item, double cost, double retail, int on_hand, int lead_time)
: item( item ), cost( cost ), retail( retail ), on_hand( on_hand), lead_time( lead_time )
{
}*/

Item::Item(const Item &obj )
: item( obj.item ), cost( obj.cost ), retail( obj.retail ), on_hand( obj.on_hand), lead_time( obj.lead_time )
{
}

class Inventory {
Item inventory[SIZE];
char *name;
Inventory(char *name);
Inventory(const Inventory &obj);
public:
void init_inv(), enter(), update(), display(), menu(), input();
char *getName(){return name;}

};
Inventory::void init_inv(){

cout << "Enter name of the inventory: ";
cin >> name;

Inventory inv1(name);
for(int i = 0; i < SIZE; i++){
inventory[i].item = '\n';
}
}
Inventory::void menu(){
char choice;
for(;;){
do{
cout << "Choose one:\n\n";
cout << "(E)nter\n(D)isplay\n(U)pdate\nQuit\n";

}while(!strchr("eduq", tolower(ch));
cin >> choice;
switch(ch){
case 'E': enter();
break;
case 'U': update();
break;
case 'D': display();
break;
case 'Q':return;
}

}

}

}
Inventory::Inventory(char *name){
this->name = name;
Item inventory[SIZE] = inventory;
}
Inventory::Inventory(const Inventory &obj)
: name(obj.name), inventory(obj.inventory){
}
Inventory::void input(int i){
cout << "Item: ";
cin >> inventory[i].item;

cout << "Cost: ";
cin >> inventory[i].price;

cout << "Retail price: "
cin >> inventory[i].retail;

cout << "Quantity in stock: ";
cin >> inventory[i].on_hand;

cout << "Lead time before restock(in days): ";
cin >> inventory[i].lead_time;
}
Inventory::void enter(){
Item temp[SIZE+10];
int i;
for(i = 0; i < SIZE;i++){
if(!*inventory[i].item)
break;
}
if(i == size){
cout << "List full!\n";
return;
}
input(i);
}
Inventory::void update(){
char *item_search;
cout << "enter item to update:";
cin >> *item_search;

for(int i = 0; inventory[i].item;i++){
if(!strcmp(inventory[i].item, item_search)){
input(i);
char ch;
cout << "Search again? (y/n)";
if(choice == 'y' || choice == 'Y')
update();
else return;
}
else {
char choice;
cout << "Item not found. Search again? (y\n): ";
cin >> choice;
if(choice == 'y' || choice == 'Y')
update();
else return;
}
}

}
Inventory::void display(){
int t;
cout << "inventory name: ";
//sort();
for(t = 0; t < SIZE; t++){
if(*invntry[t].item){
cout << inventory[t].item << '\n';
cout << "Cost $" << invntry[t].cost << '\n';
cout << "Retail Price $" << invntry[t].retail;
cout << "\nOn hand: " << invntry[t].on_hand;
cout << "\nResupply time: ";
cout << invntry[t].lead_time << " days\n\n";
}
}

}

int main () {
//cout << "Enter a name for an inventory: ";

init_inv();

return 0;
}

Paul McKenzie
July 15th, 2007, 12:09 AM
What does your C++ book say how to properly start an implementation?

void Inventory::enter(){

The return type is first.

Another thing:

char *item;

They don't teach std::string in your class? The program has many problems, let alone compiler issues.

cout << "Item: ";
cin >> inventory[i].item;

This is a memory overwrite (plus all of the other places you're doing this). inventory[i].item is a NULL pointer, and you are assigning characters to a NULL pointer. You are mismanaging pointers all over the place. Your program probably won't get passed the input stage without crashing.

Again, use std::string and forget about the char * stuff. Then you won't have these problems.

Regards,

Paul McKenzie

kevinskrazyklub
July 15th, 2007, 02:04 AM
ok i got all of that and i changed a lot of other things, like using the string class instead of char pointers, here is my revised code and i receive this error when trying to compile: /Users/Kevin/Documents/Programming/Xcode/C++/Inventoryclass/main.cpp:193: error: 'init_inv' was not declared in this scope




#include <iostream>
#include <string>
using std::string;
using namespace std;

const int SIZE = 100;
class Item {
string item;
double cost;
double retail;
int on_hand;
int lead_time;

public:
Item(string item, double cost, double retail, int on_hand, int lead_time){
item = item;
cost = cost;
retail = retail;
on_hand = on_hand;
lead_time = lead_time;
}
Item(const Item &obj);
Item();
void setItem(string c){item = c;}
void setCost(double d){cost = d;}
void setRetail(double d){retail = d;}
void setOnHand(int hand){on_hand = hand;}
void setLeadTime(int time){lead_time = time;}
string getItem(){return item;}
double getCost(){return cost;}
double getRetail(){return retail;}
int getOnHand(){return on_hand;}
int getLeadTime(){return lead_time;}


};
/*Item::Item(string item, double cost, double retail, int on_hand, int lead_time){
this->item = item;
this->cost = cost;
this->retail = retail;
this->on_hand = on_hand;
this->lead_time = lead_time;
}*/
Item::Item(){
item = '\0';
}
/*Item::Item(char *item, double cost, double retail, int on_hand, int lead_time)
: item( item ), cost( cost ), retail( retail ), on_hand( on_hand), lead_time( lead_time )
{
}*/

Item::Item(const Item &obj )
: item( obj.item ), cost( obj.cost ), retail( obj.retail ), on_hand( obj.on_hand), lead_time( obj.lead_time )
{
}

class Inventory {
Item inventory[SIZE];
string name;
Inventory(string name);
Inventory(const Inventory &obj);
public:
void init_inv(), enter(), update(), display(), menu(), input(int i);
string getName(){return name;}

};
Inventory::Inventory(string name){
name = name;
Item inventory[SIZE] = inventory;
}
Inventory::Inventory(const Inventory &obj)
: name(obj.name), inventory(obj.inventory){
}
void Inventory::init_inv(){

cout << "Enter name of the inventory: ";
cin >> name;

Inventory inv1 (name);


}
void Inventory::menu(){
char choice;
for(;;){
do{
cout << "Choose one:\n\n";
cout << "(E)nter\n(D)isplay\n(U)pdate\nQuit\n";
cin >> choice;
}while(!strchr("eduq", tolower(choice)));
}
switch(choice){
case 'E': enter();
break;
case 'U': update();
break;
case 'D': display();
break;
case 'Q':return;
}

}

void Inventory::input(int i){
cout << "Item: ";
string s;
cin >> s;
inventory[i].setItem(s);

cout << "Cost: ";
double c;
cin >> c;
inventory[i].setCost(c);

cout << "Retail price: ";
double r;
cin >> r;
inventory[i].setRetail(r);

cout << "Quantity in stock: ";
int q;
cin >> q;
inventory[i].setOnHand(q);

cout << "Lead time before restock(in days): ";
int lt;
cin >> lt;
inventory[i].setLeadTime(lt);
}
void Inventory::enter(){
//Item temp[SIZE+10];
int i;
for(i = 0; i < SIZE;i++){
if(inventory[i].getItem() == "\0")
break;
}
if(i == SIZE){
cout << "List full!\n";

return;
}
input(i);
}
void Inventory::update(){
string item_search;
cout << "enter item to update:";
cin >> item_search;
int i;
for(i = 0; i < SIZE;i++){
//Item it = inventory[i];
if(inventory[i].getItem() == item_search)break;
}
//input(i);
if(i == SIZE){
char choice;
cout << "Item not found. Search again? (y\n): ";
cin >> choice;
if(choice == 'y' || choice == 'Y')
update();
else return;
}
input(i);
char choice;
cout << "Search again? (y/n)";
if(choice == 'y' || choice == 'Y')
update();
else return;





}
void Inventory::display(){
int t;
cout << "inventory name: ";

for(t = 0; t < SIZE; t++){
cout << inventory[t].getItem() << '\n';
cout << "Cost $" << inventory[t].getCost() << '\n';
cout << "Retail Price $" << inventory[t].getRetail();
cout << "\nOn hand: " << inventory[t].getOnHand();
cout << "\nResupply time: ";
cout << inventory[t].getLeadTime() << " days\n\n";
}


}

int main () {

init_inv();

return 0;
}




notice that im using the init_inv() function as the only function in main.

ZuK
July 15th, 2007, 04:34 AM
init_inv() is a member function of Inventory, so you have to call it with an Inventory object .
like this
int main() {
Inventory i("something");
i.init_inv();
}
But then you have made all the constructors of Inventory private so it will not be possible to create any Inventory objects.
If you want to keep the constructors private you will have to add some kind of factory function to create Inventories.

I don't understand what you are trying to do here.
Inventory::Inventory(string name){
name = name;
Item inventory[SIZE] = inventory;
}
You create a local array of SIZE Items and try to initialize it with the member array inventory.

Kurt