0

プロジェクトの場合、明示的な値のコンストラクターで初期化できるように、リンクされたリスト オブジェクトを設定しようとしています。私はそれを次のようにしたい:

WORD you("you");//where the object you's linked list now contains y o u;

しかし、オブジェクトを印刷すると、この記号「=」だけが表示され、あなたの長さを印刷すると、-858993459が表示されます

これが私の明示的な値コンストラクターです。誰かが私が間違っていることを教えてもらえますか?

WORD::WORD(string s)
{
front = 0;
int i = 0;
int len = s.length();

if(front == 0)
{   
    front = new alpha_numeric;
    alpha_numeric *p = front;

    while(s[i] <= len)
    {
        p -> symbol = s[i];
        p -> next = new alpha_numeric;
        p = p -> next;
        p -> symbol = s[i++];
    }
    p -> next = 0;
}
}

役立つ場合は、クラス宣言ファイルを次に示します

#include <iostream>
#include <string>

using namespace std;
#pragma once

class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};

class WORD
{
public:
WORD(); //front of list initially set to Null
WORD(const WORD& other);
WORD(string s); //***EXPLICIT VALUE CONSTRUCTOR
bool IsEmpty(); //done
int Length();
void Add(char); //done
//void Insert(WORD bword, int position);
//void operator=(char *s);

friend ostream & operator<<(ostream & out, const WORD& w);//done

private:
alpha_numeric *front; //points to the front node of a list
int length;
};
4

4 に答える 4

0

これを試して:

WORD::WORD(string s)
{
  int i;
  int len = s.length();

  front = new alpha_numeric;
  alpha_numeric *p = front;

  for(i = 0; i < len; i++)
  {
        p -> symbol = s[i];
        p -> next = (i == len - 1) ? 0 : new alpha_numeric;
        p = p -> next;
  }
}
于 2012-06-06T19:41:13.487 に答える