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
Related
I want to add two numbers using OOP way. I am a complete newbie in C++ and hence need your help.
#include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
error: #include <iostream>
#include <string>
using namespace std;
class RunwalsClass{
public: // public function
void setName(string x){
name = x;
}
string getName(){
return name;
};
private: // good programming practice to make it private
string name;
};
class MyClass{
public:
void setSaying(string y){
CoolSaying = y;
}
string getSaying(){
return CoolSaying;
}
private:
string CoolSaying;
};
class FavSitcom{
public:
void setSitcom(string z){
BreakingBad = z;
}
string getSitcom(){
return BreakingBad;
}
private:
string BreakingBad;
};
class AddClass{
public:
void setNumbers(int a, int b){
int answer = a + b;
}
int getAddition(){
return answer;
}
private:
int answer;
};
int main(){
RunwalsClass RunwalsObject;
RunwalsObject.setName("Sir Buckey Wallace");
cout << RunwalsObject.getName() << endl;
MyClass MyObject;
MyObject.setSaying("Preaching to the choir! \n");
cout << MyObject.getSaying();
FavSitcom MyNewObject;
MyNewObject.setSitcom("My favorite Sitcom is: Breaking Bad \n");
cout << MyNewObject.getSitcom();
AddClass NewObject;
NewObject.setNumbers("answer: \n");
cout << AddObject.getAddition();
return 0;
}
Reported error:
error: no matching function for call to 'AddClass::setNumbers(const char [10])
note: candidate: void AddClass::setNumbers(int, int)
note: candidate expects 2 arguments, 1 provided.
In main, your are passing a string as parameter to your setNumbers method. This line is wrong in main:
NewObject.setNumbers("answer: \n");
Your setNumbers function neeeds 2 integers as it is declared. Try:
int a = 10;
int b = 5;
NewObject.setNumbers(a, b);
Good luck in your OOP learning journey!
EDIT:
Also, in your setNumbers function, you must not redeclare answer because this variable is a class member. Remove int, just use answer inside setNumbers.
Yep so your function setNumbers() expects two integer arguments provided, i.e. NewObject.setNumbers(5, 10); which would set the number to 15. You have provided a string literal "answer: \n" which is not the same and therefore will not compile.
This question already has answers here:
C++: Initialization of inherited field
(5 answers)
Closed 5 years ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to create a base class with two set, and derived class (inherit) with one set method. I want to combine them in a parameterized constructor.
#include <iostream>
#include <string.h>
using namespace std;
class Code
{
protected:
string letter;
int number;
public:
string getletter();
int getnumber();
void setletter(string letter1);
void setnumber(int number1);
};
void Code::setletter(string letter1)
{
letter=letter1;
}
void Code::setnumber(int number1)
{
number=number1;
}
string Code::getletter()
{
return letter;
}
int Code::getnumber()
{
return number;
}
class Course : public Code
{
private:
string name;
public:
Course(string name1, string letter1, int number1);
string getname();
void show();
};
Course::Course (string name1, string letter1,int number1) :
setletter(letter1), setnumber(number1) //Parameterized Constructor
{
name=name1;
letter=letter1;
number=number1;
}
string Course::getname()
{
return name;
}
void show()
{
Course com("Testing","TST",101);
cout<<"Constructor >>>\n Course Name : "<< com.getname()<<"\n Course Code : "<< com.getletter() << com.getnumber()<<endl;
}
int main()
{
show();
}
If it matters, I am using Microsoft's Visual C++.
You can call them inside your constructor
Course::Course (const string& name1, const string& letter1, const int number1) :
{
name=name;
setletter(letter1); // equivalent to letter=letter1
setnumber(number1);
}
You can add a constructor to your Code class
Code(const string& letter, const int number);
And call it like that in your Course constructor
Course::Course(const string& name1, const string& letter1, const int number1)
: Code(letter1, number1), name(name1)
{
}
But I'm not sure of what your asking, if you can be a little more precise on what you want that could be great, thanks.
The : after the parameter list of a constructor is the 'member initializer list'.
Course::Course (string name1, string letter1,int number1)
: name(name1), letter(letter1), number(number1) { }
Program works but I am not sure what is wrong with constructor since every time program runs it gets this error "warning: base class 'Alat' is uninitialized when used here to access 'Alat::ime' [-Wuninitialized]". I suppose it's something wrong how I called a constructor from base class but I am not sure what is problem. Really need help, tnx in advance.
#include <iostream>
#include <string>
using namespace std;
class Alat{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i);
string getIme();
void setSerBr(int sb);
int getSerBr();
void setCena(int c);
int getCena();
Alat();
Alat(string i, int sb, int c)
:ime(i),
serBr(sb),
cena(c)
{}
void info();
~Alat();
};
#include "Alat.h"
class Rucni : public Alat{
protected:
int minGodKor;
public:
Rucni():Alat(ime, serBr, cena) //I think here is problem, is it wrong called?
{}
int getminGodKor();
void setminGodKor(int min);
void info();
~Rucni();
};
Let the child default constructor call the default parent constructor, and create another child constructor with parameters to call the corresponding one of the parent:
#include <string>
using std::string;
class Alat
{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i)
{
ime = i;
}
string getIme()
{
return ime;
}
void setSerBr(int sb)
{
serBr = sb;
}
int getSerBr()
{
return serBr;
}
void setCena(int c)
{
cena = c;
}
int getCena()
{
return cena;
}
Alat()
{
}
Alat(string i, int sb, int c) : ime(i), serBr(sb), cena(c)
{
}
~Alat()
{
}
};
class Rucni : public Alat
{
protected:
int minGodKor;
public:
Rucni() // implicit call of the parent default constructor
{
}
Rucni(string i, int sb, int c) : Alat(i, sb, c) // explicit call of the corresponding parent constructor
{
}
int getminGodKor()
{
return minGodKor;
}
void setminGodKor(int min)
{
minGodKor = min;
}
~Rucni()
{
}
};
int main()
{
Rucni r;
return 0;
}
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
So I was trying to program a target heart rate calculator in C++ and when I tried to build it, this error was returned. Just to let you know, I am instructed to separate the implementation and interface.
Here's the interface
#include <iostream>
#include <string>
class HeartRates
{
public:
// constructor initialize HeartRates
HeartRates( std::string, std::string, int, int, int );
// Setting each variable
void setFirstName( std::string );
void setLastName( std::string );
void setdobMonth( int );
void setdobDay( int );
void setdobYear( int );
void setCurrentYear( int );
std::string getFirstName() const;
std::string getLastName() const;
int getdobMonth() const;
int getdobDay() const;
int getdobYear() const;
int getCYear() const;
int getAge() const;
int getMaximumHeartRate() const;
int getTargetHeartRate() const;
private:
std::string firstName;
std::string lastName;
int dobMonth;
int dobDay;
int dobYear;
int currentYear;
};
And here's the implementation
#include <iostream>
#include <string>
#include "HeartRates.h"
using namespace std;
// constructor that initializes all the stuff
This is the line that is giving me this error: Out-of-line definition of 'HeartRates' does not match any declaration in 'HeartRates'
HeartRates::HeartRates( string first, string last, int M, int D, int Y, int cY )
{
setFirstName( first );
setLastName( last );
setdobMonth( M );
setdobDay( D );
setdobYear( Y );
setCurrentYear( cY );
}
Continue on with the code
// set functions
void HeartRates::setFirstName( string first )
{
firstName = first; // store the first name in the object
}
void HeartRates::setLastName( string last )
{
lastName = last; // store the last name in the object
}
void HeartRates::setdobMonth( int M )
{
dobMonth = M; // store the month of birth in the object
}
void HeartRates::setdobDay( int D )
{
dobDay = D; // store the day of birth in the object
}
void HeartRates::setdobYear( int Y )
{
dobYear = Y; // store the year of birth in the object
}
void HeartRates::setCurrentYear( int cY )
{
currentYear = cY;
}
// get functions
string HeartRates::getFirstName() const
{
return firstName; // return user's first name
}
string HeartRates::getLastName() const
{
return lastName; // return user's last name
}
int HeartRates::getdobMonth() const
{
return dobMonth;
}
int HeartRates::getdobDay() const
{
return dobDay;
}
int HeartRates::getdobYear() const
{
return dobYear;
}
int HeartRates::getCYear() const
{
return currentYear;
}
// Functions
int HeartRates::getAge() const
{
int age;
age = currentYear-dobYear;
return age;
}
int HeartRates::getMaximumHeartRate() const
{
int maxHeartRate;
maxHeartRate = 220 - getAge();
return maxHeartRate;
}
int HeartRates::getTargetHeartRate() const
{
int targetHeartRate;
targetHeartRate = getMaximumHeartRate() * 0.85;
return targetHeartRate;
}
Sorry for the formatting and copy and pasting everything. Newbie here, I just don't know which part of the code went wrong.
HeartRates( std::string, std::string, int, int, int );
2 strings and 3 int.
HeartRates::HeartRates( string first, string last, int M, int D, int Y, int cY )
2 strings and 4 int.
I had a similar issue today and my fix was slightly different, so I'll throw this here in case it ever helps anyone in the future. I had forward declared a class OUTSIDE of the appropriate namespace in the header, so my error looked something like this:
1. Type of 1st parameter of member declaration does not match definition
'const std::shared_ptr<MyClass> &' (aka 'const shared_ptr<MyClass> &') vs
'const std::shared_ptr<MyClass> &' (aka 'const shared_ptr<myNamespace::MyClass> &'))
The solution for me was to forward declare that class inside the namespace, rather than outside it.
Bad:
class MyClass;
namespace myNamespace {
}
Good:
namespace myNamespace {
class MyClass;
}