C++ クラスの作り方とエラーメッセージ対処法

このQ&Aのポイント
  • Windows10の上で、Visual Studio Community2015 を使ってC++を勉強中の者です。Time クラスを定義して使おうとしましたが、エラーメッセージが出てきて、どこが悪いのかわかりません。
  • 詳しい方がいらっしゃいましたら、ご教授お願い致します。問題のエラーメッセージの内容は次のとおりです。
  • "consoleApplication69.cpp" 作成中にエディターの中で、波型の赤線が出ているところにカーソルを持っていくと、エラーメッセージが出てきます。具体的には、#include "time3.h" のところで「ソースファイルをひらけません。"time3.h"」というエラーメッセージが表示されます。
回答を見る
  • ベストアンサー

C++ クラスの作り方

Windows10の上で、Visual Studio Community2015 を使ってC++を勉強中の者です。 Time クラスというのを定義して使おうとしましたが、クライアント側のプログラム作成中にエラーメッセージが出てきて、色々考えましたがどこが悪いのかわかりません。 プログラム自体は時刻を設定してそれを表示させるだけのものです。 詳しい方がいらっしゃいましたら、ご教授お願い致します。 問題のエラーメッセージ; "consoleApplication69.cpp" を作成中にエディターの中で、波型の赤線が出ているところにカーソルを持っていくと、次の3か所でそれぞれ次のようなエラーメッセージが出てきます。 (1) #include "time3.h" のところ: 「ソースファイルをひらけません。"time3.h"」 (2) 関数 incrementMinute( )のプロトタイプのところ:「不完全な型は使用できません。」 (3) 最初のパラメーター Time & のところ:「識別子 Time が定義されていません。」 以下に、 1: 私が作ったTimeクラスの定義のあるヘッダー "time3.h", 2: メンバー関数の定義ファイル "time3.cpp" 3:クライアント側プログラム "consoleApplication69.cpp" の3つのソースコードをコピーします。 "time3.cpp" については長いので、上の半分だけにしました。 ****************************************************** 1: "time3.h" ******************************************************** #pragma once // header file for Time class // time3.h #ifndef TIME3_H #define TIME3_H class Time { public: Time(int = 0, int = 0, int = 0); ~Time(); void setTime(int, int, int); void setHour(int); void setMinute(int); void setSecond(int); int getHour(); int getMinute(); int getSecond(); void printUniversal(); void printStandard(); private: int hour; int minute; int second; }; #endif *********************************************** 2: "time3.cpp" ************************************************ #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> #include "time3.h" using namespace System; using namespace std; Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); } void Time::setTime(int hr, int min, int sec) { setHour(hr); setMinute(min); setSecond(sec); } void Time::setHour(int hr) { hour = (0 <= hr && hr <= 23) ? hr : 0; } void Time::setMinute(int min) { minute = (0 <= min && min <= 59) ? min : 0; } void Time::setSecond(int sec) { second = (0 <= sec && sec <= 59) ? sec : 0; } ********************************************************* 3: "consoleApplication69.cpp" ********************************************************* // ConsoleApplication69.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <iostream> #include <iomanip> #include "time3.h" using namespace System; using namespace std; //prototype void incrementMinute(Time &, const int); int main() { Time t; t.setHour(2); t.setMinute(10); t.setSecond(30); cout << " current time is "; cout << setfill('0'); cout << setw(2) << t.getHour() << ":"; cout << setw(2) << t.getMinute() << ":"; cout << setw(2) << t.getSecond() << "\n"; return 0; } // function void incrementMinute(Time &tt, const int num) { // まだ未定義 } またソリューションエクスプローラーの画面写真も添付します。 ヘッダーファイルもインクルードしていますし、ソリューションの中に入れたつもりですが、なぜ Time が定義されていないといわれるのでしょうか? どうぞよろしくお願いいたします。

質問者が選んだベストアンサー

  • ベストアンサー
  • skp026
  • ベストアンサー率45% (1011/2238)
回答No.5

