invalid use of 'class ...'

This is a discussion on invalid use of 'class ...' within the C++ Programming forums, part of the General Programming Boards category; Hi, I am writing my very first C++ program using classes. I am trying to doit in small steps so ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    invalid use of 'class ...'

    Hi, I am writing my very first C++ program using classes. I am trying to doit in small steps so I can catch the error early and quick...
    I have defined the class, and I tried to place some calls for methods from that class in main. But I get this error: invalid use of 'class Stack'
    I've checked what I could, and I really do not know why I get this.
    So, my class is defined as such:
    Code:
    /* some includes, namespace std etc. */
    class Stack {
          private:
                  int *ptr;
                  int top;
                  int array[MaxStack];
                  int tmp;
          public:
                 void Stack::init() { top = -1; }
                 Stack::Stack() { ptr = new int[MaxStack];
                              top = 10;
                              ptr = array;
                              for (int i = 0; i < 10; i++)
                                 array[i] = 0;
                            }
    /* some more methods here */
    };
    int main()
    {
       Stack s;
    
       s.init();
       s.Stack(); /* the line error indicates */
    
       return 0;
    }
    What am I doing wrong?
    Last edited by kocika73; 03-09-2006 at 04:25 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> s.Stack(); /* the line error indicates */

    What are you trying to do there?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Sorry, my error. This is just a default constructor. The 3 in brackets comes from my other constructor. Still, same error.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Why are you calling the default constructor there? What are you trying to do? The default constructor is automatically called when you declare the variable s.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    So what is the purpose of writing your own constructors? How can you use them??? If I have a default constructor and my own, I was told I need to call the constructor. So I did, and it gives me errors... Now I am confused.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The constructor that you wrote is called automatically when you declare the variable s. So it is already being used. You never actually "call" a constructor, one is just called for you when you create an instance of your class. In this case, you created an instance with the variable s. Then you called init(). At that point you can use your stack.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    So, if I create another instance of the class Stack, lets say s1, and do not call init() on it, I can use my other constructor?

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    In general here is an idea

    Code:
    class MyClass
    {
    public:
    int m_health;
    int m_damage;
    
    MyClass( Health = 100, Damage = 50 );  // constructor
    };
    
    // use constructor outside main
    
    MyClass::MyClass ( Health, Damage )
    {
    m_health = Health;          // assign members values
    m_damage = Damage;
    }
    
    main()
    {
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> So, if I create another instance of the class Stack, lets say s1, and do not call init() on it, I can use my other constructor?

    What "other" constructor. In the code you posted, there is only one constructor for the class Stack. It will be called each and every time you create an instance of the Stack type.

    You can add additional constructors. For example, you might add a new constructor that takes an initial size. Once you have more than one constructor available, you can pick which one you want to have called depending on how you declare the new variable.

    As far as the init() function, it isn't necessary in this case, you should remove it from your attempt to understand the constructors. The constructor you have written initializes the class on its own, so there is no real need for an init function.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    OK, so f I have my own constructor
    Code:
    Stack::Stack(int n) { top  = n; 
                                   ptr = new int[MaxStack]; 
                                   ptr = array;
                                   for(int i = 0; i < top; i++)
                                   array[i] = 0;
                                 }
    Is it OK to use it?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes. To use that constructor instead of the one you have in your first post, you would use
    Code:
    Stack s1(3);
    (or whatever you want instead of 3).

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Although you wouldn't want to use the code you have in that particular constructor because it contains an error (memory leakage).
    "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

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks, Daved and hk_mp5kpdw. I think I can handle exploring some more (for now) by myself. You were of great help, I appreciate it.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    See if this helps:
    Code:
    #include <iostream>
    using namespace std;
    
    class Simple
    {
    private:
    	int start;
    
    public:
    	Simple() //default constructor
    	{
    		start = 1;
    	}
    	
            Simple(int num)
    	{
    		start = num;
    	}
    
    	void show()
    	{
    		cout<<start<<endl;
    	}
    };
    
    int main()
    {
    
    	Simple s1;  //calls default constructor
    	s1.show();
    
    	Simple s2(30);  //calls the other constructor
    	s2.show();
    
    	return 0;
    }
    The way the program knows which constructor to call is determined by the line that creates the object:

    Simple s2(30);

    That line wants to create the object by sending one int argument to a constructor, so the compiler looks for a constructor that has one int parameter in its definition, e.g.:
    Code:
    Simple(int num)
    {
        //code
    }
    In this line:

    Simple s1;

    there is no argument listed--not even parentheses like this:

    Simple s1(); //error

    When you create an object like that, the compiler looks for a default constructor. A default constructor is a constructor that has no parameters in its definition, e.g.:
    Code:
    Simple(/*no parameters in here*/)
    {
    
    }
    If you do not supply a constructor for your class, the compiler will supply an invisible default constructor for you. The default constructor provided by the compiler only creates the objects--it does not initialize the member variables to anything.
    Last edited by 7stud; 03-09-2006 at 06:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 05:30 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Replies: 7
    Last Post: 05-26-2005, 11:48 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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