[Linked Lists] Custom String Class Add Char problems

This is a discussion on [Linked Lists] Custom String Class Add Char problems within the C++ Programming forums, part of the General Programming Boards category; Hi guys, It's been a while since I did anything with C++, but I am attempting to get back into ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Unhappy [Linked Lists] Custom String Class Add Char problems

    Hi guys,

    It's been a while since I did anything with C++, but I am attempting to get back into it for the writing of my own little operating system. As I can't use any external headers or libraries, I have decided to create my own set of libraries based loosely on the .NET framework.

    I have started by creating a simple string class using linked lists. But it's not working . It's been a while since I used pointers so that's probably where the problem is.

    I'll post the code that I think is causing the problem below, and I'll also upload the VC++ 6 project and source. If anyone could take a look and tell me where I've gone wrong, I would really appreciate it.

    The string code is broken into two classes - String and StringNode. String contains a member called "FirstNode", which points to the first StringNode in the list (the first character).

    The problem seems to be that FirstNode is not being assigned by my Add(char) function.

    Main() - DebugMain.cpp
    PHP Code:
    #include "System.String.hpp"
    #include <iostream>

    using namespace std;
    using namespace System;

    int main()
    {
        
    String = new String("Hello");
        *
    "Yellow";
        
    s->Traverse();
        
    cout << s->ToChar() << endl;
        
    delete s;
        return 
    0;

    System.String.Hpp - String Header File
    PHP Code:
    #ifndef SYSTEM_STRING_H
    #define SYSTEM_STRING_H 1

    namespace System
    {
        class 
    String
        
    {
            public:
                
    String();
                
    String(const char *);
                const 
    char operator[](const int Index);
                
    String &operator+=(const char &);
                
    String &operator=(const char chararray);
                
    bool Add(const char C);
                
    bool Add(String);
                
    bool Clear();
                const 
    char ToChar();
                ~
    String();
                
    void Traverse();
            private:
                class 
    StringNode
                
    {
                    public:
                        
    StringNode();
                        
    StringNode(const char wC);
                        
    char C;
                        
    StringNode Next;
                };

                
    StringNode FirstNode;
        };
    }

    #endif 
    Add(char) Function
    PHP Code:
    //-----------------------------------------------------------------------------
    /// <summary>
    ///    Adds a character to the String.
    /// </summary>
    /// <param name="C">The character to add.</param>
    /// <returns>True if the character was added.</returns>
    //-----------------------------------------------------------------------------
    bool System::String::Add(const char C)
    {
        
    cout << "Add(" << << ") called." << endl;
        
    StringNode node FirstNode;
        
    cout << "&*node: " << (&*node) << endl;
        while (
    node != 0)
        {
            
    node node->Next;
        }

        
    node = new StringNode(C);
        
    cout << "&*node: " << (&*node) << endl;
        
    cout << "&*FirstNode: " << (&*FirstNode) << endl;
        return 
    true;

    String Constructor
    PHP Code:
    System::String::String(const char Text)
    {
        
    this->FirstNode 0;
        
    int X 0;
        while (
    Text[X] != && Text[X] != '\0')
        {
            
    this->Add(Text[X]);
            
    X++;
        }

    When run, FirstNode has the address of 0 even after Add() has been called. At the start of Add(), doesn't my logic mean a new StringNode will be created and assigned where FirstNode is?

    Thank you to anyone who can help!

    Paul Stovell
    Attached Files Attached Files
    Last edited by nickname_changed; 07-23-2004 at 07:27 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    the problem is, you traversed the list to far: remember - you're adding to the list - you don't want 'node' to be NULL!

    Code:
    while (node->Next != 0) 
        { 
            node = node->Next; 
        } 
    
        node->Next = new StringNode(C);
    also be sure that StringNode's constructor initializes it's 'Next' ponter to NULL.

    >> cout << "&*node: " << (&*node) << endl;

    that's the very same as:

    cout << "node: " << node << endl;

    if you're trying to print the address of the pointer on the other hand, it would be:

    cout << "&node: " << &node << endl;
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 07:21 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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