I'm trying to get my implementation file(main) and the code files separated. Heres what I have so far, I copied it out of my deitel how to program C++ 8th edition. I am using windows 7 64 bit, code::blocks, and mingw C++ compiler.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//#include <iostream>
//using namespace std;
#include "GradeBook.h"
int main()
{
GradeBook myGradeBook("CS101 C++ Programming");
myGradeBook.displayMessage();
myGradeBook.inputGrades();
myGradeBook.displayGradeReport();
return 0;
}
#include <iostream>
usingnamespace std;
#ifndef GRADEBOOK_H_INCLUDED
#define GRADEBOOK_H_INCLUDED
class GradeBook
{
public:
GradeBook(string); //constructor
void setCourseName(string); // function to set name
string getCourseName(); //gets the course name
void displayMessage(); //displays welcome message
void inputGrades(); //input grades method
void displayGradeReport(); //displays the grade report
private:
string courseName;
int aCount;
int bcount;
int cCount;
int dCount;
int Fcount;
};
#endif // GRADEBOOK_H_INCLUDED
#include <iostream>
#include "GradeBook.h"
usingnamespace std;
//constructor
GradeBook::GradeBook(string name)
{
setCourseName(name);
aCount = 0;
bcount = 0;
cCount = 0;
dCount = 0;
Fcount = 0;
}// end GradeBook constructor
//sets the name and checks limit size if need be
void GradeBook::setCourseName(string name)
{
if (name.length() <= 25)
courseName = name;
else
{
courseName = name.substr(0, 25); //gets the first 25 characters
cout << "Name \"" << name << "\"exceeds maximum lenght(25). \n
<< "Limmiting courseName to first 25 charcters. \n" << endl;
}//end if..else
}// end setCourseName
//retrives course name
string GradeBook::getCourseName();
{
return courseName;
}// ends getCourseName;
void GradeBook::displayMessage()
{
cout << "Welcome to grade book for \n" << getCourseName() << "!\n" << endl;
}// end displayMessage
void GradeBook::inputGrades()
{
int grade;
cout << "Enter letter grades" << endl;
cout << "Enter EOF to end" << endl;
//loop
while( (grade = cin.get() ) != EOF)
{
//awitch statement!
switch(grade)
{
case 'A':
case 'a':
++aCount;
break;
case 'B':
case 'b'
++bcount;
break;
case 'C'
case 'c'
++cCount;
break;
case 'D'
case 'd'
++dCount;
break;
case 'F'
case 'f'
++Fcount
break;
case '\n':
case '\t':
case ' ':
break;
default:
cout << "Nigga, that ain't no grade!" << " Enter a real grade nigga! " << endl;
break;
} // end switch
} //end while
} // end inputGrades
void GradeBook::displayGradeReport()
{
cout << "\nNumber of studnents who got what"
<<"\nA :" << aCount
<< "\nB:" << bcount
<< "\nC:" << cCount
<<"\n D:" << dCount
<< "\n F:" << Fcount
<< endl;
}// end displayGradeReport
When I try to compile I get these errors
main.cpp|11|undefined reference to `GradeBook::GradeBook(std::string)'
main.cpp|13|undefined reference to `GradeBook::displayMessage()'
main.cpp|14|undefined reference to `GradeBook::inputGrades()'
main.cpp|15|undefined reference to `GradeBook::displayGradeReport()'
Now here is the weird thing, I when I forgo the GradeBook.cpp and just put all the code in GradeBook.h(which I'm told although it works is poor coding practice) everything compiles without any problems. Like I said I copied the code out of my book, and yes I checked multiple times its copied character for character. Googleinn did not yield any solutions. So I come here to ask, does anyone know what is wrong with this code?
1.) You haven't actually included GradeBook.cpp anywhere
2.) "\"exceeds maximum lenght(25). \n You've forgotten a quotation mark and thus all your functions are now in quotes and not actual code.
3.) Nigga, that ain't no grade! Consider dropping the racism?
Okay fixed the quote problem, I think. Still no dice, same errors as before. Heres the code with changes I made to it.
I know I haven't done any #include "GradeBook.cpp" anywhere but the book said so long as I do include the GradeBook.h in the main.cpp and GradeBook.cpp I didn't need to.
If you go to your main.cpp and just add: #include "GradeBook.cpp" It'll fix your undefined reference errors, however there are some other errors in your GradeBook.cpp that you have to fix as well.
Zortisk - if they include the header file then there's no reason to include the .cpp
me 1291 - try including <string> since you are working with strings in your code (don't forget to do it with all three files). I haven't plugged your code into an IDE to test it, but that's the only problem I see with the new code that you posted.
EDIT;
Looks like you left out some colons. going to be a problem...
OK, I would argue that point about including .cpp, but whatever, you're right about there being a lot of other issues with the code, after you get through the simple problems like a ; in the wrong place (line 34) or missing in another (line 89). And several other small problems, the bigger issue is that switch doesn't deal with strings, but the info that you want to pass into the variable is a string (you also need quotes around EOF). I think there's a function that can return the numerical representation of a chr() in int form, I know that there is, I just don't remember what class contains it.
As far as working with debugger in codeblocks, they make it really nice by placing a red block next to the line of code that is causing issues, click on the next debugger message and the red block moves to the line that that next message is talking about.
newbieg, his switch isn't working with strings he's working with: int grade; when he gives it a letter he's just giving it a number, so his switch is valid.
Yep had to include GradeBook.cpp, and do a bit of debugging. But other than that it went perfectly! Thanks so much for the help guys! I'm a bit miffed that the book said I only had to include the .h file but eh so long as I know what to do now I'm fine with it. Thanks again!
I'm a bit miffed that the book said I only had to include the .h file but eh so long as I know what to do now I'm fine with it. Thanks again!
I would be a bit miffed that the book was right and I took bad advice.
You should only need to #include the .h file, but you need to include the implementation (.cpp) file in your project, otherwise your IDE will think it's something entirely separate.
#including a cpp file is generally a very bad idea (given the common use of a cpp file to contain actual function definitions and actual objects). As soon as you have a project larger than trivial, the linker will rightfully refuse to link anything as the same symbols will be repeated throughout the object files.
You do not need to #include the cpp file - you need to ensure the linker knows to link the compiled object file together with everything else. This is an ideal time for you to stop and learn what a compiler is, and what a linker is, and how they work together.
The problem with strings that I mentioned is that he wrote the code so that the user has to type in EOF which is passed to the variable int grade (this would supposedly then end the loop and the program), now that sounds like a string to me. I now think what he wanted could be done with CTRL+Z but I haven't tried that out, just saw it in a tutorial a while ago.
But thanks, I didn't know that single quotes returned the numerical representation of keys, that'll be good to know.
me1291 - it's been a while since I've used codeblocks, but I'm pretty sure if you go to file->new-> class it will run a wizard that will link the new files together so that it will run properly with just including the header file.