I'm having trouble writing a class.

This is a discussion on I'm having trouble writing a class. within the C++ Programming forums, part of the General Programming Boards category; Okay, I have been learning about using classes, now I want to write my own. I read how to do ...

  1. #1
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35

    I'm having trouble writing a class.

    Okay, I have been learning about using classes, now I want to write my own.

    I read how to do it in various textbooks and this is what I came up with:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    class Myclass
    {
    public:
      Myclass()
      {
        value = 0;
      }
    
      Myclass(int x)
      {
        vlaue = x;
      }
      
      void print()
      {
        cout<<value;
      }
      
      void set(int x)
      {
        value = x;
      }
    private:
      int value;
    };
    
    void main()
    {
      int number;
      Myclass test;
      test.print();
      cout<<"Enter a new value: ";
      cin>>number;
      test.set(number);
      test.print();
      cin.get();
    }
    Now I know the class definition is usually in a seperate file which is included, but it's meant to work like this too isn't it?

    When I try to complile this code in Bloodshed Dev-C++ I get an error at the line
    Code:
        value = 0;
    telling me
    9 h:\myclass.cpp `value' undeclared (first use this function)
    What's wrong?
    Am I totally ont he wrong track or is it just a typo or small syntax error?
    - Well that's my 4c Australian.

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Code:
    Myclass(int x)
      {
        vlaue = x;
      }
    You have value spelled wrong.

    -DIS

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Agreed.

    Such as it is, it runs fine after that correction.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    THANK YOU!

    Oh, I tried this about 5 times!
    In different places, with different code.

    And EVERY time, it didn't work.
    I really gotta be careful of those typso...
    - Well that's my 4c Australian.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Don't think you'd be the frist...fsrit...first.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Try to become familiar with the error messages that the compiler spits out. Some are very cryptic but that would probably be an undeclared identifer error which will point you to the line or even the word, depending on your compiler.

    After a while you wont even need to read the whole error, just going to the line will make it obvious

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 02:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. writing a C++ class to file
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2002, 10:06 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 03:02 PM

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