Simple class doesn't work

This is a discussion on Simple class doesn't work within the C++ Programming forums, part of the General Programming Boards category; Code: ------------------------------------------------------------------------------- //integer.h #ifndef INTEGER_H #define INTEGER_H #include <iostream> class Integer { private: int value; public: Integer(); //constructor ~Integer(); //destructor ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    66

    Simple class doesn't work

    Code:
    -------------------------------------------------------------------------------
    //integer.h
    
    #ifndef INTEGER_H
    
    #define INTEGER_H
    #include <iostream>
    class Integer
    {
        private:
            int value;
        
        public:
            Integer();               //constructor
            ~Integer();              //destructor
            void setValue(int number);              //sets value
            int returnValue();           //returns value
            
    }
    
    #endif
              
    --------------------------------------------------------------------------------
    //integer.cpp
    #include "integer.h"
    #include <iostream>
    
    Integer::Integer()
    {    
        value = 0;  
        std::cout << "Constructor used." << std::endl;
    }
    
    Integer::~Integer()
    {
        std::cout << "Destructor sued." << std::endl;
    }
    
    void Integer::setValue(int number)
    {
        value = number;
    }
    
    int Integer::returnValue()
    {
        return value;
    }
    
    ----------------------------------------------------------------------------------
    //main.cpp
    #include "integer.h"
    #include <iostream>
    
    int main()
    {
        Integer integer1;
        Integer interger2;
        
        integer1.setValue(1);
        integer2.setValue(2);
        
        std::cout << "The value of integer1 is "
                  << integer1.returnValue
                  << "."
                  << std::endl;
        
        std::cout << "The value of integer2 is "
                  << integer2.returnValue
                  << "."
                  << std::endl;
                  
        system("PAUSE");
        return 0;
    }
    
    -------------------------------------------------------------------------------
    Line              File                                      Message
    5              integer.cpp        ISO C++ forbids defining types within return type
    5              integer.cpp        return type for constructor invalid
                    Makefile.win       [build error] [integer.o] Error 1
    What's the problem?
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Code:
    };
    gg

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by Codeplug
    Code:
    };
    gg
    Do you mean that I need the ';' for my constructors and destructors? I tried that, but nothing changed.
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    std::cout << "The value of integer1 is "
    << integer1.returnValue ()
    << "."
    << std::endl;
    How do you call a member function?
    Success is a two word story, "WORK" works.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by pratip
    How do you call a member function?
    wups ! that's true. But the same errors is still there....
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Code:
    class PleaseCloseMeWithADamnSemiColon
    {
    ...
    };
    gg

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You've forgotten a rule about classes:
    Code:
    class someclass
    {
    //...
    }; //<-- semicolon
    Edit: Beaten...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Here is the code I used and it works fine with gcc compiler on Linux after some editing. I can't remember where I edited. Its upto you to figure it out :-)

    In main.cpp check the declaration of interger2

    Code:
    //integer.h
    #ifndef INTEGER_H
    
    #define INTEGER_H
    #include <iostream>
    
    class Integer
    {
     private:
      int value;
        
     public:
      Integer();               //constructor
      ~Integer();              //destructor
      void setValue(int number);              //sets value
      int returnValue();           //returns value
            
    };
    
    #endif
    ------------------------------------------------------------

    Code:
    //integer.cpp
    #include "integer.h"
    #include <iostream>
    
    Integer::Integer()
    {    
      value = 0;  
      std::cout << "Constructor used." << std::endl;
    }
    
    Integer::~Integer()
    {
      std::cout << "Destructor sued." << std::endl;
    }
    
    void Integer::setValue(int number)
    {
      value = number;
    }
    
    int Integer::returnValue()
    {
      return value;
    }
    --------------------------------------
    Code:
    //main.cpp
    #include "integer.h"
    #include <iostream>
    
    int main()
    {
      Integer integer1;
      Integer integer2;
        
      integer1.setValue(1);
      integer2.setValue(2);
        
      std::cout << "The value of integer1 is "
    	    << integer1.returnValue()
    	    << "."
    	    << std::endl;
        
      std::cout << "The value of integer2 is "
    	    << integer2.returnValue()
    	    << "."
    	    << std::endl;
                  
      system("PAUSE");
      return 0;
    }
    Last edited by pratip; 01-08-2004 at 10:24 PM.
    Success is a two word story, "WORK" works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple Short Class Question
    By Travis Dane in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2002, 06:12 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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