help with c++ classes
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..:?:
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
//class GradeBook's source file
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h"// to tell the compiler i am using resources from class GradeBook
GradeBook::GradeBook(string name){
setCourseName(name);
}
void GradeBook::setCourseName(string name){
if(name.length() == 0){
cout<<"\nName was not Entered";
}
if((name.length() > 0)&&(name.length() <=25)){
courseName = name;
}else
if(name.length()>25){
courseName=name.substr(0,25);
cout<<"\nName \""<<name<<"\" is more than 25 characters and has been truncated to 25"<<endl;
}
}
string GradeBook::getCourseName(){
return courseName;
}
void GradeBook::displayMessage(){
cout<<"\nWelcome to the grade book of : "<<getCourseName()<<endl;
}
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
1. you did not include the header file
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thnx for the reply,
I have included that in the class' interface like this :
// implementation of class grade book's interface
#include <string>
using std::string;
//GradeBook's class definition
class GradeBook
{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
private:
string courseName;
}
I have tried what you said but i'm still in the rat race
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
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
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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,
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
>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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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:confused:
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
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.
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
//GradeBook's class definition
class GradeBook
{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
private:
string courseName;
};
//#include "GradeBook.h"// to tell the compiler i am using resources from class GradeBook
GradeBook::GradeBook(string name){
setCourseName(name);
}
void GradeBook::setCourseName(string name){
if(name.length() == 0){
cout<<"\nName was not Entered";
}
if((name.length() > 0)&&(name.length() <=25)){
courseName = name;
}else
if(name.length()>25){
courseName=name.substr(0,25);
cout<<"\nName \""<<name<<"\" is more than 25 characters and has been truncated to 25"<<endl;
}
}
string GradeBook::getCourseName(){
return courseName;
}
void GradeBook::displayMessage(){
cout<<"\nWelcome to the grade book of : "<<getCourseName()<<endl;
}
int main()
{
return 0;
}
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thnx alot
i have tried the code, there were no errors in the code, it compiled but i didnt check for logical errors.
but there is a but,
when i separated the code thus:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
//GradeBook's class definition
class GradeBook
{
public:
GradeBook(string);
void setCourseName(string);
string getCourseName();
void displayMessage();
private:
string courseName;
};
and the implemetation was like this:
#include "GradeBook.h"
GradeBook::GradeBook(string name){
setCourseName(name);
}
void GradeBook::setCourseName(string name){
if(name.length() == 0){
cout<<"\nName was not Entered";
}
if((name.length() > 0)&&(name.length() <=25)){
courseName = name; }else
if(name.length()>25){
courseName=name.substr(0,25);
cout<<"\nName \""<<name<<"\" is more than 25 characters and has been truncated to 25"<<endl;
}
}
string GradeBook::getCourseName(){
return courseName;
}
void GradeBook::displayMessage(){
cout<<"\nWelcome to the grade book of : "<<getCourseName()<<endl;
}
this is the error i got:
c:\documents and settings\user\my documents\dejsoft projects\gradebook.cpp(3) : error C2143: syntax error : missing ';' before 'PCH creation point'
Error executing cl.exe.
and the main function thus:
// main program to use the class GradeBook
#include <iostream>
#include <string>
using std::endl;
#include "GradeBook.h"
int main(){
return 0;
}
this is the outcome of the compilation:
c:\documents and settings\user\my documents\dejsoft projects\gradebookclient.cpp(9) : error C2628: 'GradeBook' followed by 'int' is illegal (did you forget a ';'?)
about the class GradeBook, will i compile the interface of the class or the implementation of the class;
the GradeBook.h
or the GradeBook.cpp
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
using VC++ 2005 Express I created a new console project, added the three files you posted -- GradeBook.h, GradeBook.cpp and main.cpp. Everything compiled with no errors or warnings.
The code you posted is ok. So y0ur problem must be the way you created the project and files.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
or could it be the compiler? cos i am having problems with structures too.
some one said i should get the vc++2005 express and i am already downloading it but it will take some time
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
that compiler are you using? If its VC++ 6.0 then yes you need to upgrade.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I just tried it with VC++ 6.0 and it compiled ok too.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
just got the visual C++ 2005 express edition, i am learning to use it now, i didnt see any build or compile icon on it, i was thinking may be i have to register to use it
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
i just got the visual C++ 2005 enterprise edition, i couldnt use it, does anyone kows where i can get the tutorial?
i wrote a fresh hello world program and i saved it, still, the build icon was not highlighted. i dont know how to compile my programs, or is there anyone who can help with that?:?:
addicted
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
try this . It is for VC++ 2003.NET but I don't think there is much difference. You should be able to get an idea of what to do. Click on the images to enlarge them.
And you don't have to register VC to be able to compile applications.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115