追加のご質問に返信いたします。 >>やり方が間違っているでしょうか? すみません。具体的な操作まではわからないです。 とりあえずご案内したかったことは、 ソリューションとプロジェクトの関係と、 VSの内部動作についてです。 ひとつのアプリを作成するとき、 exeとdllというように分けて作成することがあります。 このexeやdllはそれぞれがプロジェクトです。 それをひとまとめにするのがソリューションです。 そういうわけでソリューションに表示されても、 プロジェクトに入っていない可能性を、 ご案内したかったのです。 私は過去のバージョンを触ったことありますが、 テキトーにファイル追加しても、 コンパイルは通らなかったので、 ソリューションとプロジェクトを新規に作り直し、 って対応した記憶があります。 解決に直結してなくてごめんなさい。

papashiroSooke
質問者

お礼

有難うございます。 プロジェクトとソリューションの関係さえも知りませんでした。 Visual Studio そのものの教本を探して、勉強したいと思います。

papashiroSooke
質問者

補足

前回教えて頂いた英語の質問サイト http://go.microsoft.com/fwlink/?LinkId=158194 にポスティングしたら、デストラクターの定義がないのにクラスの中にメンバーとして入っているのが原因だとわかりました。そこでデストラクターをコメントアウトしてからビルドしたら、問題なくコンパイルできました。英語で質問を書くのはきついのですが、いいサイトを教えて頂いて感謝します。 今回の質問に多くの方々から回答を頂き、どの方の回答も本当にありがたく思いますが、問題の解決に直接つながったということで、SKP026さんをベストアンサーにさせていただきます。ほかの方々、どうかあしからずご了承ください。

その他の回答 (5)

  • wormhole
  • ベストアンサー率28% (1619/5654)
回答No.6

>プロジェクトはC++の中のCLRコンソールアプリケーションというものです。C++/CLIとは何かわかりません。 C++/CLIは.NET Frameworkで実行するプログラムを作成するためにC++を拡張した言語です(C++/CLIは文法上C++の上位互換ですけど別言語と思った方がいいです)。 Visual Studio 2015だとC++でCLRコンソールアプリケーションを作る際にはC++/CLIになると思います。 >上のエラーメッセージにあるLNK2028やLNK2019、LNK1120:2などはどこを検索すれば見つかるのでしょうか。 Visual Studio 2015のIDEのメニューからヘルプ→ヘルプの表示でヘルプがブラウザで開きますから、そこから Visual Studio 2015→Visual C++→IDEと開発ツール→C/C++プログラムのビルド→C/C++ビルドのリファレンス→C/C++ビルドエラー で各種エラーの書かれたページに行けます。

papashiroSooke
質問者

お礼

色々有難うございます。 勉強しなくてはならないことが山のようにあるようです。 最初はちょっとした趣味のように考えて始めたプログラミングですが、甘い考えではついていけないことが分かりました。でもせっかく始めたので、少なくともC++の基本ををマスターするまで頑張るつもりです。応援よろしくお願いします。

回答No.4

> プロジェクトはC++の中のCLRコンソールアプリケーションというものです。C++/CLIとは何かわかりません。 ひとまず "Win32コンソールアプリケーション” で仕切り直し。

papashiroSooke
質問者

お礼

ご回答有難うございます。 Win32コンソールアプリケーションでやり直してみます。

papashiroSooke
質問者

補足

Win32コンソールアプリケーションで空のプロジェクトを作り、プロジェクトメニュー=>新しい項目の追加でヘッダーファイル、 関数定義ファイル、main() の入ったプログラムファイルを用意して、それぞれに前のプロジェクトからのものをコピーしてきましたが、#include "stdafx.h", using namespace System; のところで、赤い波線が出てきました。"stdafx.h"と"stdafx.cpp" は、プロジェクト=>既存の項目の追加でプロジェクトに加えてあります。なんだか一番最初の問題に戻ったような状況です。

  • skp026
  • ベストアンサー率45% (1011/2238)
回答No.3

たぶんですけど、 そのソリューションのプロジェクトへ、 ファイルを追加した手順に問題があり、 うまくビルドの設定に反映されてないのかも… 右クリックして、プロジェクトに追加くらいしか思い付かないです。 ビルドの設定とかみても分かる気がしますが、 具体的な操作はわからないです。 役に立たなくてごめんなさい。 以下の方が正解をくれやすいかも。 Visual C++ 全般のフォーラム http://go.microsoft.com/fwlink/?LinkId=158194 Visual C++ 言語のフォーラム http://go.microsoft.com/fwlink/?LinkId=158195

papashiroSooke
質問者

お礼

