i keep getting undefined for the objects created and expected an identifier when initializing the constructor

StackOverflow https://stackoverflow.com/questions/23595889

Question

I've done all the coding but I'm getting errors. for both objects created item1 and item2 it says undefined and for the constructor is says expected an identifier.

Inventory.h

#ifndef Inventory
#define Inventory

class Inventory{
private:
    string name;
    double price;
    int quantity;

public:
    Inventory(); //constructor

    string getName();
    double getPrice();
    int getQuantity();

    void setName(string);
    void setPrice(double);
    void setQuantity(int);

    double totalPrice();
    int decreaseQuantity(int);
    int increaseQuantity(int);
    void displayItemData();

};
#endif

inventory.cpp

#include "Inventory.h"
#include <string>
#include <iostream>
using namespace std;

 Inventory::Inventory(){
    name="null";
    price=0.0;
    quantity=0;
}

string Inventory::getName(){
    return name;
}

double Inventory::getPrice(){
    return price;
}

int Inventory::getQuantity(){
    return quantity;
}

void Inventory::setName(string N){
     N = name;
}

void Inventory::setPrice(double P){
    P = price;
}

void Inventory::setQuantity(int Q){
    Q = quantity;
}

double Inventory::totalPrice(){
    return price*quantity;
}

int Inventory::decreaseQuantity(int x){
    return quantity-x;
}

int Inventory::increaseQuantity(int x){
    return quantity+x;
}

void Inventory::displayItemData(){
    cout<<"Name\tPrice\tQuantity\tTotal"<<endl;
    cout<<getName()<<getPrice()<<getQuantity()<<totalPrice()<<endl;
}

test.cpp

int main(){

Inventory item1, item2;
item1.setName("Keyboard");
item1.setPrice(23.5);
item1.setQuantity(12);

item2.setName("Speakers");
item2.setPrice(48.0);
item2.setQuantity(4);

item1.displayItemData();
item2.displayItemData();

item1.decreseQuantity(2);
item2.decreseQuantity(5);

item1.increaseQuantity(20);
cout<<"Total price of item 1: "<<item1.totalPrice()<<endl;
cout<<"Total price of item 2: "<<item2.totalPrice()<<endl;

return 0;
}
Was it helpful?

Solution

Malformed include guard. Your #define Inventory means that the preprocessor will replace every instance of Inventory with the empty token.

Use:

#ifndef INVENTORY_H
#define INVENTORY_H
// ...
#endif

OTHER TIPS

Use #pragma once instead of header guards.

Pros

  • Your header will be always compatible with any other that can define same guard name too.
  • You don`t have to make redundant definitions.
  • You don`t have to write painful directives both on begin and the very end of every header file.
  • You make your compile time shorter.

Cons

  • It`s non–standard, but widely supported preprocessor directive.

Conclusion

If you want to get rid of all these redundant requirements about header guard from the past millennium, and don`t bother about stricte standards (hey! standards are for people, not people for standards), just use #pragma once directive and forget about any futher unpleasantnesses about obsolescent header guards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top