Can some one help me with this error? - c++

I have to write this program as an assignment. I've been struct with the following error for a week now.Thank in advance.
========Error list==============
1. x,w,y is undefined.
2. For checkin/checkout it says "declaration is in compatible with void employee::checkin/checkout(int,int,int)"
3. In this->Time(a,b,c) it's said type name is not allowed.
P.S. If there is anyway to improve it or something need fixing please tell me that would be very great. Thanks again in advance.
#include <iostream>
#include <iomanip>
#define N 2
using namespace std;
class Time
{
private:int h,m,s;
public:
void time_diff(Time T1,Time T2)
{
Time t;
t.s=(60+T2.s-T1.s)%60;
t.m=(60+T2.m-T1.m+(1-(60+T2.s-T1.s)/60))%60;
t.h=T2.h-T1.h+(1-(60+T2.m-T1.m+(1-(60+T2.s-T1.s)/60))/60);
t.h=t.h<0?t.h+=24:t.h;
*this=t;
}
Time(int hh=0,int mm=0,int ss=0){
s=ss%60;
m=(mm+ss/60)%60;
h=(hh+(mm+ss/60)/60)%24;
}
void set_time(int a,int b,int c){
this->Time(a,b,c);
}
double converter(int hn,int m,int s)
{
int hn,mn,sn;
hn=h*3600;
mn=m*60;
sn=hn+mn+s;
return sn;
}
};
class employee
{
int id;
protected:
Time t_in,t_out;
double wage;
public:
employee(int=0,double=5);
void checkin(int,int,int);//set t_in
void checkout(int,int,int);//set t_out
void display();//display id and total time at work (t_out-t_in )
};
employee::employee(int w,double x)
{
x=wage;
w=id;
}
class staff:public employee{
public:
double get_paid();//wage*(t_out-t_in);
staff(int=0,int=5);//id,wage
};
void employee::checkin()
{
t_in.set_time(x,w,y);
}
void employee::checkout()
{
t_out.set_time(x,w,y);
}
double staff::get_paid()
{
int paid,load;
Time wo;
wo.time_diff(t_out,t_in);
load=wo.converter;
paid=load*wage;
}

If you really need set_time, try this::
class Time
{
...
void set_time(int a, int b, int c) {
*this = new Time(a, b, c);
}
...
}
... however I think set_time is useless because you have the constructor already.
In checkin/checkout implementation you have to repeat the same firm of the declaration, so:
void employee::checkin(int x, int w, int y)
{
t_in.set_time(x, w, y);
}
void employee::checkout(int x, int w, int y)
{
t_out.set_time(x, w, y);
}
Indeed the constructor employee is wrong because you set local arguments x and w with the same values of member fields wage and id but these values are zeros and remain so.
You would probably like do the inverse, so:
employee::employee(int w, double x)
{
wage = x;
id = w;
}

Related

Is there a way to use one function to replace two similar functions in C++

