2012-02-17 5 views
1

これは私のプログラムで発生しているエラーです。独自の文字列クラスを作成しようとしています

myString1.cpp: In constructor ‘MyString1::MyString1(char*, int)’: 
myString1.cpp:6: error: expected primary-expression before ‘]’ token 
myString1.cpp:6: error: expected primary-expression before ‘]’ token 
myString1.cpp: In member function ‘MyString1 MyString1::append(MyString1)’: 
myString1.cpp:11: error: invalid use of member (did you forget the ‘&’ ?) 
myString1.cpp: In member function ‘void MyString1::clear()’: 
myString1.cpp:25: error: expected primary-expression before ‘]’ token 
myString1.cpp:25: error: expected primary-expression before ‘{’ token 
myString1.cpp:25: error: expected `;' before ‘{’ token 
myString1.cpp: In member function ‘bool MyString1::empty()’: 
myString1.cpp:29: error: expected primary-expression before ‘]’ token 
myString1.cpp:31: error: expected primary-expression before ‘else’ 
myString1.cpp:31: error: expected `;' before ‘else’ 

ここで私のプログラムは3つの異なる部分に分かれています。

myString1.h

#ifndef MYSTRING1_H 
#define MYSTRING1_H 

class MyString1 
{ 
    private: 
    char chars[]; 
    int size; 
    public: 
    MyString1(); 
    MyString1(char chars[], int size); 
    MyString1 append(MyString1 s); 
    char at(int index); 
    int length(); 
    void clear(); 
    bool empty(); 
    int find(char ch); 
}; 
#endif 

初心者がちょうどそう見ているだけG ++コンパイラを使用する方法を学習しようとして

#include <iostream> 
#include "myString1.h" 
using namespace std; 

int main() 
{ 
    MyString1 first("cat", 4); 
    MyString1 second("dog", 4); 
    cout << first.at(1) << " and " << second.at(1) << endl; 
    first.append(second); 
    cout << first.at(6) << endl; 

    return 0; 
} 

イムmyString1.cpp

#include "myString1.h" 
using namespace std; 

MyString1::MyString1(char chars[], int size) 
{ 
    this->chars[] = chars[]; 
    this->size = size; 
} 
MyString1 MyString1::append(MyString1 s) 
{ 
    for(int i = size; i > size - s.length; i++) 
    chars[i] = s.at(i); 
} 
char MyString1::at(int index) 
{ 
    return chars[index]; 
} 
int MyString1::length() 
{ 
    return size; 
} 
void MyString1::clear() 
{ 
    size = 0; 
    chars[] = {}; 
} 
bool MyString1::empty() 
{ 
    if(chars[]){ 
    return true; 
    else 
     return false; 
    } 
} 
int MyString1::find(char ch) 
{ 
    for(int i = 0; i < size; i++) 
    if(chars[i] = ch) 
     return i; 
} 

testMyString1.cppエラーメッセージを読んで、私のコードをデバッグするための助けが必要です。また、いくつかの非常に悪いコードがあるので、どんな助けもありがとうございます。

+2

ヒント:だから、あなたが何かを宣言する必要があることを行うために

MyString1::~MyString1() { free(this->chars); } 

デストラクタは最後に、ちょうどそのようにすることができます空の関数であると呼ばれます - これには何が間違っていますか? –

+0

this-> chars [] = chars [] <---とこれです。良い答えは – RetroCode

答えて

4

コードに間違いが多いので、どこから始めたらいいのかわかりませんが、コードを理解するのに役立つポインタがあれば大丈夫でしょう。

私は、文字列クラスにサイズインデックスを持たせる必要はありません。strlen()関数が喜んで計算されるからです。 クラス宣言のために、文字列を保持するポインタの宣言方法を確認してください。次のようにする必要があります。

class MyString1 
{ 
    private: 
    char* chars;//this declares a pointer to a char that will hold the string for you 
    public: 
    ... 

また、文字列を保持するchar *を割り当てることは決してありません。あなたのコンストラクタは次のようになります。

MyString1::MyString1(const char* chars) 
{ 
    this->chars = (char*) malloc(strlen(chars)+1); //this will allocate an array of strlen() chars +1 
    strcpy(this->chars,chars); 
} 

あなたはstrlen関数は非常に効率的にあなたのためにそれを見つけることができますので、私はサイズインデックスを使用していない見ることができるように。 +1は、文字列の終わりを示す '\ 0'を表します。

これで文字列に何かを追加すると、それは扱いにくくなります。

void MyString1::append(const MyString1& s) //it's good to give a constant reference to the string here 
{ 
    //first of all we gotta reallocate the pointer,since we don't have enough memory for the string 
    int newsize = strlen(this->chars) + strlen(s)+1; 
    this->chars = (char*) realloc(this->chars,newSize); \\ no check for realloc failure, I know but this is just an example 

    strcat(this->chars,s.chars); 

} 

追加するときに何も返す必要はありません。あなたはこの文字列に何かしています。

あなたの:: at()関数は大丈夫です。文字列のサイズが10で、MyString1 :: at(12)を要求するとどうなるか想像してみてください。これはおそらくセグメンテーションフォールト(それは良くない)を引き起こすでしょう。

だから、以下のような境界チェックを行うには、あなたのコードを変更する必要があります

char MyString1::at(int index) 
{ 
    //if it's out of bounds let's return -1 which will signify that we got an out of bounds value (could also throw an exception here but that's a different subject altogether) 
    if(index > strlen(this->chars) || index <0) 
     return -1; 

    return chars[index]; 
} 

はまた、C/C++であなたが割り当てメモリを解放しなければなりません。 - `<;` char型の文字[]:

bool MyString1::empty() 
{ 
    return (this->chars[0] == '\0'; 
} 
+2

+1です。しかし、 'empty()'の中で 'strlen()'を呼び出すのではなく、すべての再割り当てのために現在のサイズを追跡する方が良いです。 – jweyrich

+0

は常にシステムと要件によって異なります。私は自分のプロジェクトのために私自身のString実装を持っています。私は別の変数として文字列のサイズを保持せずに行いますし、私の要件ではパフォーマンスは本当に良いです。しかし、それはまったく別の話題です。何らかの理由でそれについて議論する必要があります。 – Lefteris

+0

malloc()は、動作していることを確認する必要があります(NULLを返す可能性があります)。 –

関連する問題