Reputation:
I'm a bit stuck on my homework and could use a little help. I'm definitely not looking for someone to do my homework for me, but I sure could use some suggestions.
I am really sorry for posting on a topic that has already been posted on dozens of times. I've read through what I could find and I'm still stuck.
The program has 3 files associated with it - app.cpp has main, student.h has function prototypes and class definition. student.cpp has all of the functions.
We just started learning about classes, and this assignment is just an exercise using a class. It compares two student IDs to find which is less, and then checks to see if the gpa associated with the ID is high enough for honors.
When I try to compile it, I get a bunch of errors about not being declared in scope. My understanding is that this error happens when something is not declared ahead of time. But I can't figure out what that might be as everything is declared. I had the tutor at school take a quick look at it and she didn't see what the problem was. I'm going to post my 3 files and my error messages - I hope it's not too much!
I really, really appreciate any help!
app.cpp
#include "student.h"
int main()
{
Student s1("G10", 3.9);
Student s2("G20", 3.5);
isLessThanByID(s2);
qualifyForHonor(minGpaForHonor);
s1.print();
s2.print();
return 0;
}
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student
{
public:
Student(const char initId[], double gpa);
bool isLessThanByID(const Student& aStudent) const;
bool qualifyForHonor(double minGpaForHonor) const;
void print()const;
private:
const static int MAX_CHAR = 100;
char id[MAX_CHAR];
double gpa;
}; double minGpaForHonor = 3.75;
#endif
student.cpp
void Student::Student(const char initId[], double gpa)
{
id = initId[];
}
bool Student::isLessThanByID(const Student& aStudent) const
if(s1.isLessThanByID (s2) == > 0)
cout << "s1 is greater than s2" << endl;
}
bool Student::qualifyForHonor(double minGpaForHonor, double gpa) const
{
if (gpa >= minGpaForHonor)
{
return true;
}
}
void Student::print() const
{
cout << id << '\t' << gpa << endl;
}
errors
g++ -Wall -g -std=c++11 -c -o app.o app.cpp
app.cpp: In function ‘int main()’:
app.cpp:8:2: error: ‘isLessThanByID’ was not declared in this scope
isLessThanByID(s2);
^~~~~~~~~~~~~~
app.cpp:9:2: error: ‘qualifyForHonor’ was not declared in this scope
qualifyForHonor(minGpaForHonor);
^~~~~~~~~~~~~~~
app.cpp:9:2: note: suggested alternative: ‘minGpaForHonor’
qualifyForHonor(minGpaForHonor);
^~~~~~~~~~~~~~~
minGpaForHonor
<builtin>: recipe for target 'app.o' failed
make: *** [app.o] Error 1
Upvotes: 1
Views: 4891
Reputation: 311126
These functions
isLessThanByID(s2);
qualifyForHonor(minGpaForHonor);
are non-static member functions of the class Student. So they shall be called with an object of the class the same way as the member function print is called
s1.print();
s2.print();
Moreover these member functions have undefined behavior because they either return nothing or in some path of execution return nothing.
bool Student::isLessThanByID(const Student& aStudent) const
{ // <=== you forgot the open brace.:)
if(s1.isLessThanByID (s2) == > 0)
cout << "s1 is greater than s2" << endl;
}
bool Student::qualifyForHonor(double minGpaForHonor, double gpa) const
{
if (gpa >= minGpaForHonor)
{
return true;
}
}
And the first function recursively calls itself in this statement
if(s1.isLessThanByID (s2) == > 0)
using the undeclared identifier s2.
The functions can be defined for example the following way
bool Student::isLessThanByID(const Student& aStudent) const
{
return strcmp( id, aStudent.id ) < 0;
}
bool Student::qualifyForHonor(double minGpaForHonor ) const
{
return minGpaForHonor <= gpa;
}
Also pay attention to that this variable
double minGpaForHonor = 3.75;
is defined twice because its definition is included in the header.
You could declare the variable either like
const double minGpaForHonor = 3.75;
or
inline const double minGpaForHonor = 3.75;
Upvotes: 1
Reputation: 729
It seems that you are confusing the scopes. For example, consider this class, and how is the proper way of invoking its methods.
class Foo {
void Bar1();
static void Bar2();
};
Foo foo;
foo.Bar1();
foo::Bar2();
This is also valid for variables. Therefore you should use s1.isLessThanByID(s2);
and s1.qualifyForHonor(minGpaForHonor);
instead.
Your function qualifyForHonor
should be static or left out of the class, since it does not use any member, just the parameters passed to it.
Also, this line is a bit strange for me: if(s1.isLessThanByID (s2) == > 0)
, what were you trying to achieve in here?
Upvotes: 0
Reputation: 1675
isLessThanByID
and qualifyForHonor
are member functions in the class Student
, therefore should be invoked on a instance of class Student
.
isLessThanByID(s2);
qualifyForHonor(minGpaForHonor);
should be changed to
s1.isLessThanByID(s2);
s1.qualifyForHonor(minGpaForHonor);
Having said this, you apperantly have more errors than this. The function declaration bool qualifyForHonor(double minGpaForHonor) const
in Student
class doesn't match with its signature in the definition as well.
Upvotes: 1