Classes and constructors help please

This is a discussion on Classes and constructors help please within the C++ Programming forums, part of the General Programming Boards category; I am working on a college course and I am having difficulty with class constructors. I thought I understood them, ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Classes and constructors help please

    I am working on a college course and I am having difficulty with class constructors. I thought I understood them, but I can't get my code to compile cleanly. Here's the code for a problem similar to what I've been trying to do:

    Code:
    // Temperature.h
    
    #ifndef TEMPERATURE
    #define TEMPERATURE
    
    #include <iostream>
    using namespace std;
    
    class Temperature
    {
          public:
                 Temperature();
                 void print(ostream& out) const;
          
          private:
                  double myDegrees;
                  char myScale;
    }
    
    inline Temperature::Temperature()
    {
           myDegrees = 0.0;
           myScale = 'C';
    }
    
    inline void Temperaure::print(ostream& out)
    {
           out << myDegrees << ' ' << myScale;
    }
    
    #endif
           
    // testTemp.cpp
    
    #include <cstdlib>
    #include <iostream>
    #include "Temperature.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        Temperature temp1;
        temp1.print(cout);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    This example is the same as the one in my textbook, but I get a compiler error in Temperature.h:

    19 C:\Dev-Cpp\Temperature.h return type specification for constructor invalid

    I didn't specify any return type, so why is it that I'm getting this error?

    Thanks in advance,
    Sean

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Code:
    class Temperature
    {
          public:
                 Temperature();
                 void print(ostream& out) const;
          
          private:
                  double myDegrees;
                  char myScale;
    };
    You're missing the ending semicolon.
    Code:
    inline Temperature::Temperature()
    {
           myDegrees = 0.0;
           myScale = 'C';
    }
    
    inline void Temperaure::print(ostream& out)
    {
           out << myDegrees << ' ' << myScale;
    }
    These should be in a separate source file and linked with the rest of the program. (I'm not sure about inlining a constructor, probably where your error comes from).

    Code:
    system("PAUSE");
    Avoid calling system if possible. A call to cin.get() would do just as good here.
    Last edited by hk_mp5kpdw; 08-27-2007 at 08:38 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by S_Diggy View Post
    I didn't specify any return type, so why is it that I'm getting this error?
    It's a missing semicolon after the end of the class in the header file.
    Kurt

    Edit: Way too slow again.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    Great, thanks folks. I did have them in separate files, Temperature.h and testTemp.cpp, I just cut and pasted them into the same section in the message. The system call is simply what Dev-CPP puts in the "default" main function. thanks for the extra advice on that.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Quote Originally Posted by S_Diggy View Post
    Great, thanks folks. I did have them in separate files, Temperature.h and testTemp.cpp, I just cut and pasted them into the same section in the message. The system call is simply what Dev-CPP puts in the "default" main function. thanks for the extra advice on that.
    I got that... I'm saying that the main function should be in testTemp.cpp, the class definition should be in the header temperature.h and the code for the two functions (the constructor and print) should be in a separate source file perhaps temperature.cpp for example. The two source files get compiled individually into object files and the linker combines the code from those two object files into a single/complete executable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I seem to remember that it is impossible to use the inline keyword in a separate source file (unless it is included from the header).

    Anyway, in this case there is no real reason to worry about inlining: console output will still be expensive and slow and the constructor could be "optimized" by using an initializer list instead:

    Code:
    Temperature::Temperature(): myDegrees(0.0), myScale('C')
    {
    }
    Because of the missing semicolon the compiler apparently thought that class Temperature is the return type of the constructor of Temperature because the declaration wasn't closed properly.

    Learn to recognise this error, as it is quite common and the error is usually reported in a different file (including the header with this class). Or just make it a habit to type the closing brace and semicolon before filling in the rest so as not to forget about it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I think you are correct anon. If the functions are defined outside the class definition but still in the header file, they should be declared inline or you will get multiple definition errors. If you move the function definitions to the source file, then they should not be declared inline, although some compilers may be able to handle it in certain situations.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by anon View Post
    Anyway, in this case there is no real reason to worry about inlining: console output will still be expensive and slow and the constructor could be "optimized" by using an initializer list instead:
    The initializer list MUST be used to initialize members. Doing it inside the constructor body itself is just wrong.

    Although you won't find out why this is until you start using data types which are not assignable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and overloading constructors
    By barneygumble742 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2005, 05:32 AM
  2. Destructors and Constructors on classes
    By Da-Nuka in forum C++ Programming
    Replies: 14
    Last Post: 06-14-2005, 02:08 PM
  3. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  4. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM
  5. Replies: 2
    Last Post: 01-15-2002, 05:00 PM

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