-1

Object-oriented C++ here.

I'm supposed to code a Microwave object that "heats" a FrozenMeal object. One method of the Microwave object, called void heatMeal(FrozenMeal), is supposed to take an instance of a FrozenMeal object as a parameter and increase its temperature.

FrozenMeal.h

#include <string>

class FrozenMeal {
public:
    FrozenMeal(std::string, int);

    void setTemperature(double);
    std::string getName() const;
    int getVolume() const;
    double getCoeffizient() const;
    double getTemperature() const;

private:
    std::string name;
    int volume;
    double temperature;
    double coeffizient;
};

FrozenMeal.cpp

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

FrozenMeal::FrozenMeal(string mealName, int mealVolu) {
    name = mealName;
    volume = mealVolu;
    temperature = -18;
    coeffizient = 0.24;
}

void FrozenMeal::setTemperature(double mealTemp) { temperature = mealTemp; }

string FrozenMeal::getName() const { return name; }

int FrozenMeal::getVolume() const { return volume; }

double FrozenMeal::getCoeffizient() const { return coeffizient; }

double FrozenMeal::getTemperature() const { return temperature; }

Microwave.h

#include "FrozenMeal.h"

class Microwave {
public:
    Microwave();

    void morePower();
    void lessPower();
    void setPeriod(double);
    void heatMeal(FrozenMeal); // <----------------------------
    int getPower() const;
    double getPeriod() const;

private:
    int power;
    double period;
};

Microwave.cpp

#include "Microwave.h"
using namespace std;

Microwave::Microwave() {}

void Microwave::morePower() { if (power < 1000) power += 200; }

void Microwave::lessPower() { if (power > 200) power -= 200; }

void Microwave::setPeriod(double sessionPeri) { period = sessionPeri; }

void Microwave::heatMeal(FrozenMeal mealInst) {
    mealInst.setTemperature(80); //example
}

int Microwave::getPower() const { return power; }

double Microwave::getPeriod() const { return period; }

Now, my problem is that my compiler says that the file FrozenMeal.h apparently redefines the object type of FrozenMeal, even though that should be the job of the FrozenMeal.cpp file, and compiling is unsuccessful.

I tried including FrozenMeal.h to Microwave.cpp but that resulted in even more compiler errors.

I feel like I'm doing something horribly wrong here.

  • Recommend adding full error message text. – user4581301 Nov 14 '18 at 23:59
  • That sounds exactly like something I need to do. Thanks for the suggestion I'm going to try that! I'm new to OOP in C++ so I need to look up on how to implement that. – Suayb Yurdakul Nov 14 '18 at 23:59
  • @SuaybYurdakul I think the suggestion was to add the text of the compiler error *to your question*. Error messages help a lot more than descriptions of error messages in diagnosing a problem. – Caleb Nov 15 '18 at 00:09
  • @Caleb unfortunately my compiler spurts out its error messages in german. I'm not sure if those would be helpful. But I'll definitely do that in the future, thanks for the tip! – Suayb Yurdakul Nov 15 '18 at 00:14

1 Answers1

1

Add include guards to your header files so its contents doesn't get included more than once:

FrozenMeal.h:

#ifndef FROZENMEAL_H_INCLUDED
#define FROZENMEAL_H_INCLUDED

// your code ...

#endif /* FROZENMEAL_H_INCLUDED */

Microwave.h:

#ifndef MICROWAVE_H_INCLUDED
#define MICROWAVE_H_INCLUDED

// your code ...

#endif /* MICROWAVE_H_INCLUDED */

Also, you never initialize int Microwave::power and double Microwave::period so you will read and write garbage values in Microwave::morePower() and Microwave::lessPower()

As suggested in the comments, you want to take the parameter of Microwave::heatMeal() by reference so the function can modify the passed object:

void Microwave::heatMeal(FrozenMeal &mealInst)
//                                  ^
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • I removed that because I wasn't sure what was attempting to be done. But yeah need to make it a reference to actually modify the passed in object. – Austin Stephens Nov 15 '18 at 00:04
  • As a side note - there's also `#pragma once` - https://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards – Adam Kotwasinski Nov 15 '18 at 00:06
  • @AdamKotwasinski I've done that instead. Ironically VS included the line #pragma when I created the header file initially, but I didn't think much of it since I don't know anything about preprocessor directives just yet. – Suayb Yurdakul Nov 15 '18 at 00:16