• 締切済み

C++の課題について教えてください。

配列のコピーに関する問題なのですが、なぜエラーになるのか教えてください。 #include <iostream> using namespace std; void arrayCopy(int target,int original,int size){ for(int i=0;i<size;i++){ target[i]=original[i]; } } int main(){ const int size=5; int original[size]={1,2,3,4,5}; int target[size]; arrayCopy(target,original,size); print(target,size); }

みんなの回答

回答No.2

※ どこでどんなエラーが出たのかくらい報告しろ。 - 関数 arrayCopyの引数型は int,int,int であるのに対し  呼び出し側は int*, int*, int すなわち型の不整合が生じている。  正しいのはどちらか?

  • koujikuu
  • ベストアンサー率43% (428/992)
回答No.1

「関数に配列へのポインタを渡す」を参照して下さい http://brain.cc.kogakuin.ac.jp/~kanamaru/lecture/prog1/11-03.html

関連するQ&A

  • C++

    C++で書いた下記のプログラムが、文字の入力が1023個までなら実行されますが、1024個を超えると実行されません。なぜだか分かる方、教えてください。 #include <iostream> #include <string> using namespace std; int main(void) { string Str; cin >> Str; cout << "here"; }

  • 2つのcppファイルから

    1つのexeファイルを作る必要性が出てきました。 (現在質問中の質問に関連しています。) 例えば file.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } void main(void) { for(int i=0;i<9;Output(i++)); } をコンパイルリンクすれば済むことを file1.cpp: #include<iostream> using namespace std; void Output(int x) { cout<<x<<endl; } file2.cpp: void main(void) { for(int i=0;i<9;Output(i++)); } という風に分かれているファイルをコンパイルリンクしなければならないのです。 cpp -e file.exe -c file1.cpp -c file2.cpp ではエラーになります。 どうすれば良いのでしょうか?

  • C++のreduce

    C++のreduceを使おうとすると error: 'reduce' was not declared in this scope となってしまいます reduceを使うにはどうしたらよいでしょうか? OSはMac GCCのバージョンは9.2.0_3となってます よろしくおねがいします コード #include <iostream> #include <vector> #include <numeric> using namespace std; int main() { const vector<int> v = {1, 2, 3}; int sum = reduce(v.begin(), v.end()); cout << sum << endl; }

  • プログラミング言語Cとプログラミング言語C++

    プログラミング言語Cとプログラミング言語C++の違いって何ですか? あと、プログラミング言語C++についていくつか質問があります。 #include <iostream> using namespace std; void main(){ cout << "Hello world!" << endl; } このプログラムについて質問なんですが、iostreamってプログラミング言語Cで言うstdio.hのことですか? using namespace stdって何ですか? あと、 cout <<"Hello world!"って何ですか?

  • C++の配列について

    #include <iostream> using namespace std; int main() { float w[] = {1.2,2.3,3.4,4.5,5.6}; float x[] = {4.8,2.6,1.3,9.1,8.7}; float u = 0.0; int i; for(i=0;i<5;i=i++) { u += w[i] * x[i]; } cout << "u=" << u << "です\n"; return 0; } u=105.83って出たんですが、これは何をしているプログラムなんですか

  • shared_ptr クラスについて

    shared_ptrクラスを使いたいのですが、使えません、どうしてでしょうか?ソースはこれです。 #include<iostream> #include <string> #include <fstream> #include<memory> using namespace std; class SMonster{ string name; int power; public: SMonster(); SMonster(int p); ~SMonster(){ }; void SetPower(int p); int GetPower(SMonster& t)const; void walk(const string& str); int GetPoint(void)const; }; class B {}; class D : public B {}; int main(void) { shared_ptr<D> sp0(new D); SMonster m(200); SMonster n(100); std::cout<<m.GetPower(m)<<std::endl; std::cout<<n.GetPower(n)<<std::endl; ShowWindow(10); }

  • c++での入出力がうまくいきません;

    初心者ですが独習c++でプログラミングについて勉強している者です。 なんとか環境設定もできたと思いきや、うまくコンパイルすることができなくて困っています。具体的には、 #include <iostream> using namespace std; int main() { int i, j; double d; i = 10; j = 20; d = 99.101; cout << "値を表示: "; cout << i << ' ' << j << ' ' << d; return 0; } という値を表示するプログラムではコンパイルもうまくいったのですが、次の #include <iostream> using namespace std; int main() { int i; cout << "値を入力: "; cout >> i; cout << "入力した値: " << i << "\n"; return 0; } という入力のプログラムをコンパイルしようとすると、コマンドプロンプトに演算子が使われたクラス ostream では int型の定義が存在しないとなりコンパイルエラーとなってしまいます。 理由が考えてもわからないので、質問してみました。 どのようにすれば、このエラーを解消できるのか知りたいので、どうかご意見をよろしくお願いします。

  • int *a = new int; エラー

    下記をコンパイルすると、'int' 型は 'int *' 型に変換できない(関数 main() ) とエラーが出てしまいました。 何がおかしいのでしょうか ? 初歩的な間違いをしていると思いますが、アドバイスお願いします。 #include<iostream> #include<stdlib> #include<ctime> using namespace std; void main(void) { int *a = new int; a=rand(); cout<<a<<endl; delete a; }

  • アドバイスください。

    10個の1桁の整数を入力して、入力した順番に並べたいのですが 最後の出力のところで番地が表示されてしまいます。 どうしてでしょうか? #include<iostream> using namespace std; int main(void){ int a; int b[10]; for(int i=0; i<9; i++){ cin >> a; b[i]=a; } cout << b; return 0; }

  • C++プログラム問題(初心者です)

    C++の勉強初めて1ヶ月ぐらいです。 以下にある問題がわかりません。 解説付きでお願いします。 [問題] 関数 funcのみを修正することで、標準出力に 2 が表示する。 [プログラム] #include <iostream> using namespace std; int func(int i) {return i;} int main() { int i = 1; cout << ++func(i) << "\n"; return 0; }

専門家に質問してみよう