Thread: Objects getting me down...:(

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    Objects getting me down...:(

    Well. I'm almost ashamed to ask this question. But I'm stumped. I'm trying to remember OOP (haven't programmed in awhile), and I'm having some trouble.

    Code:
    #include <iostream.h>
    
    class CSubtraction
    {
      int s1, s2;
    public:
      void Equalize(int,int);
      int Subtract(void) {return (x-y);}
    };
    
    void CSubtraction::Equalize(int x, int y)
    {
      x = s1;
      y = s2;
    }
    
    int main()
    {
      CSubtraction sub;
      sub.Equalize(4,3);
      cout << "Answer: " << sub.Subtract();
      return 0;
    }
    I know it's some tiny little thing I missed, like a semi-colon or something. Just incase you haven't caught on, it's just supposed to subtract 4 and 3.

    The errors:
    Line 8: 'x' undeclared
    Line 8: 'y' undeclared

    Thanks guys.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    int Subtract(void) {return (x-y);}
    ... x, y are not declared. I think you meant ...
    Code:
    int Subtract(void) { return s1 - s2; }
    Also, use this instead of those old headers:
    Code:
    #include <iostream>
    using namespace std;

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Thanks. I knew it was something stupid.

    Originally posted by Speedy5
    Also, use this instead of those old headers:
    Code:
    #include <iostream>
    using namespace std;
    [/B]
    I've seen this before. Why should I use it? And some people tell me that it pollutes the namespace. Is that true?
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    You'll also need to change this:

    Code:
    void CSubtraction::Equalize(int x, int y)
    {
      x = s1;
      y = s2;
    }
    To this:

    Code:
    void CSubtraction::Equalize(int x, int y)
    {
      s1 = x;
      s2 = y;
    }

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Originally posted by KneeLess
    I've seen this before. Why should I use it? And some people tell me that it pollutes the namespace. Is that true?
    Code:
    #include <iostream.h>
    is essentially the same as

    Code:
    #include <iostream>
    using namespace std;

    The second form wraps the header into the 'std' namespace. To avoid "polluting" the namespace, you can import separate pieces with the 'using' directive:

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    Or, you can just tag 'std::' onto the front of the identifiers:

    Code:
    #include <iostream>
    ....
      std::cout << "hello world" << std::endl;
    Also, the header files that end in '.h' are deprecated. Standard conforming compilers are not required to provide them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM