classes in c++
hi i get four errors when compiling this and it should all work cause the site im using even says it does so please can someone help me with it thanks.
void Aclass::aFunction()
{
cout<<"Whatever code";
}
#include
class Computer //Standard way of defining the class
{
public:
//This means that all of the functions below this(and any variables)
//are accessible to the rest of the program.
//NOTE: That is a colon, NOT a semicolon...
Computer();
//Constructor
~Computer();
//Destructor
void setspeed(int p);
int readspeed();
//These functions will be defined outside of the class
protected:
//This means that all the variables under this, until a new type of
//restriction is placed, will only be accessible to other functions in the
//class. NOTE: That is a colon, NOT a semicolon...
int processorspeed;
};
//Do Not forget the trailing semi-colon
Computer::Computer()
{ //Constructors can accept arguments, but this one does not
processorspeed = 0;
//Initializes it to zero
}
Computer::~Computer()
{ //Destructors do not accept arguments
}
//The destructor does not need to do anything.
void Computer::setspeed(int p)
{ //To define a function outside put the name of the function
//after the return type and then two colons, and then the name
//of the function.
processorspeed = p;
}
int Computer::readspeed()
{ //The two colons simply tell the compiler that the function is part
//of the clas
return processorspeed;
}
int main()
{
Computer compute;
//To create an ''instance'' of the class, simply treat it like you would
//a structure. (An instance is simply when you create an actual object
//from the class, as opposed to having the definition of the class)
compute.setspeed(100);
//To call functions in the class, you put the name of the instance,
//a period, and then the function name.
cout<<compute.readspeed();
//See above note.
return 0;
}
/* here are the errors i get
C:\My Documents\c++\old\classes.cpp(1) : error C2653: ''Aclass'' : is not a class or namespace name
C:\My Documents\c++\old\classes.cpp(3) : error C2065: ''cout'' : undeclared identifier
C:\My Documents\c++\old\classes.cpp(3) : error C2297: ''<<'' : illegal, right operand has type ''char [14]''
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : error C2371: ''cout'' : redefinition; different basic types
C:\My Documents\c++\old\classes.cpp(60) : warning C4552: ''<<'' : operator has no effect; expected operator with side-effect
Error executing cl.exe.
//thanks to anyone that can help me get it to work
i am the best
C:\My Documents\c++\old\classes.cpp(1) : error C2653: 'Aclass' : is not a class or namespace name
That's right. You make no reference to the class definition. Do you even need that code? Maybe try to comment out the void Aclass::aFunction() definition. It's not being used anyway and looks to be added as a part of a tutorial.
C:\My Documents\c++\old\classes.cpp(3) : error C2065: 'cout' : undeclared identifier
Add #include "iostream.h" to the top of the file. That should take care of this one. (Replace quotes with GT/LT sign)
C:\My Documents\c++\old\classes.cpp(3) : error C2297: '<<' : illegal, right operand has type 'char [14]'
See #include above...
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : error C2371: 'cout' : redefinition; different basic types
See #include above...
C:\My Documents\c++\old\classes.cpp(60) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
See #include above...
Regards,
Jumpster
Edited by - Jumpster on November 18, 2000 4:32:00 PM
Edited by - Jumpster on November 18, 2000 4:33:22 PM
That's right. You make no reference to the class definition. Do you even need that code? Maybe try to comment out the void Aclass::aFunction() definition. It's not being used anyway and looks to be added as a part of a tutorial.
C:\My Documents\c++\old\classes.cpp(3) : error C2065: 'cout' : undeclared identifier
Add #include "iostream.h" to the top of the file. That should take care of this one. (Replace quotes with GT/LT sign)
C:\My Documents\c++\old\classes.cpp(3) : error C2297: '<<' : illegal, right operand has type 'char [14]'
See #include above...
c:\program files\microsoft visual studio\vc98\include\ostream.h(139) : error C2371: 'cout' : redefinition; different basic types
See #include above...
C:\My Documents\c++\old\classes.cpp(60) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
See #include above...
Regards,
Jumpster
Edited by - Jumpster on November 18, 2000 4:32:00 PM
Edited by - Jumpster on November 18, 2000 4:33:22 PM
Regards,JumpsterSemper Fi
Oh. Disregard the #include in the message above. I didn''t realize you were already doing that. I don''t know what would be causing those last three errors. My bad. Sorry.
Regards,
Jumpster
Regards,
Jumpster
Regards,JumpsterSemper Fi
First, do your include like this:
#include /*iostream*/
using namespace std;
Noter replace /*...*/ with the greater than and less than symbols.
do not put iostream.h, just iostream.
Not sure if you have a typo here but this will generate errors:
cout< //See above note.
try, cout << compute.readspeed();
Last wrap your header file with this
#ifndef __COMPUTER__
#define __COMPUTER__
...
#endif
ECKILLER
#include /*iostream*/
using namespace std;
Noter replace /*...*/ with the greater than and less than symbols.
do not put iostream.h, just iostream.
Not sure if you have a typo here but this will generate errors:
cout< //See above note.
try, cout << compute.readspeed();
Last wrap your header file with this
#ifndef __COMPUTER__
#define __COMPUTER__
...
#endif
ECKILLER
ECKILLER
There is a cout in Aclass::aFunctio(), which is above the include statement, meaning cout isn''t defined yet. Geting rid of the function will of course remove the iostream errors too.
Why does everyone always __FILE_NAME__ for header guards? All names starting with _[A-Z] or with __ anywhere in the name are reserved words (for the standard and the implementation) so that stuff can be added without breaking anyone's code. That's why the new C99 types start with _[A-Z], and all the generated names in MSVC start with __ (other compilers will use the same kind of thing).
You should use a different system for you header guards, something like FILE_NAME_INCLUDED.
Edited by - Wilka on November 18, 2000 5:58:30 PM
You should use a different system for you header guards, something like FILE_NAME_INCLUDED.
Edited by - Wilka on November 18, 2000 5:58:30 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement