Uni用にこのプログラムを実行する必要があります。この「変数はこのスコープで宣言されていません」エラーが発生し続けます。変数がこのスコープで宣言されていませんでした。C++
は、文字列パラメータとして整数配列を受け取り、整数配列内の対応する値に従って文字列パラメータ内の各文字が乗算された文字列を返す関数を書く:これは割り当てです。あなたは、(文字列の終わりのヌル/ 0文字を除いて)両方の配列が同じ数の要素を持っていると仮定することができます。さらに、関数は参照パラメータで結果から余分な文字数を返します。 Exempleパラメータ1 "ABCD" {1,2,3,4} 結果:これは私がこれまでに書くことができたコードである6
を "abbcccdddd":
#include <iostream>
#include <string>
using namespace std;
string Function (string c, int numbers[n])
{
string result;
for (int i=0; i< n;i++)
{
int index= numbers[i];
for (int j=0; j< index;j++)
{
result.append(c.at(i));
}
}
return result;
}
int main()
{
int n;
string r;
cout << "Nr in array";
cin >> n;
int numbers [n];
for (int i=0; i<n;i++)
{
cout << "Nr";
cin >> numbers[i];
}
string c;
cout << " Write some letters";
cin >> c;
r=Function(c,numbers[n]);
cout << r;
return 0;
}
そして、これらは、私は私のコードをコンパイルしようとしたときにポップアップエラーです:
4:40: error: 'n' was not declared in this scope
In function 'std::string Function(...)':
7:20: error: 'n' was not declared in this scope
9:14: error: 'numbers' was not declared in this scope
12:18: error: 'c' was not declared in this scope
In function 'int main()':
33:25: error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
は、私は、これは些細な溶液であってもよい知っている、と私はこれを投稿する前にstackoverflowの上で同様の問題を探しましたが、何も本当に近い来ませんでした〜へ私の問題だと思います。つまり、私はmain関数の後に関数を定義したり、ループ内に変数を定義してから、それらを外部に呼び出すことはできません。ありがとうございます!:)
'string関数(文字列c、int numbers [n])' 'それは何ですか? –
これは、 'std :: vector'を使うことで混乱がなくなります。 – tadman