Jump to content
  • Advertisement
Sign in to follow this  
Jeff_Eklund

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
Original post by Dave Hunt
What 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;
};



// Funktioner

void 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Jeff_Eklund
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!


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++

edit: made the address a link

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Jeff_Eklund
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?


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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!