The following code compiles and runs fine. But, to me, there are repeated code in funtions workflow1 and workflow2.In fact, the only differences are the added parameter y in workflow2, and the different calls to method1/method2. Is there a way to use one function to replace those two? Maybe there is a way to pass a function (workflow1 or workflow2) and its parameters as parameters to another general function (say, workflow)? Maybe there exist some design patterns for this kind of job?
class MyClass
{
public:
void workflow1(int x) {
doThings(); // actually some code more than just one function call
method1(x);
doOtherThings(); // actually some code more than jus one function call
}
void workflow2(int x, int y=1) {
doThings(); // actually some code; the same as in workflow1()
method2(x,y);
doOtherThings(); // actually some code; the same as in workflow1()
}
private:
void doThings() {}
void doOtherThings() {}
void method1(int x) {}
void method2(int x, int y) {}
};
int main()
{
MyClass myClass;
int x = 0;
myClass.workflow1(x);
int y = 2;
myClass.workflow2(x, y);
}
Two options:
Use a lambda approach:
#include <functional>
class MyClass
{
public:
void workflow1(int x) {
workFlowInternal([x, this] {method1(x); });
}
void workflow2(int x, int y = 1) {
workFlowInternal([x, y, this] {method2(x,y); });
}
private:
void workFlowInternal(const std::function<void()> &func) {
doThings();
func();
doOtherThings();
}
void doThings() {}
void doOtherThings() {}
void method1(int x) {}
void method2(int x, int y) {}
};
Or, use a pointer to member function:
class MyClass
{
public:
void workflow1(int x) {
workFlowInternal(&MyClass::method1, x, 0);
}
void workflow2(int x, int y = 1) {
workFlowInternal(&MyClass::method2, x, y);
}
private:
void workFlowInternal(void(MyClass::* func)(int, int), int x, int y) {
doThings();
(this->*func)(x,y);
doOtherThings();
}
void doThings() {}
void doOtherThings() {}
void method1(int x, int ignored=0) {}
void method2(int x, int y) {}
};
You can just use std::optional:
#include <optional>
class MyClass
{
public:
void workflow(int x, std::optional<int> y = {}) {
doThings();
if (!y) method1(x);
else method2(x, *y);
doOtherThings();
}
private:
void doThings() { }
void doOtherThings() { }
void method1(int x) { }
void method2(int x, int y) { }
};
int main() {
MyClass myClass;
int x = 0;
myClass.workflow(x);
int y = 2;
myClass.workflow(x, y);
}
Demo.
You can write a templated function like this:
template <bool workflow1>
void Workflow(int x, int y=1)
{
doThings();
if constexpr (workflow1)
{
method1(x);
}
else
{
method2(x, y);
}
doOtherThings();
}
int main()
{
int x = 0;
Workflow<true>(x);
int y = 2;
Workflow<false>(x, y);
}
if constexpr statements are decided at compile time, so this will generate two different functions, but you won't have to rewrite the code that is the same. The template argument can be changed from a bool to an enum or another type if you need to support more than two types.

class inheritance and using setters and getters

Here I've made a derived class called Essay, from the base class GradedActivity. I've made an object of the Essay class in main called object. When I wrote object.setGrammar(grammarPts) in main(), I'd hoped to feed what the score is to be held in the variable grammar in the setGrammar() function. What am I doing wrong? Thanks!
I get one error:
99 8 F:\lab6part3.cpp [Error] request for member 'setGrammar' in 'object', which is of non-class type 'Essay(float, float, float, float)'
#include <iostream>
using namespace std;
//class gradedactivity (page 900)
class GradedActivity
{
protected:
double score;
public:
//default constructor
GradedActivity()
{
score = 0.0;
}
//parameterized constructor
GradedActivity(double s)
{
score = s;
}
setScore(double s)
{
score = s;
}
double getScore() const
{
return score;
}
char getLetterGrade() const;
};
class Essay : public GradedActivity
{
private:
float grammar;
float spelling;
float length;
float content;
public:
Essay(float g, float s, float l, float c)
{
setGrammar(g);
setSpelling(s);
setLength(l);
setContent(c);
}
void setGrammar(float);
float getGrammar();
void setSpelling(float);
float getSpelling();
void setLength(float);
float getLength();
void setContent(float);
float getContent();
};
void Essay::setGrammar(float g)
{
grammar = g;
}
float Essay::getGrammar() {return grammar;}
void Essay::setSpelling(float s)
{
spelling = s;
}
float Essay::getSpelling() {return spelling;}
void Essay::setLength(float l)
{
length = l;
}
float Essay::getLength() {return length;}
void Essay::setContent(float c)
{
content = c;
}
float Essay::getContent() {return content;}
int main()
{
float grammarPts;
cout << "How many points, out of 30, did the student get for grammar?";
cin >> grammarPts;
Essay object;
object.setGrammar(grammarPts);
return 0;
}
This could just be because you never defined a default constructor for Essay.
Anyway, I defined a default constructor and your code runs fine so that might be the issue. https://ideone.com/yNxV8N

c++ - declaration is incompatible with (method) - pointers