ご回答有難うございます。 自分のやった方法は、ファイルメニューから新規作成==>ファイルとしてヘッダーファイルtime3.hと、関数定義ファイルtime3.cpp を書いた後、それぞれのタブをクリックしてハイライトしてから、ファイルメニュー==>ファイルの移動先==>consoleApplication69 とやって、ソリューションエクスプローラーにそれぞれが追加されたので、このプロジェクトの中に含まれたと解釈したのですが、やり方が間違っているでしょうか?

  • wormhole
  • ベストアンサー率28% (1619/5654)
回答No.2

>しかし今度はリンクエラーが出てしまい、コンパイルできません。 リンクエラーではわかりません。 出力されているエラーメッセージをそっくりそのまま書きましょう。 気になるのはプロジェクトがC++ではなくC++/CLIのようですけど。

papashiroSooke
質問者

お礼

ご回答有難うございます。 エラーメッセージは下の通りです。 1>------ ビルド開始: プロジェクト:ConsoleApplication69, 構成:Debug Win32 ------ 1> time3.cpp 1> AssemblyInfo.obj : マネージ コードが存在する場合は、/DEBUG:FASTLINK はサポートされません。/DEBUG:FULL でリンクを再開します 1>ConsoleApplication69.obj : error LNK2028: 1>ConsoleApplication69.obj : error LNK2019: 1>C:\Users\Shiro\documents\visual studio 2015\Projects\ConsoleApplication69\Debug\ConsoleApplication69.exe : fatal error LNK1120: 2 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== プロジェクトはC++の中のCLRコンソールアプリケーションというものです。C++/CLIとは何かわかりません。 上のエラーメッセージにあるLNK2028やLNK2019、LNK1120:2などはどこを検索すれば見つかるのでしょうか。 アドバイス頂けるとありがたいです。

回答No.1

エラーの内容に関しては、プロジェクトがTime3.hを認識していないのが原因で、そこさえ解決すれば全部解決(して次の問題点が見つかるかも?)するように思います。 > ソリューションの中に入れたつもりですが、 そのように見えますね。 ソリューションエクスプローラのTime3.hをダブルクリックして、対象のファイルが開けますか? ソリューションエクスプローラから一旦削除、再度追加してみては? ソリューションエクスプローラから一旦削除、Time3.hを別の場所に移動、ソリューションエクスプローラで新規にTime3.hを作成、中身をコピペとかでは。

papashiroSooke
質問者

お礼

早速にご回答をいただき、有難うございます。 >ソリューションエクスプローラから一旦削除、Time3.hを別の場所に移動、ソリューションエクスプローラで新規にTime3.hを作成、中身をコピペとかでは。 この方法でソースコード内のエラーは消えました。 しかし今度はリンクエラーが出てしまい、コンパイルできません。 全く新しいプロジェクトにして、ソースファイルをコピーしてやってみましたが、結果は同じです。 何かアドバイスを頂けると有り難いです。

