943,986 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2233
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 18th, 2007
0

help with c++ classes

Expand Post »
pls i will like you to help me with something
i was practising with c++ classes and i created a class interface called GradeBook, you know that i will need a source file to write the details of the implementation of the public functions? so in the source file i included the headeer file "GradeBook.h" then the next line i declared the class' constructor like this GradeBook::GradeBook(param goes here...){....} but the compiler returned an error message that a constructor cannot return a value, what pissed me off was that.... that's how it is written in the textbook i use; it must have been seeing the first GradeBook before the colons as a return type.. i am getting fed up..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Mar 18th, 2007
0

Re: help with c++ classes

sometimes books contain errors. please post the code you are trying to compile so we can help you.

>>i am getting fed up
we all feel that way at times. But don't give up. When you feel really frustrated, walk away for awhile and do something else, like watch a good movie on TV.
Last edited by Ancient Dragon; Mar 18th, 2007 at 4:40 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Mar 18th, 2007
0

Re: help with c++ classes

//class GradeBook's source file
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. #include "GradeBook.h"// to tell the compiler i am using resources from class GradeBook
  6. GradeBook::GradeBook(string name){
  7. setCourseName(name);
  8. }
  9. void GradeBook::setCourseName(string name){
  10. if(name.length() == 0){
  11. cout<<"\nName was not Entered";
  12. }
  13. if((name.length() > 0)&&(name.length() <=25)){
  14. courseName = name;
  15. }else
  16. if(name.length()>25){
  17. courseName=name.substr(0,25);
  18. cout<<"\nName \""<<name<<"\" is more than 25 characters and has been truncated to 25"<<endl;
  19. }
  20. }
  21. string GradeBook::getCourseName(){
  22. return courseName;
  23. }
  24. void GradeBook::displayMessage(){
  25.  
  26. cout<<"\nWelcome to the grade book of : "<<getCourseName()<<endl;
  27. }
Last edited by Ancient Dragon; Mar 18th, 2007 at 4:51 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Mar 18th, 2007
0

Re: help with c++ classes

1. you did not include the header file <string>

2. If you are not declaring using namespace std: after the includes then you need to declare the namespace at each object instance. For example, line 6 would read
GradeBook::GradeBook(std::string name){

and line 21
std::string GradeBook::getCourseName(){

alternatively, you can add #using std::string after the header file inclusion.
Last edited by Ancient Dragon; Mar 18th, 2007 at 4:59 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Mar 18th, 2007
0

Re: help with c++ classes

thnx for the reply,
I have included that in the class' interface like this :

// implementation of class grade book's interface
  1. #include <string>
  2. using std::string;
  3. //GradeBook's class definition
  4. class GradeBook
  5. {
  6. public:
  7. GradeBook(string);
  8. void setCourseName(string);
  9. string getCourseName();
  10. void displayMessage();
  11. private:
  12. string courseName;
  13. }

I have tried what you said but i'm still in the rat race
Last edited by Ancient Dragon; Mar 19th, 2007 at 12:05 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Mar 19th, 2007
0

Re: help with c++ classes

The problem is that the class must end with a semicolon. Put a semicolon after the closing brace on line 13 of the header file
Last edited by Ancient Dragon; Mar 19th, 2007 at 12:06 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Mar 19th, 2007
0

Re: help with c++ classes

thnx but this is what i get:

--------------------Configuration: GradeBook - Win32 Debug--------------------
Compiling...
GradeBook.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/GradeBook.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
GradeBook.exe - 2 error(s), 0 warning(s)

pls can you do me a favor?
create the class interface yourself with the idea i have given and see if it works you can omit all the validations in member function setCourseName(), i hope i am not a pain in the ****....
if i am, i am sorry
i just can't proceed until i get this,
Last edited by addicted; Mar 19th, 2007 at 12:55 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Mar 19th, 2007
0

Re: help with c++ classes

>LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
You haven't defined your main() function. The linker doesn't know where the entry point of the application is.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 19th, 2007
0

Re: help with c++ classes

thanx alot,
please could you help me with my program, i have wrote about it in the early pages, go through it...... The error came when i put a semicolon at the end of my class interface, when i try to compile the class' source code, i get that message but when i remove the semicolon it brings back the previous error
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
addicted is offline Offline
57 posts
since Mar 2007
Mar 19th, 2007
0

Re: help with c++ classes

This version compiles without errors. In this version I combined the .h and *.cpp into one *.cpp file for simplicity, but you should keep them separate if you wish. I also added the main() function at the bottom of the code. Hope this helps.

  1. #include <iostream>
  2. #include <string>
  3. using std::cout;
  4. using std::endl;
  5. using std::string;
  6. //GradeBook's class definition
  7. class GradeBook
  8. {
  9. public:
  10. GradeBook(string);
  11. void setCourseName(string);
  12. string getCourseName();
  13. void displayMessage();
  14. private:
  15. string courseName;
  16. };
  17.  
  18. //#include "GradeBook.h"// to tell the compiler i am using resources from class GradeBook
  19. GradeBook::GradeBook(string name){
  20. setCourseName(name);
  21. }
  22. void GradeBook::setCourseName(string name){
  23. if(name.length() == 0){
  24. cout<<"\nName was not Entered";
  25. }
  26. if((name.length() > 0)&&(name.length() <=25)){
  27. courseName = name;
  28. }else
  29. if(name.length()>25){
  30. courseName=name.substr(0,25);
  31. cout<<"\nName \""<<name<<"\" is more than 25 characters and has been truncated to 25"<<endl;
  32. }
  33. }
  34. string GradeBook::getCourseName(){
  35. return courseName;
  36. }
  37. void GradeBook::displayMessage(){
  38.  
  39. cout<<"\nWelcome to the grade book of : "<<getCourseName()<<endl;
  40. }
  41.  
  42. int main()
  43. {
  44.  
  45. return 0;
  46. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with c++ vectors
Next Thread in C++ Forum Timeline: error C2664





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC