2016-05-20 12 views
-2

私は文字列変数を追加、削除、編集することができるシンプルなデータベースプログラムを作ろうとしています。C++ if(Array [x] .length> 0)

私は次の問題に遭遇しました:追加された各変数を次の空の配列要素に割り当てます。

- 私は動的配列についてもっと学ぶまで静的配列を使っています。

- 文脈から外れているので、ここでコードを再作成して、意味が分かりました。申し訳ありませんが、私は何かを見逃しました。

//static array declaration 
std::string names[5]; 

//string selection from main function 
std::string stringSel = "Item"; 

//boolean condition for loop 
bool completed = false; 

//if (names[x].length > 0) then increment x by 1 until names[x].length == 0 and then set that array element to the value from stringSel 
int x = 0 
while(completed == false) 
{ 
    //if the length of the element is greater than zero characters, increment the element 
    if (names[x].length > 0) 
    { 
     x++; 
    } 
    //if the length of the element is not greater than zero characters, set the string to that element 
    else 
    { 
     //The empty element is assigned the string and the entire array prints out 
     names[x] = stringSel; 
     std::cout << stringSel << " was added to the database.\nThis is the printout of all current items: " << names; 
     completed = true; 
    } 
} 

エラーはif文の条件から来ています。 '。'赤

1 IntelliSenseの下線が引かれている:バインドされた関数へのポインタのみ

答えて

4
names[x].length 

std::string::length()関数を呼び出すために使用することができるが、あなたが必要とするので、それをアクセスするには、メンバ関数ではなく、データメンバであります

names[x].length() 
+0

ありがとう、それは簡単に解決して意味があります。 stdクラス、オブジェクト文字列、長さメンバーですか?私は最近、クラスとオブジェクトの仕組みを学びました。 –

+1

@Bijan Kelleyようこそ。 'std'は' string'クラスが属する名前空間で、 'length()'は 'string'クラスのメンバ関数です。この場合の 'string'クラスのオブジェクトは、' std :: string names [5]; 'のような配列を作成するので' names [x] 'が保持するものです。 –

+0

あまりにもそれをクリアしていただきありがとうございます!先週もネームスペースについて学んだので、ちょうど1つの石で2匹の鳥を殺したようです。 –