0

Today I have tried to make a class named calculator, for a physics project and as the name suggests, my class would just do calculations. Being a program for physics, it's expected to need calculus formulas; and they would be calculated using parameterized functions, returning a string consisting of a double and the measuring unit like this: 177 J/Kg*K

Now, let's get to the problem:

So i have tested my functions, and after that I decided to make it into Object Oriented Programming as it would make things a little bit simpler for me, at least... After making the class, I got an error saying that

"string" in "class Calculator" doesn't define a type

Here is the code:

Calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <iostream>

using namespace std;

class Calculator{
private:

public:
    Calculator();
    string capCalorica(double cReceived, double varTemp);
    string caldSpecifica(double cReceived, double varTemp, double mass);
};

#endif // CALCULATOR_H

Calculator.cpp

#include "Calculator.h"
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

Calculator::Calculator(){
    cout<<"Calculus Module Initialized!"<<endl;
    Sleep(2000);
    system("cls");
}

Calculator::string capCalorica(double cReceived, double varTemp)){
    stringstream sstr;
    double result = cReceived/varTemp;

    sstr<<result<<" J/Kg";

    return sstr.str();
}

Calculator::string caldSpecific(double cReceived, double mass, double varTemp){
    stringstream sstm;
    double result = cReceived/(mass*varTemp);

    sstm<<result<<" J/Kg*K";

    return sstm.str();
}

The error is in the cpp file, wherever i defined a string returning function

Zentrix
  • 13
  • 4

2 Answers2

1

change

Calculator::string capCalorica(double cReceived, double varTemp)){
...
}

to

std::string Calculator::capCalorica(double cReceived, double varTemp)){
...}

etc

pm100
  • 48,078
  • 23
  • 82
  • 145
0

Your declaration string capCalorica(double cReceived, double varTemp); in the header file uses string as a type, yet this type is not known at this point. You simply forgot to #include<string> in the header file.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • #ifndef CALCULATOR_H #define CALCULATOR_H #include #include using namespace std; class Calculator{ private: public: Calculator(); string capCalorica(double cPrimit, double varTemp); string caldSpecifica(double cPrimit, double varTemp, double masa); }; #endif // CALCULATOR_H Did nothing..... – Zentrix Nov 02 '18 at 22:41
  • @Zentrix, The code in your comment [compiles here](https://gcc.godbolt.org/z/6KGjdI). – chris Nov 02 '18 at 22:48
  • yes, no problem in the header, that's right! I said that the IDE signals an error `Calculator::string capCalorica(double cReceived, double varTemp)` here and `Calculator::string caldSpecifica(double cReceived, double mass, double varTemp)` here – Zentrix Nov 02 '18 at 22:54
  • Note that `Calculator::string` is something different than `std::string`. Your IDE probably assumes an `Calculator::string` (which is not defined then), as you forgot to include a header defining `string` in the namespace `std` (as used then in the cpp-file) – Stephan Lechner Nov 02 '18 at 22:56
  • yes, but i used USING NAMESPACE STD; at the top of the CPP file, so what should be the problem? – Zentrix Nov 02 '18 at 22:57
  • The problem is that there is NO `Calculator::string`. – Swordfish Nov 02 '18 at 22:58
  • `using` is a shortcut that allows you to write `string` instead of `std::string`, yet it does not define the type `string`; for that, you need the #include first. – Stephan Lechner Nov 02 '18 at 22:58
  • @Swordfish yes, you are right... the problem was the wording. i should have written `string Calculator::caldSpecifica` . Thanks for waking me up, mates! – Zentrix Nov 02 '18 at 23:04
  • @Zentrix a note about `using namespace std;`. It can be more trouble than it's worth. More on that here: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). As noted in the comments under the question, using it without scoping it in a header is extremely frowned on as turns the header into a boobytrap for anyone who wrote their code expecting proper namespace behaviour and includes the header. – user4581301 Nov 02 '18 at 23:11