problem with constructor

Started by
27 comments, last by Jeff_Eklund 18 years, 10 months ago
I get this strange error with my constructor:
klass::klass(numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0))
{
}

I get the following error: line message 3 error: expected ´)' before'(' token 3 error: expected ´,' or ´;' before ')' token i'm using the MingWcompiler under windows. Any help appreciated! Thanks!
Advertisement
Try:
klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0){}

Not sure what exactly you're trying to do there, but if numG, numB etc are member variables of klass, then I would assume you're attempting base member initialization.

klass::klass() : numG(0) , numB(0) , AvGradeG(0) , AvGradeB(0) , totGrade(0){}


Edit: Beaten to it by Dave! I need to improve my ninja posting skills.
Thanks guys. I've been stuck with this one for a while, untill it hit me that I included the wrong file... That was the crook.
However, I have another problem.
I get
3:error: Definition of implicitly-declared `klass::klass()'
5:error: expected ´)' before "klass"
6:error: Definition of implicitly-declared `klass::klass()'

from the following file:

calc.cpp
#include "calc.h"klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0)klass::~klass(){}

the class, "Klass" (meaning just class in Swedish) is in calc.h
#include "student.h"#define students 20class klass{    public:        float AvGradeGirls(){return AvGradeG;};// average grade for girls        float averagegrade(){return totGrade;};	    // total average        float AvGradeBoys(){return AvGradeB;}; // average grade for boys         void addstudent();        int numBo(){return numB;};				    // number of B        int numGi(){return numG;};				    // number of G        int totalStudents(){return numG+numB;};	    // total number of students        void collect();        void calcaverage();	protected:    private:        student stud[students];        int Ggrade;         int Bgrade;     // Girls and boys total grade (not average)        int numG;        int numB;        double AvGradeB;        // boys grade (av)        double AvGradeG;        // girls grade(av)        double totGrade;        // Total av grade};void klass::collect(){	for(int i=(numG+numB);i>0;i--)	{		if(stud.getGender())    // if getGender() = TRUE = girl...		{			Ggrade=Ggrade+stud.getGrade();			numG++;		}		else                    // else boy		{			Bgrade=Bgrade+stud.getGrade();			numB++;		}	}}void klass::calcaverage(){	if(numG>0)                  // No division by zero		AvGradeG=Ggrade/numG;	if(numB>0)                  // No division by zero		AvGradeB=Bgrade/numB;	if((numB+numG)>0)           // No division by zero		totGrade=(Bgrade+Ggrade)/(numB+numG);}


If you're wondering, I'm making a try at nested classes, and I'm doing a program whoch handels grades of students in a class and calculates average grade both amongst all of the students and for boys and girls separately.
Please, come with suggestions for improving or making my program better in any way, since I am truly a beginner at this class-concept. Please, teach me.

EDIT: Translated my comments into English. Oh, sorry 'bout any badly used English, I am Swedish, as you might have figured... ;-)
Well there are a couple of problems here.

First, you left off the curly braces in your klass::klass() definition in calc.cpp.


Second, you haven't declared a constructor for klass in calc.h.

Thanks! Helped a bunch!
I still have one error left though, (calc.cpp) 8:error: Definition of implicitly-declared `klass::~klass()'

calc.cpp looks like this now:
#include "calc.h"klass::klass() : numG(0), numB(0), AvGradeG(0), AvGradeB(0), totGrade(0){}klass::~klass(){}

Does this mean I need to declare the destructor in calc.h too?
Yes. If you provide a definition for the constructor, then you have to declare it in your class. Without the declaration, the compiler expects to create one for you implicitly. That is why it's complaining that you've provided a definition.
And same goes for destructor.
Declare ~klass() in your class definition.
Thanks.
Please don't kill me, but I keep getting error that I don't know how to solve...

Errors: (calc.h)

5: error: redefinition of `class klass'
5: error: `class klass' previously defined here
31: error: redefinition of `void klass::collect()'
31: error: `void klass::collect()' previously defined here
48: error: redefinition of `void klass::calcaverage()'
48: error: `void klass::calcaverage()' previously defined here


calc.h
#ifndef CALC_H#define CALC_H#include "student.h"#define students 20class klass{    public:        klass();        ~klass();        float AvGradeGirls(){return AvGradeG;};// average grade for girls        float averagegrade(){return totGrade;};	    // total average        float AvGradeBoys(){return AvGradeB;}; // average grade for boys         void addstudent();        int numBo(){return numB;};				    // number of B        int numGi(){return numG;};				    // number of G        int totalStudents(){return numG+numB;};	    // total number of students        void collect();        void calcaverage();    protected:    private:        student stud[students];        int Ggrade;         int Bgrade;     // Girls and boys total grade (not average)        int numG;        int numB;        double AvGradeB;        // boys grade (av)        double AvGradeG;        // girls grade(av)        double totGrade;        // Total av grade};void klass::collect(){	for(int i=(numG+numB);i>0;i--)	{		if(stud.getGender())    // if getGender() = TRUE = girl...		{			Ggrade=Ggrade+stud.getGrade();			numG++;		}		else                    // else boy		{			Bgrade=Bgrade+stud.getGrade();			numB++;		}	}}void klass::calcaverage(){	if(numG>0)                  // No division by zero		AvGradeG=Ggrade/numG;	if(numB>0)                  // No division by zero		AvGradeB=Bgrade/numB;	if((numB+numG)>0)           // No division by zero		totGrade=(Bgrade+Ggrade)/(numB+numG);}#endif // CALC_H
What do you have in student.h?

This topic is closed to new replies.

Advertisement