I recently started playing around with c++, but for I don't understand what this means:
I get this error:
declaration is incompatible with "void
student_system::addStudent(<error-type> z)" (declared at line 31)
and the same goes for removeStudent and editStudent
I copied over the "function()" from another stackoverflow post, which seems fine and added the header with no issues, but my own "student" methods don't seem to work, I do not understand why,
I also tried adding the pointers rather than the variable but that didn't work either ( and by pointer I mean " student *x ").
#include "database.h"
#include <vector>
int main()
{
return 0;
}
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x) // <------------- LINE 31
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
Put this bit of code
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
Just after the
#include <vector>
So that student_system and its definitions know about it
Add class student; above your student_system class definition.
Edit
Using forward declarations we can only declare methods in which we can use incomplete type not define. So, my first answer was wrong, but the following code will work.
int main()
{
return 0;
}
// Forward Declaration
class student;
// ================================
// Class student_system
// ================================
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
// ================================
// Class student
// ================================
class student
{
private:
string name, surname, ID;
int sid;
public :
student(int sid, string n, string s, string id);
};
// ================================
// Definition of methods
// ================================
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x)
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
1) Please simplify a problem down to its basest elements before posting.
2) Please don't post code that relies on non-provided includes or implicit "using" statements or the like
3) "student" should be declared before student_system. The fact that you're not getting more errors surprises me, but maybe you're doing something unseen in database.h

c++ nested class with 1 of the object as private

I have two classes, for example class A and B. B is encapsulation in A under private;
class A
{
private:
int x;
int y;
B b;
public:
void set(int , int, int, int, int);
void setX(int);
void setY(int);
string toString();
};
void A::set(int high, int low, int middle)
{
B(high, low, middle);
setX(x);
setY(y);
}
void A:: setX(int x)
{
this -> x = x;
}
void A:: setY(int y)
{
this -> y = y;
}
string A::toString()
{
string str;
ostringstream convert;
convert << getlow();
str = convert.str();
return str;
}
class B
{
private:
int low;
int middle;
int hight;
public:
B();
B(int, int, int);
int getLow();
int getMiddle();
int getHigh();
};
in another class with my int main
int main ()
{
int test1,test2,test3,test4,test5;
// with lots of codes
A a;
a.set (test1,test2,test3,test4,test5);
}
When i get some values from int main, i pass in the 3 values into set, which initialize object B. when i use the getB function, all i get is values of 0, or it doesn't appear at all. (I have a constructor that takes in arguments and set all int to 0). Can someone enlighten me? And please do not tell me not to use this. I'm new to C++ so guide me along.
Change the definition of class A at least the following way
class A
{
private:
B b;
int x;
int y;
public:
void set(int, int, int);
int getB();
};
void A::set(int high, int low, int middle)
{
b = B(high, low, middle);
}
int A::getB()
{
return b.getLow();
}
in your set you don't initialize b member variable:
A::set(int high, int low, int middle)
{
B(high, low, middle);
}

Templating with user defined classes as a parameter

I'm having a bit of trouble with templating and composition-style coding. I have an object being created inside the constructor of another with the *this parameter. Sorry, if I'm being unclear. The code is as below:
In the outer.h file:
class outer {
public:
outer(int w, int l);
int getWidth();
int getLength();
private:
inner<outer> test(*this);
int width;
int length;
};
outer::outer(int w, int l) {
width = w;
length = l;
}
int outer::getLength() {
return length;
}
In the inner.h file
template<typename T>
class inner {
public:
inner(T &name);
private:
int top;
int bot;
};
template<typename T>
inner<T>::inner(T &name) {
top = name.getLength() /2;
bot = -name.getLength() / 2;
}
I don't know if this is allowed as I can't find anything online that addresses this. The compiler is having problems with the *this statement in outer.h.
Thanks in advance for your help.
If you're using C++03, you must perform initial assignments in the constructor.
class outer {
public:
outer(int w, int l);
int getWidth();
int getLength();
private:
// Member variables are initialized in the order they are declared here.
int width;
int length;
inner<outer> test;
};
outer::outer(int w, int l)
: width(w)
, length(l)
, test(*this)
{
}
Edit: Kerrek SB also observes that the order of your variables need to be changed. They are initialized in the order you declare them in the class and test needs to be initialized last, so the other variables are initialized.