Public Group

# problem with constructor

This topic is 5005 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Quote:
 Original post by Dave HuntWhat do you have in student.h?

/* Skapar elever, tilldelar dem kön samt betyg */#ifndef STUDENT_H#define STUDENT_H#include <iostream>class student{	public:		student();		virtual ~student();		void setGender(); // FALSE=boy, TRUE=girl		void giveGrade();		float getGrade(){return grade;};		bool getGender(){return gender;};	protected:	private:        bool gender;        double grade;};// Funktionervoid student::setGender(){	bool sex;	std::cout << "Specify sex of student; 1 (girl) or 0 (boy): ";	std::cin >> sex;	gender=(sex=0?0:1);}void student::giveGrade(){    std::cout << "Specify student grade: (1-5) ";    std::cin >> grade;}#endif // STUDENT_H

##### Share on other sites
I pasted your calc.h and student.h into my own and created a crap.cpp that included calc.h. I don't get any errors. Either you've done something strange in your .cpp(s), you've done something strange in the compiler command line, or your file(s) is/are corrupted.

There is nothing wrong with the files you've posted. Some warnings about int-to-float conversion, but no errors.

##### Share on other sites
I was having simaler issues earlier with a .h file what you should do is make a .cpp file for all your classes then do all the function definitions in there that should solve your problem

so a klass.h and klass.cpp

and a student.h and student.cpp in this case

##### Share on other sites
I also tried pasting my calc.h and student.h-codes into new text files and compiling them. Worked fine. However, when i tried doing the same thing with my main.cpp I got alot of error once again...
My main.cpp looks like this:
#include <iostream>#include "student.h"#include "calc.h"using std::cin; using std::cout;using std::endl;bool menu();int main(int argc, char *argv[]){    klass klass;        // Initiera ett objekt, "Klass", av klassen Klass    int SEL;    cout << "Menu selection:\n1.Add student data\n2.Randomize student data\n3.Exit" << endl;    cin >> SEL;    switch(SEL)    {        case 1:             klass.addstudent();        break;        case 2:        break;        case 3:            return 0;        default:            cout << "ERROR" << endl;	}    	klass.collect();	klass.calcaverage();	cout << "de " << klass.numBo() << " killarnas medel \x84r " << klass.AvGradeBoys() << "\n\n";	cout << "de " << klass.numGi() << " tjejernas medel \x84r " << klass.AvGradeGirls() << "\n\n";	cout << "de " << (klass.numGi()+klass.numBo()) << " studenternas medel \x84r " << klass.averagegrade() << endl;	    cin.ignore(255,'\n');    cin.get();    return 0;}

##### Share on other sites
If you provide implementations for functions inside your header files, you will need to declare them as 'inline' in your class definition. In this case, collect(), calcaverage(), setGender(), and giveGrade() need to be declared as inline.

Otherwise, you should move those method definitions into your calc.cpp/student.cpp files.

##### Share on other sites
Okey, I'm not familiar with inline functions, what do I need to do? And moving my method definitions to student.cpp or calc.cpp... Could you perhaps show me an example of this so I know where I am to put everything?
Excuse me if I'm being a nuisance, but I just want to get it right, so I can stop bugging you guys ;-)
Anyway, thanks a bunch for all your help!

##### Share on other sites
Quote:
 Original post by Jeff_EklundOkey, I'm not familiar with inline functions, what do I need to do? And moving my method definitions to student.cpp or calc.cpp... Could you perhaps show me an example of this so I know where I am to put everything?Excuse me if I'm being a nuisance, but I just want to get it right, so I can stop bugging you guys ;-)Anyway, thanks a bunch for all your help!

I found this to be a well done article right here at gamedev. It covers the basics pretty well.

Organizing code files in C and C++

##### Share on other sites
Hm I'm getting kind of tired from a pretty rough day, but I think I might have understood something from that link. I tried doing like this; including student.h, calc.h and calc.cpp into main.cpp and it compiles with just one exception; error: expected primary-expression before '.' token, at every point in my main.cpp where klass is mentioned.
Some ideas to what caused this?

##### Share on other sites
Quote:
 Original post by Jeff_EklundHm I'm getting kind of tired from a pretty rough day, but I think I might have understood something from that link. I tried doing like this; including student.h, calc.h and calc.cpp into main.cpp and it compiles with just one exception; error: expected primary-expression before '.' token, at every point in my main.cpp where klass is mentioned.Some ideas to what caused this?

I suspect it's that you have a variable with the same name as a class. Specifically, you define a class called klass, and then created a variable called klass of the type klass. You should probably rename your variable to something that isn't a type already.

##### Share on other sites
You were right, but now I'm getting the good old "multiple definitions" and "undefined reference to"-errors instead... AH! This is driving me mad!
I'm too tired for this now...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 16
• 13
• 13
• 9
• ### Forum Statistics

• Total Topics
634453
• Total Posts
3017547
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!