関連するQ&A

  • C++ の while ループ

    C++の基本学習者です。Windows 10 で Visual Studio Community 2015 を使っています。 教本に載っている、機械語のプログラムをC++でシミュレーションする、というものを作ろうと、途中まで下のようなコードを書き込み、そこまで間違いがないかを確かめるために、コンパイルして実行したら、添付の写真のようなエラーメッセージが出てきました。 // ConsoleApplication65.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> using namespace System; using namespace std; int main() { // variables int program = 0; int accumulator = 0; int count = 0; int instructionRegister = 0; int operationCode = 0; int operand = 0; int location[100]; cout << "*** Welcome to Simpletron ***" << endl; cout << "*** Please enter your program ***" << endl; cout << "*** one instruction at a time,***" << endl; cout << "*** after the location number and and the prompt of '?' ***" << endl; cout << "*** To stop entering instructions, ***" << endl; cout << "*** enter -9999 . ***" << endl << endl; cout << setfill('0') << internal; cout << setw(3) << count << " ? "; cin >> program; while (program != -9999) { location[count] = program; count++; } return 0; } while ループに問題がありそうですが、コンパイルはちゃんとできたのに、何が悪いのでしょうか? 詳しい方、どうぞ教えてください。お願いします。

  • 【C++】関数からクラスに変更するには?

    いつも大変お世話になっています。 VC++初心者です。 関数をクラス化していきたいのですが、具体的にどのようにしたら良いか ご指導頂けませんでしょうか。 例えば、このようなソースがあった場合、どのようにクラス化させるのでしょうか。 (また、下記の例ですと、pulsとsubで1つのグループ、 goodMoringとgoodNightで1つのグループにさせる場合には どうすれば宜しいでしょうか。) //======================= //Tool.hの中身 //======================= int plus(int x, int y); int sub(int x, int y); void goodMorning(); void goodNight(); //======================= //Tool.cppの中身 //======================= #include <iostream.h> int plus(int x, int y){ return x + y; } int sub(int x, int y){ return x - y; } void goodMorning(){ cout << "おはよう\n"; } void goodNight(){ cout << "こんばんは\n"; } //======================= //Main.cppの中身 //======================= #include <iostream.h> #include "Tool.h" int main(){ int a = puls(1,1); cout << a <<'\n'; int b = puls(2,1); cout << b <<'\n'; goodMorning(); goodNight(); }

  • C++ クラス定義の質問

    C++学習者です。Visual studio community 2015 を使っています。 銀行口座を表す SavingsAccount というクラスを作り、添付のプログラムをコンパイルしようとしましたが。以下のようなエラーメッセージが出てきました。 1>------ ビルド開始: プロジェクト:ConsoleApplication86, 構成:Debug Win32 ------ 1>savingsAccount.obj : error LNK2001: 外部シンボル ""private: static double SavingsAccount::annualInterestRate" (?annualInterestRate@SavingsAccount@@0NA)" は未解決です。 1>C:\Users\Shiro\documents\visual studio 2015\Projects\ConsoleApplication86\Debug\ConsoleApplication86.exe : fatal error LNK1120: 1 件の未解決の外部参照 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== ちゃんと値を代入しているのに、annualInterestRate が未解決とはどういうことなのでしょうか? 以下が、クラス定義ヘッダーファイル、クラス関数定義ファイル、クライアントプログラムです。どうかよろしくお願いいたします。 //**************************************** // クラス定義ヘッダーファイル //**************************************** #pragma once #ifndef SAVINGSACCOUNT_H #define SAVINGSACCOUNT_H class SavingsAccount { static double annualInterestRate; public: SavingsAccount(double); ~SavingsAccount(); static void modifyInterestRate(double); double calculateMonthlyInterest(); double getBalance() const; double getSavingsBalance() const; private: double balance; double savingsBalance; }; #endif //*************************************** // クラス関数定義ファイル //*************************************** // savingsAccount class member functions #include "stdafx.h" #include <iostream> using namespace std; #include "savingsAccount.h" double annualInterestRate = 0.03; SavingsAccount::SavingsAccount(double initialAmount) :balance(initialAmount) { savingsBalance = balance + calculateMonthlyInterest(); cout << "savings account with balance of " << balance << " and savings balance of " << savingsBalance << " has been constructed\n"; } SavingsAccount::~SavingsAccount() { cout << "savings account of the savings balance of " << savingsBalance << " has been destructed\n"; } void SavingsAccount::modifyInterestRate(double newRate) { annualInterestRate = newRate; } double SavingsAccount::getBalance() const { return balance; } double SavingsAccount::getSavingsBalance() const { return savingsBalance; } double SavingsAccount::calculateMonthlyInterest() { return balance * annualInterestRate / 12; } //******************************************** // クライアントプログラム //******************************************** // ConsoleApplication86.cpp : // class SavingsAccount driver program #include "stdafx.h" #include <iostream> using namespace std; #include "savingsAccount.h" int main() { SavingsAccount saver1(2000.00); SavingsAccount saver2(3000.00); cout << "original amount of saving for saver1 : " << saver1.getBalance() << " and savings with interest: " << saver1.getSavingsBalance() <<endl; cout << "----------------------------------\n"; cout << "original amount of saving for saver2 : " << saver2.getBalance() << " and savings with interest: " << saver2.getSavingsBalance() << endl; return 0; }

  • 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 ではエラーになります。 どうすれば良いのでしょうか?

  • 名前空間でビルドエラー(LNK2005)

    C++の名前空間について勉強しています。 ソースを組んでビルドしようとしたところ、以下のようなエラーが表示されました。 > error LNK2005 "int test::count" は既に main.obj で定義されています。 > fatal error LNK1169: 1つ以上の複数回定義されているシンボルが見つかりました。 変数が多重定義(?)されているというエラーであることはわかるのですが、 その対処法が分からず困っています・・・。 わかる方がいればアドバイスください。 よろしくお願いします。 --------------------------------------- ○main.cpp #include "ns.h" int main(){  test::hoge(); } ○ns.h #pragma once namespace test {  int count;  void hoge(); } ○ns.cpp #include <iostream> #include "ns.h" void test::hoge(){  std::cout << "hoge()が呼ばれた" << test::count++ << "回目" << std::endl; }

  • C++のファイルの分割について教えてください。

    分割したプログラムの書き方を練習中なのですが、 下のようなプログラムを書いて、ビルドしようとしたら失敗します。 どこが悪いのか、教えていただけないでしょうか? (5)\test30\Debug\test30.exe : fatal error LNK1120: 外部参照 1 が未解決です。 1>test30 - エラー 2、警告 0 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== //test30.cpp #include <iostream> #include "sub.h" int main(void) { std::cout<<"メインプログラムです。"<<std::endl; sub1(); sub2(); return 0; } ------------------------------------------------------------- //sub1.cpp #include <iostream> void sub1(void) { std::cout<<"サブ1です。"<<std::endl; } ------------------------------------------------------------- //sub2.cpp #include <iostream> void sub1(void) { std::cout<<"サブ2です。"<<std::endl; } --------------------------------------------------------------- //sub.h void sub1(void); void sub2(void);

  • C++言語の非常に初歩的な質問(数表)

    今、C++言語を勉強中です。 そこで数表を作るみたいな例題があるのですがどうしても思ったとおりになってくれません。 いろんなことを考えましたが自分の力ではどうにもなりません・・・・。 そこで間違いがあれば指摘していただけたらと思い質問させていただきます。 以下がそのプログラム?です。よろしくお願いします。 #include<iostream.h> #include<math.h> #include<iomanip.h> main() { int n ; double n3 , n5; cout << setw(5) << "n" << setw(10) << "1/n" << setw(10) << "n^1/3" << "\n"; cout << setiosflags(ios::fixed); for(n=1 ; n<=25 ; ++n) { n3=1/n ; n5=pow(n,1/3) ; cout << setw(5) << n << setw(10) << setprecision(5) << n3 << setw(10) << setprecision(5) << n5 << "\n" ; } return 0 ; }

  • C++での入出力演算子のオーバーロード

    C++学習者です。Visual Studio 2015 を使っています。 入力演算子>> と出力演算子<<をオーバーロードする関数をfriend としてクラス定義の中に書きましたが、”メンバーではありません” というメッセージが出てきてコンパイルできません。 エラー番号はC2039です。 何回調べても原因がわからないので、詳しい方にお聞きしたいです。 どうかよろしくお願いいたします。 ヘッダーファイルと関数定義ファイル、クライアントプログラムと、エラーメッセージのコピーを張り付けてあります。 // クラスヘッダーファイル // class Array header #ifndef ARRAY1_H #define ARRAY1_H #include <iostream> using namespace std; class Array { // operator overloading as non-member functions friend ostream &operator<<(ostream &, const Array &); friend istream &operator>>(istream &, Array &); public: Array(int = 10); // constructor with default of 10 elements of array Array(const Array &); //copy constructor ~Array(); // destructor int getSize() const; // size of array // operator overloadings as member functions const Array &operator=(const Array &); // assignment bool operator==(const Array &) const; // check equality of two arrays // because both sides of the operator are constant, // this function must be constant too bool operator!=(const Array &right) const { // fully defined function in the header file like this one // will be made in-line function and save overhead time return !(*this == right); } int &operator[](int); // check subscript range for non-constant array const int &operator[](int) const; // check subscript range for constant array // remember we can only invoke constant function of a constant object // so if we want to check the subscript range of a constant object, we need // to use a constant member function of that object, that's why we need a // constant version of the same functionn as above operator[] private: int size; // size of array int *ptr; // pointer to the first element of the array // so ptr is the name of the array }; // end of class Array definition #endif // 関数定義ファイル // array1.cpp // member function definitions of class Array #include "stdafx.h" #include <iostream> #include <iomanip> using namespace std; using std::cout; #include <new> // for new and delete #include <cstdlib> // for exit() using std::exit; #include "array1.h" // >> , << 以外のオーバーロード関数は省略します // input operator overloading istream &Array::operator>>(istream &input, Array &a) { for (int i = 0; i < a.size; i++) input >> a.ptr[i]; return input; } // output operator overloading ostream &Array::operator<<(ostream &output, const Array &a) { int i; for (i = 0; i < a.size; i++) { output << setw(12) << a.ptr[i]; if ((i + 1) % 4 == 0) output << endl; } if (i % 4 != 0) output << endl; return output; } // クライアントプログラム // ConsoleApplication87.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #include "stdafx.h" #include <iostream> using namespace std; #include "array1.h" int main() { Array integers1(7); // 7 elements array Array integers2; // default 10 element array cout << "size of array integer1 is " << integers1.getSize() << endl; cout << "contents of integers1 after instantiation are :\n"; cout << integers1; cout << "---------------------------------------------\n"; cout << "size of array integers2 is " << integers2.getSize() << endl; cout << "contents of integers2 after instantiation are :\n"; cout << integers2; cout << "---------------------------------------------\n"; return 0; } // エラーメッセージ 1>------ ビルド開始: プロジェクト:ConsoleApplication87, 構成:Debug Win32 ------ 1> array1.cpp 1>c:\users\shiro\documents\visual studio 2015\projects\consoleapplication87\consoleapplication87\array1.cpp(96): error C2039: '>>': 'Array' のメンバーではありません。 1> c:\users\shiro\documents\visual studio 2015\projects\consoleapplication87\consoleapplication87\array1.h(9): note: 'Array' の宣言を確認してください 1>c:\users\shiro\documents\visual studio 2015\projects\consoleapplication87\consoleapplication87\array1.cpp(104): error C2039: '<<': 'Array' のメンバーではありません。 1> c:\users\shiro\documents\visual studio 2015\projects\consoleapplication87\consoleapplication87\array1.h(9): note: 'Array' の宣言を確認してください ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========

  • C++で解りません。

    C++で #include <iostream.h> int main(void) { int x; int y; cout << "xを入力してください:"; cin >> x; cout << "yを入力してください:"; cin >> y; cout << "x+yは" << x + y << "です。\n"; return 0; } ----------- で、 エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) とでます。どこがおかしいのか解りません。

  • C言語の質問です!

    #include "stdafx.h" #include <iostream> #include <string> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <conio.h> using namespace std; double arrayMin(double array[][], int n){ double Min=9999999999999999999999.999999; for(int N=0;N<n;N++){ if(array[N][0]!=NULL&&array[N][0]<Min){ Min=array[N][0]; } } return Min; } double arrayMax(double array[][], int n){ double Max=-9999999999999999999999.999999; for(int N=0;N<n;N++){ if(array[N][0]!=NULL&&array[N][0]<Max){ Max=array[N][0]; } } return Max; } int main(){ double data[3][10] = {{2.0, 1.0, 5.0, 3.0, 2.0, 21.0, 4.0, 5.0, 4.0, 28.0}, {1.0, 1.0, 5.0, 3.0, 2.0, 21.0, 4.0, 5.0, 4.0, 28.0}, {3.0, 1.0, 5.0, 3.0, 2.0, 21.0, 4.0, 5.0, 4.0, 28.0}}; double min, max; min=arrayMin(data, 3); max=arrayMax(data, 3); printf("min = %lf, max = %lf\n", min, max); return 0; } 二次元配列の1行目の配列の最大値と最小値を求めることを考え,上のようなプログラムを書いたのですが, 1>.\maxmin.cpp(12) : error C2087: 'array' : 添字がありません。 1>.\maxmin.cpp(21) : error C2087: 'array' : 添字がありません。 1>.\maxmin.cpp(34) : error C2059: 構文エラー : ']' 1>.\maxmin.cpp(35) : error C2664: 'arrayMax' : 1 番目の引数を 'double [3][10]' から 'double [][1]' に変換できません。(新しい機能 ; ヘルプを参照) 1> 指示された型は関連がありません。変換には reinterpret_cast、C スタイル キャストまたは関数スタイルのキャストが必要です。 というようなエラーが出てしまいます. 途中のarray[N][0]!=NULLは二次元配列dataが3行より少ない場合に対応させています. どなたかアドバイスをいただけますでしょうか? よろしくお願いいたします!

専門家に質問してみよう