help with c++ classes

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

help with c++ classes

 
0
  #1
Mar 18th, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: help with c++ classes

 
0
  #2
Mar 18th, 2007
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

Re: help with c++ classes

 
0
  #3
Mar 18th, 2007
//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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: help with c++ classes

 
0
  #4
Mar 18th, 2007
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

Re: help with c++ classes

 
0
  #5
Mar 18th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: help with c++ classes

 
0
  #6
Mar 19th, 2007
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

Re: help with c++ classes

 
0
  #7
Mar 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with c++ classes

 
0
  #8
Mar 19th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: addicted is an unknown quantity at this point 
Solved Threads: 0
addicted's Avatar
addicted addicted is offline Offline
Junior Poster in Training

Re: help with c++ classes

 
0
  #9
Mar 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: help with c++ classes

 
0
  #10
Mar 19th, 2007
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. }
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2062 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC