If you run main.cpp or item.cpp, you will see an undefined reference error I can't fix it any more here. I would appreciate it if you could tell me what the problem is.
main.cpp
---------------
#include <iostream>
#include "Item.h"
int main(){
Item item1{ "Potato Chips", 1.78 };
Item item2{ "Doritos", 1.99, 2 };
std::cout << item1.get_name() << '\n';
std::cout << "Original " << item2.get_name() << '\n';;
std::cout << "Price: " << item2.get_price() << '\n';
item2.set_price(2.49);
std::cout << "Updated " << item2.get_name() << '\n';;
std::cout << "Price: " << item2.get_price() << '\n';
return 0;
}
Item.h
-----------
#ifndef ITEM_H
#define ITEM_H
#include <string>
class Item {
public:
Item(std::string name, double price, int quantity=1);
std::string get_name() const;
double get_price() const;
int get_quantity() const;
void set_price(double new_price);
void set_quantity(double new_quanity);
private:
std::string name;
double price;
int quantity;
};
#endif
Item.cpp
--------------
#include "Item.h"
#include <iostream>
using std::string;
Item::Item(std::string name, double price, int quantity){
this->name = name;
this->price = price;
this->quantity = quantity;}
string Item::get_name() const{
return name;
}
double Item::get_price() const{
return price;
}
int Item::get_quantity() const{
return quantity;
}
void Item::set_price(double new_price){
price = new_price;
}
void Item::set_quantity(double new_quanity){
price = new_quanity;
}
I turned it around, but there was no error and it ran well.
It's not a code problem, but a computer or integrated development environment problem.
© 2024 OneMinuteCode. All rights reserved.