Reputation:
I have a slight problem with my new program. I made a class file, and it all went nice and cozy UNTIL! I get this!
C:\Code Block Projects\Not so Advanced Calculator\Calcu.cpp|5|error: expected lified-id before '{' token|
In codeblocks 12.11;
My Calcu.h file:
#ifndef CALCU_H
#define CALCU_H
class Calcu
{
public:
Calcu();
};
#endif // CALCU_H
My Calcu.cpp class file:
#include "Calcu.h"
#include <iostream>
using namespace std;
Calcu{
Calcu(){}
public:
void setNumbers(int x, int y){ no1=x, no2=y; }
int getAddition() {return no1+no2;}
int getSubstraction() {return no1-no2;}
int getDivision() {return no1/no2;}
int getMultiplication() {return no1*no2;}
int getExponent(){ return no1**no2; }
private:
int no1, no2;
};
And my main.cpp:
#include <iostream>
#include <Calcul.h>
using namespace std;
void function_calculator()
{
Calcu calcul;
int noI, noII; char operation;
cout << "Welcome to a more advanced and optimized calculator. ©UnityInc." << endl;
cout << "Please insert your desired numbers. First number should be carefully selected if you wish to raise to the exponent.";
cin>>noI;
cout<<"Great, now insert your other number: ";
cin>>noII;
calcu.setNumbers(noI, noII);
cout<<"Very well, what kind of operation would you like? +, -, *, / or exponantiation(type **): ";
cin>>operation;
if(operation=='+')
calcul.getAddition();
else if(operation=='-')
calcul.getSubstraction();
else if(operation=='/')
calcul.getDivision();
else if(operation=='*')
calcul.getMultiplication();
else if(operation=='**')
calcul.getExponent();
else
return function_calculator;
}
int main()
{
function_calculator;
return 0;
}
Any help would be much appreciated!
Upvotes: 0
Views: 98
Reputation: 29754
My Calcu.h file:
#ifndef CALCU_H
#define CALCU_H
class Calcu
{
public:
Calcu(){}
void setNumbers(int x, int y){ no1=x, no2=y; }
int getAddition() {return no1+no2;}
int getSubstraction() {return no1-no2;}
int getDivision() {return no1/no2;}
int getMultiplication() {return no1*no2;}
int getExponent(){ return //your exponent; }
private:
int no1, no2;
};
#endif // CALCU_H
My Calcu.cpp class file:
#include "Calcu.h"
#include <iostream>
using namespace std;
And my main.cpp:
#include <iostream>
#include <Calcul.h>
using namespace std;
void function_calculator()
{
//...
}
int main()
{
function_calculator();
return 0;
}
Upvotes: 0
Reputation: 2219
here is some recommendation based on your question:
You should not define Calcu
twice in your Calcu.cpp
and Calcu.h
. You should arrange your code that the header file only have the members and interface declaration of the class, and put all the implementations in the Calcu.cpp
file.
#include <header.h>
statement in the code, the C++ preprocessor will only expand the header file at that point. So after expansion, your Calcu.cpp will have two definition of class Calcu
which will have problem.In getExponent()
, C++ does not support **
operator. You should use a math library. (and can not overload **
operator, since C++ only support exist operator overload )
Upvotes: 1
Reputation: 409482
You have two problems: One is in the source file Calcu.cpp
where the error message is about. You're missing a class
or struct
before Calcu
.
The other problem is that you try to redefine the class in the source file. You should put the full definition in the header file, and the implementation of the methods in the source file.
And as mentioned by WhozCraig in a comment, solving the second problem also solves the first.
Upvotes: 3