Class constructors and pointers ????

This is a discussion on Class constructors and pointers ???? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am trying to make a class call stack which will hold a series of string. I am ...

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Class constructors and pointers ????

    Hi all, I am trying to make a class call stack which will hold a series of string. I am attempting to make the constructor but I am having trouble with getting it to work as I am using a c string.
    Code:
    class stack {
    	private:
    		int depth;
    		int current;
    		char *stack_layer[100];
    
    	public:
    		stack(char item) {
    			stack_layer=item;
    		};
    
    };
    What am I doing wrong? I get the fallowing compiler error.
    error C2440: '=' : cannot convert from 'char *__w64 []' to 'char *[100]'

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    stack_layer is a pointer to a char array, item is a single char, but you are trying to assign one to the other which cant be done. im not sure what you want to do so i dont know what to recommend. if its c++, is there a restriction on using string objects rather than c-style strings?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    None of this makes sense. You have an array of char *, which is an array of C-style strings. You have a "current" which I assume is the stack pointer. You have a "depth" which I don't know what it is.

    And your constructor takes a single char? First of all, shouldn't it take a string? Second, why do you force the user to push an element when they create a stack?

  4. #4
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Yes, i am quite lost with it as well... Its for a uni homework assignment... excluding the the attempt at a constructor the rest was given to me...

    The purpose of the task is to finish off the class. it is meant to hold a series of strings. With methods such as "push" to push a string into the stack, "pop" to get the string from the stack and "print" which is to print the contents of the stack...

    why they want us to use a c_string is beyond me. as a c_string is c item and a class is c++.

    and they haven't told me much more... writing the methods should be a peice of cake, its just this constructor's that are killing me...

    Edit:
    oh and depth - is the Depth of the stack
    current - is the Current pointer this will start at the top of the stack and decrees to the bottom of the stack. the maximim range is the depth of the stack
    stack - the actual stack layers to contain the stings of characters as they are pushed into the stack...

    by the way before anyone jumps in and accuses me to get someone to do my homework for me.. thats not what i am asking, i just need some help with the constructors
    Last edited by Loic; 09-12-2007 at 10:25 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    My guess is that your prof wants you to have a stack of up to 100 null terminated strings (stack_layer). depth is probably the current number of strings in the stack, and current is the index of string at the top of the stack.

    In this context, a constructor that takes a char argument does not make sense. More likely you would want to copy the interface of std::stack and write your own:
    1. Default constructor that initialises depth and each element in stack_layer to 0, and current to say, 0 or -1.
    2. Copy constructor that performs deep copying.
    3. Copy assignment operator, perhaps implemented by invoking the copy constructor with a member swap function.
    4. A destructor that loops through the array of strings to deallocate memory for each string in use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i was thinking he was making a constructor that took in an initial string then the constructor creates the stack and pushes the string given into the stack at stack_layer[0].
    if that is what you are trying to do, you should probably overload the constructor and have a no-args constructor that just initializes the stack.

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by laserlight View Post
    In this context, a constructor that takes a char argument does not make sense. More likely you would want to copy the interface of std::stack and write your own:
    std::stack? never herd of it, where can i find this?

    Quote Originally Posted by nadroj View Post
    no-args constructor.
    no-args?
    Last edited by Loic; 09-12-2007 at 10:53 PM.

  8. #8
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Would it be possible that the class isn't meant to hold the contents of the string but just the address of the string? so something like this will work?

    Code:
    stack(char ptr) {stack_layer = &ptr;};

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Loic View Post
    std::stack? never herd of it, where can i find this?
    http://www.cplusplus.com/reference/stl/stack/

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    no-args?
    Also known as a default constructor, at least in C++ parlance.

    Would it be possible that the class isn't meant to hold the contents of the string but just the address of the string? so something like this will work?
    Possibly, but you would have to clarify with your prof. I find it rather unlikely.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Using Pointers in Contained Classes
    By arb215 in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:31 AM
  4. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 04:52 AM
  5. Replies: 1
    Last Post: 11-10-2002, 07:37 PM

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