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
Related
This is part of a C++ program based on the Alternative Vote electoral method, using VS2015. I have a class for Party
#pragma once
#ifndef _PARTY_H
#define _PARTY_H
#include <string>
class Party {
public:
Party();
~Party();
Party(std::string n, int pos);
void reset();
void upTotal();
int getPosition();
std::string getName();
int getVotes();
private:
std::string name;
int votes;
int position;
};
#endif
and
#include <iostream>
#include "Party.h"
using namespace std;
Party::Party() {}
Party::~Party() {}
Party::Party(string n, int p) {
name = n;
position = p;
}
void Party::reset() {
votes = 0;
}
void Party::upTotal() {
votes += 1;
}
int Party::getPosition() {
return position;
}
string Party::getName() {
return name;
};
int Party::getVotes() {
return votes;
}
I tried to sort on votes received using (calculated from ballot papers elsewhere in the program
void sortParties() {
sort(parties.begin(), parties.end(), [](const auto& a, const auto& b)
{
return a.getVotes() < b.getVotes();
});
}
which returned illegal operand errors. Moving the variables from private to public and writing the following did work
void sortParties() {
sort(parties.begin(), parties.end(), [](const auto& a, const auto& b)
{
return a.votes < b.votes;
});
}
which gets it working, but I want to write it with proper encapsulation using private variables and an accessor for votes. Do I need to overload somehow, or convert type?
You have the following functions defined:
int getPosition();
std::string getName();
int getVotes();
They should probably all be const; ie
int getPosition() const;
std::string getName() const;
int getVotes() const;
This will allow you to call the functions from your const object at
sort(parties.begin(), parties.end(), [](const auto& a, const auto& b)
I get a compile error "cannot convert 'Chips' to 'GroceryItem' and I'm having trouble figuring out how to actually convert something to a virtual class object. Teacher provided a 'GroceryCart' class that adds GroceryItems to the cart in 'main' and prints the cart out.
class GroceryItem {
public:
virtual ~GroceryItem();
virtual float GetPrice() = 0;
virtual char* GetDescription() = 0;
virtual float GetWeight() = 0;
};
#include "GroceryItem.h"
class Chips : public GroceryItem {
public:
Chips();
~Chips();
float GetPrice();
char* GetDescription();
float GetWeight();
private:
float price;
char* itemName;
float weight;
};
#include "GroceryItem.h"
#include "Chips.h"
Chips::Chips() {
price = 0.75;
itemName = new char[5];
itemName = "Chips";
weight = 1.0;
}
Chips::~Chips() {
delete this;
}
float Chips::GetPrice() {
return price;
}
char* Chips::GetDescription() {
return itemName;
}
float Chips::GetWeight() {
return weight;
}
int main(int argc, const char * argv[])
{
GroceryCart c;
c.AddItemToCart(new Chips);
std::cout << c;
return 0;
}
void GroceryCart::AddItemToCart(GroceryItem *i)
{
items.push_back(i);
}
provide full error log, check that container parameterized with pointer to GroceryItem (<GroceryItem*>)
As someone pointed out
delete[] itemName; //destructor
but you also have to define non-trivial copy-constructor/operator=, so it's better to replace char* with std::string, also need defined destructor for base abstract class even it's pure virtual.
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
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;
}
So, I have an algorithm that takes a few sensors, scales them to a temperature and puts the temps in a global data store. However, sensor class A does more calculations that Class B needs. I can't put the new calcs in the data store, and i don't want to include class A inside class B just to get one piece of data with a getter.
Class A
{
private:
float x[4];
float y[4];
public:
//Scaling functions, etc...
}
Class B
{
private:
float c[4];
public:
//Scaling functions etc...
}
What would be the best way to get x[4] passed to class B to put in c[4]? The real classes have much more going on, this is about as simple as I think I can make. x[4] has data that needs to be used in class B.
class A
{
private:
float x[4];
float y[4];
public:
float* getXs()
{
return x;
}
}
class B
{
private:
float c[4];
public:
//Scaling functions etc...
void setXs(float *x)
{
for (int i=0;i<4;i++)
c[i] = x[i];
}
}
Well, you could use friends, if you're not willing to write accessors:
http://en.wikipedia.org/wiki/Friend_class
Some would argue this breaks encapsulation, and that a getter would be the preferred approach.
Use a getter of x[4] on an instance of A when calling the constructor of B.
#include <string.h>
class A
{
private:
float x[4];
float y[4];
public:
float const *xArray() const
{
return x;
}
};
class B
{
private:
float c[4];
public:
void setCArray(float const arr[4])
{
memcpy(c, arr, 4 * sizeof(int));
}
};
int main()
{
A a;
B b;
b.setCArray(a.xArray());
}
There are number of ways. The best depends on Your criteria.
If time is not crucial for you I would be simple and use copy constructor:
Class A
{
private:
float x[4];
float y[4];
public:
const float& X(int i) { return x[i]; }
}
Class B
{
private:
float c[4];
public:
B( const A& a ) {
for( k = 0; k < 4; k++ )
c[k] = a.X(k);
}
}
If time is crucial you can consider to use pointers copy. But be Very accurate with it:
Class A
{
private:
friend B;
float x[4];
float y[4];
public:
...
}
Class B
{
private:
const float* const c;
public:
B( const A& a ):c(a.x){}
// use c like c[4], but don't change it.
}