classes won't execute

This is a discussion on classes won't execute within the C++ Programming forums, part of the General Programming Boards category; hey, I just did the tutorial on classes. when I compile it says 0 errors, but the button to execute ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    classes won't execute

    hey,
    I just did the tutorial on classes.
    when I compile it says 0 errors, but the button to execute the program is disabled.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    class computer
    {
     public:
     computer();
     ~computer();
     void setSpeed( int p);
     int readSpeed();
     protected:
     int processorspeed;
    };
      computer::~computer()
      {
      }
    void computer::setSpeed ( int p )
    {
      processorspeed = p;
    }
    int computer::readSpeed()
    {
    return processorspeed;
    }
    int main(int argc, char *argv[])
    {
    computer compute;
      return 0;
    }
    When I take computer computer; out of main it lets me execute. Any ideas?

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You forgot to define your constructor, aka add this:
    Code:
    computer::computer(){
    }
    The problem is that since you specifically prototyped the constructor in your class definition:
    Code:
    class computer
    {
     public:
     computer();
     ~computer();
    You need to explicitley define it in order for your class to be able to create an instance of itself. Now C++ does have default constructors however all items prototyped in the class definition need to be explicitley defined by you.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    where do I put it?

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    The same place that you put all your other function definitions:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    class computer
    {
     public:
     computer();
     ~computer();
     void setSpeed( int p);
     int readSpeed();
     protected:
     int processorspeed;
    };
    
    computer::computer()
    {
    }
      
    computer::~computer()
    {
    }
    void computer::setSpeed ( int p )
    {
      processorspeed = p;
    }
    int computer::readSpeed()
    {
    return processorspeed;
    }
    int main(int argc, char *argv[])
    {
    computer compute;
      return 0;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If you don't supply a constructor for your class, C++ will supply a 'default constructor', which will allow you to create objects of that class. However, if you list a constructor for your class, then C++ won't supply a 'default constructor'. In your class, you have this line:

    computer();

    which is a constructor, so C++ doesn't supply you with a default constructor. You can tell it's a constructor because it has no return type and it has the same name as the class. Regular functions have to have a return type. Since you never defined your constructor, i.e. you never told the compiler how you want your objects to be created, your program doesn't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do classes execute code?
    By bargomer in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2008, 04:10 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21