How to properly use a header file to be a complete class?

前端 未结 2 2195
萌比男神i
萌比男神i 2021-02-20 00:45

(Beginner programmer..) I\'m following the style of a header file that worked fine, but I\'m trying to figure out how I keep getting all of these errors when I compile. I am com

2条回答
  •  [愿得一人]
    2021-02-20 01:39

    How about look on errors? variables and functions can't have same names. And include guard should never names such as class.

    #ifndef INGREDIENT_H
    #define INGREDIENT_H
    
    class Ingredient {
    
    public:
      // constructor
        Ingredient() : name(""), quantity(0) {} 
        Ingredient(std::string n, int q) : name(n), quantity(q) {}
    
      // accessors
        std::string get_name() const { return name; }
        int get_quantity() const {return quantity; }
    
      // modifier
    
    private:
      // representation
      std::string name;
      int quantity;
    };
    
    #endif
    

提交回复
热议问题