• ベストアンサー

C++超初心者質問

Visual C++ 2008 Express Edition Ver9.0.30729.1 SP を利用し、以下のプログラムを実行するための手順を教えてください。 Visual C++ 6.0では、ファイル>新規作成→ファイルTAB>C++ソースファイルのように、上記の場合はどのように進んだらよいのでしょう? ファイル>新規作成>プロジェクト→Win32 コンソール アプリケーション??? とすると、↓のエラーが出てしまいます。尚、VC2008をインストールしてから何も触っていません。 http://okwave.jp/qa4665322.html ソース #include<iostream> using namespace std; class coord{ int x,y; public: coord(){x=0;y=0;} coord(int i,int j){x=i,y=j;} void get_xy(int &i,int &j){i=x;j=y;} friend coord operator+(coord ob1,int i); friend coord operator+(int i,coord ob1); }; coord operator+(coord ob1,int i){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } coord operator+(int i,coord ob1){ coord temp; temp.x=ob1.x+i; temp.y=ob1.y+i; return temp; } int main(){ char c[3]; // 画面固定のため coord o1(10,10); int x,y; o1=o1+10;; o1.get_xy(x,y); cout<<"(o1+10)X:"<<x<<",Y:"<<y<<"\n"; o1=99+o1;; o1.get_xy(x,y); cout<<"(99+o1)X:"<<x<<",Y:"<<y<<"\n"; cout<<"\nエンターで抜けます"<<endl; // 画面固定のため gets(c); return 0; }

noname#102223
noname#102223

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

  • ベストアンサー
  • D-Matsu
  • ベストアンサー率45% (1080/2394)
回答No.1

「全般→空のプロジェクト」でプロジェクトを作成し、ソースファイルを右クリックして「追加→新しい項目→C++ソースファイル」でソースを作成してください。

noname#102223
質問者

お礼

ありがとうございます! やっと作業が続けられます!

その他の回答 (1)

回答No.2

>↓のエラーが出てしまいます とは? C1010ですか? >ファイル>新規作成>プロジェクト→Win32 コンソール アプリケーション のときに、 「プリコンパイル済みヘッダ」にチェックを入れていませんでしたか? プロジェクト内にstdafx.hがあるのであれば、対象のファイルの先頭行に #include "stdafx.h" を記述してください。 もしくは、オプションで使わないようにすることもできます。 [構成プロパティ]-[C/C++]-[プリコンパイル済みヘッダ] の プリコンパイル済みヘッダの作成/使用 を プリコンパイル済みヘッダーを使用しない に変更。 プロジェクトから、stdafx.h,stdafx.cppを削除

関連するQ&A

  • C++について教えてください。(初心者です)

    現在C++についての学習を進めているのですが、 2項演算子のオーバーロードで理解できないところがありますので、よろしかったらご教授ください。 //+をcoordクラスに対してオーバーロードする #include<iostream> using namespace std; class coord{ int x,y; public: coord() {x=0;y=0;} coord(int i,int j) {x=i;y=j;} void get_xy(int &i,int &j) {i=x;j=y;} coord operator+(coord ob2); }; //+をcoordクラスに対してオーバーロードする coord coord::operator+(coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } int main() { coord o1(10,10),o2(5,3),o3; int x,y; o3 = o1 + o2; o3.get_xy(x,y); cout << "(o1+o2) X:" << x << ",Y:" << y << endl; return 0; } この文の中で、o3.get_xy(x,y);というコードがありますが、 ここの部分がよくわからないのです。 そもそも、引数としてx,yがありますが、これはprivateメンバを 見に行きなさい。っと言っているのでしょうか? main()の中から直接使っている?? それとも?? すいません。この辺の理解が薄いようなので、2項演算子のオーバーロードとは関係ないかもしれませんが、教えてください。 よろしくお願いします。

  • プログラムの出力の説明を教えてください。

    出力が、画像のようになるのですが、最後に基本クラスのoperator=()が呼ばれるのがわからないです。 なぜ、そのようになるのか教えてください。 /* +,-,=をcoordクラスに関してオーバーロードし、 そのcoordをquadの基本クラスとして使用する */ #include <iostream> using namespace std; class coord{ public: int x, y; //座標 coord(int i = 0, int j = 0){ x = i; y = j; } void get_xy(int &i, int &j){ i = x; j = y; } coord operator+(coord ob2); coord operator-(coord ob2); coord operator=(coord ob2); }; //+をcoordクラスに関してオーバーロードする coord coord::operator+(coord ob2) { coord temp; cout << "coord operator+()を使用" << endl; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } //-をcoordクラスに関してオーバーロードする coord coord::operator-(coord ob2) { coord temp; cout << "coord operator-()を使用" << endl; temp.x = x - ob2.x; temp.y = y - ob2.y; } //=をcoordクラスに関してオーバーロードする coord coord::operator=(coord ob2) { cout << "coord operator=()を使用" << endl; x = ob2.x; y = ob2.y; return *this; //代入先のオブジェクトを返す } class quad :public coord{ int quadrant; public: quad(){ x = 0; y = 0; quadrant = 0; } quad(int x, int y) :coord(x, y) { if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; } void showq() { cout << "象限を示す:" << quadrant << endl; } quad operator=(coord ob2); }; quad quad::operator=(coord ob2) { cout << "quad operator=()を使用" << endl; x = ob2.x; y = ob2.y; if (x >= 0 && y >= 0)quadrant = 1; else if (x<0 && y >= 0)quadrant = 2; else if (x<0 && y<0)quadrant = 3; else quadrant = 4; return *this; } int main() { quad o1(10, 10), o2(15, 3), o3; int x, y; o3 = o1 + o2; //2つのオブジェクトの加算。演算子+()を呼ぶ o3.get_xy(x, y); o3.showq(); cout << "(o1+o2)X:" << x << ",Y:" << y << endl; o3 = o1 - o2; //2つのオブジェクトの減算 o3.get_xy(x, y); o3.showq(); cout << "(o1-o2)X:" << x << ",Y:" << y << endl; o3 = o1; //オブジェクトを代入する o3.get_xy(x, y); o3.showq(); cout << "(o3=o1)X:" << x << ",Y:" << y << endl; return 0; }

  • C++ ソートのやり方

    僕が作ったプログラムで、これはバブルソートなのかわからないので教えてください。 また、ほかのソートの仕方も教えてください。 よろしくお願いします。 汎用関数を使っているのでわかりにくいかもしれないですがお願いします。 #include <iostream> using namespace std; template <class X>void Sort(X *data, int size) { X temp; for (int i = 0; i < size; i++){ for (int j = i + 1; j < size; j++){ if (data[i]>data[j]){ temp = data[i]; data[i] = data[j]; data[j] = temp; } } } } int main() { int i[10]{1, 4, 3, 5, 2, 10, 2, 7, 6, 8}; char c[10]{'c', 'b', 'z', 'a', 'x', 'y', 'j', 'n', 'm', 'r'}; Sort(c, 10); Sort(i, 10); for (int j = 0; j < 10; j++){ cout << i[j] << ' '; } cout << endl; for (int j = 0; j < 10; j++){ cout << c[j] << ' '; } cout << endl; getchar(); return 0; }

  • C++について。

    現在”猫でもわかるプログラミング”のC++編をSDKと共に勉強している身です。 現在第22章、第23章を勉強中です。 22章 http://www.kumei.ne.jp/c_lang/cpp/cpp_22.htm 23章 http://www.kumei.ne.jp/c_lang/cpp/cpp_23.htm 作業環境はVisual Studio 2005.net C++です 22章でのプログラムを作成し、実行した結果エラーが出てしまいました。 ソースです #include <iostream> class xy_position { int x; int y; public: xy_position(int x = 0, int y = 0){ xy_position::x = x; xy_position::y = y; } int X() const {return x;} int Y() const {return y;} }; ostream& operator << (ostream& o, const xy_position& p) { return o << "(" << p.X() << "," << p.Y() << ")"; } int main(void) { xy_position a(50, 60), b; std::cout << a << b << std::endl; return 0; } (17):error C2143: 構文エラー : ';' が '&' の前にありません。 (17):error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません (17):error C2065: 'o' : 定義されていない識別子です。 (17):error C2059: 構文エラー : 'const' (18):error C2143: 構文エラー : ';' が '{' の前にありません。 (18):error C2447: '{' : 対応する関数ヘッダーがありません (旧形式の仮引数リスト?) (26):error C2679: 二項演算子 '<<' : 型 'xy_position' の右オペランドを扱う演算子が見つかりません (または変換できません)。 このようなエラーが出てしまいました。 もちろんソースは全て同様に書いています。 この”猫でも”で使用しているコンパイラと異なるために出たエラーでしょうか? それに単に cout << のように記述するとエラーが出てしまい、 std::cout << のように記述しなければ通りません。 また、エラーとは別の質問になってしまいますが、プログラム中に int X() const {return x;} という記述がありますが、このconstの意味が分かりません。 単純に return x が変更不可能という意味でしょうか? 次に23章についての質問です。 ここでもソースは同じなのに以下のようなエラーが出てしまいます。 ソースです。 #include <iostream> int main(void) { int x=10, y=15, z=20; std::cout << "16進表示" << std::endl; std::cout.setf(ios::hex); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; std::cout.unsetf(iostream::hex); std::cout << "8進数" << std::endl; std::cout.setf(ios::oct); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; return 0; } (8):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (8):error C2065: 'hex' : 定義されていない識別子です。 (13):error C2653: 'iostream' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2065: 'oct' : 定義されていない識別子です。 これも何か設定をしなければいけないのでしょうか? なにぶんC++は・・・というかオブジェクト指向の言語は初心者なもので疑問も多いですorz

  • C++ 2次元配列について 【 初心者です 】

    こんにちは.C++初心者です. 以下のプログラムは, オブジェクトの2次元配列の作成と そのアクセスをポインタで行うことを 目的としています. 以下の□■部が質問箇所です. なぜobをsamp型でキャストするのか分かりません. obはすでにsamp型で宣言しているのに… それと※部において 2度目のp++処理について教えていただきたいです. メモリーイメージを書いてもらえると ありがたいです。 よろしくおねがいします。 #include <iostream.h> using namespace std; class samp { int a; public: samp(int n) { a = n; } int get_val() { return a; } }; int main(void) { samp ob[3][2] = { 1, 2 3, 4, 5, 6 }; int i; samp *p; // □■□■□■□■ p = (samp *) ob; for(i = 0; i < 3; i++) { cout << p->get_val() << ' '; p++; ※ cout << p->get_val() << endl; ※ p++; } cout << endl; return 0; } }

  • 派生クラスから基本クラスprotectedメンバへのアクセスについて

    C++初心者です。 以下のような基本クラスcoordと、その派生クラスquadがあり、quadクラスに関して=演算子をオーバーロードしました。 class coord { protected: int x, y; }; class quad : public coord { public: quad operator=(coord &ob2); }; quad quad::operator=(coord &ob2) { x = ob2.x; y = ob2.y; } するとob2からxにアクセスできないとエラーが出ます。 quadクラスのメンバ関数内なので、基本クラスのprotectedメンバにはアクセスできると思ったのですが、違うのでしょうか。 ご教授お願いします。

  • C言語、行列の積を求めるプログラムについて

    「次に示す行列x,yの積を求めるプログラムを作成せよ。   x[2][3]={{1,2,3},{4,5,6}} y=[3][2]={{1,5},{5,3},{81}}」 という問題です。自分ではとりあえず、 #include<stdio.h> int main(void) { int i,j; int x[2][3]={{1,2,3},{4,5,6}}; int y[3][2]={{1,5},{5,3},{8,1}}; int xy[3][3]={0}; for(i=0;i<3;i++) for(j=0;j<3;j++) xy[i][j]=x[i][j]*y[i][j]; for(i=0;i<3;i++){ for(j=0;j<3;j++) printf("%3d",xy[i][j]); putchar('\n'); } return 0; } というプログラムを作ってみましたが、ダメでした。 ちゃんと積の表示が出るようにするにはどこをどう変えるべきでしょうか?

  • C++で行列とベクトルの積を求める

    行列とベクトルの掛け算 y=Ax (A(3*3行列以上)とxを適当に初期化) を作成せよ これが分からないんですが誰か分かる人いませんか?下は行列の和と積をそれぞれ求めてるんですが、こんな感じになりそうなんですよね #include<iostream> using namespace std; int main() { double A[3][3]={{1,1,6},{5,3,2},{2,2,2}}; double B[3][3]={{4,1,3},{2,4,3},{5,9,2}}; double temp; int i,j,k; for(i=0;i<3;i++){ for(k=0;k<3;k++){ } } for(i=0;i<3;i++){ for(k=0;k<3;k++){ } } cout<<"和:A+B="<< '\n'; for(i=0;i<3;i++){ cout<<" { "; for(j=0;j<3;j++){ cout<< (A[i][j]+B[i][j]); if(j!=2) cout<<" , "; } cout<< " }" << '\n'; } cout<< '\n'; cout<<"積:A*B="<< '\n'; for(i=0;i<3;i++){ cout<<" { "; for(j=0;j<3;j++){ temp=0.0; for(k=0;k<3;k++) temp += A[i][k]*B[k][j]; cout<< temp; if(j!=2) cout<< " , "; } cout<< " }" << '\n'; } return 0; }

  • C言語 プログラム 確率

    今、学校の授業で病気の感染プログラムを作っています。 内容は、 ・3カテゴリー(0:健康体、1:感染者、2:免疫体)の人間がいる。 ・2カテゴリー(0:病原体を持たない蚊、1:病原体ウイルスを持つ蚊)の蚊がいる。 ・人間と蚊がxy座標上をある速度でランダムに移動している。 ・健康体の人間とウイルスを持った蚊と人間ある一定距離内にいると蚊が人間を刺して病気が感染し、人間が0:健康体から1:感染者に変化する。 簡単に言いましたが、このようなプログラムを作っています。 その中で、人間に感染する部分のプログラムを下に載せます。 double c0x, c0y, ax, ay, bx, by; for(i=0; i<N;++i){ if(b[i].category==1){ c0x=(*cat0ka).coord.x; c0y=(*cat0ka).coord.y; ax=a[i].coord.x; ay=a[i].coord.y; bx=b[i].coord.x; by=b[i].coord.y; if(((c0x-ax)*(c0x-ax)+(c0y-ay)*(c0y-ay))<R){ (*cat0agent).category=1; } else if(a[i].category==1){ c0x=(*cat0agent).coord.x; c0y=(*cat0agent).coord.y; ax=a[i].coord.x; ay=a[i].coord.y; bx=b[i].coord.x; by=b[i].coord.y; if(((c0x-bx)*(c0x-bx)+(c0y-by)*(c0y-by))<R){ (*cat0ka).category=1; return ; } } int t; for(t=1; 7-t;++t ){ if((*cat0agent).category=1){ if(6<t){ (*cat0agent).category=2; } } } (*cat0agent).coord.x+=(frand()-0.5); (*cat0agent).coord.y+=(frand()-0.5); (*cat0ka).coord.x+=(frand()-0.5); (*cat0ka).coord.y+=(frand()-0.5); } } } このプログラムでは、一定距離内にいると100%感染するようになっていますが、 それを、"一定距離内にいると30~50%の確率で感染する"という条件に変えたいと思っていますが、そのやり方がわかりません。 どなかか分かりやすく丁寧に教えていただけませんか? お願いします。 ちなみに感染率は今30%で考えてもらえれば良いです。 載せたプログラム自体も完成しきれていないので、わからないかもしれません。 すみません。

  • C++ 法線の計算

    Depthmapをもつ画像のピクセルごとに法線を計算する関数を作成したのですが、いまいち計算が遅いです。if文をできるだけ使わないようにしたり、自分なりに変更したのですが、どなたかアドバイスを頂けないでしょうか。 Visual Studio, windows7, C++ です。 Tvector3<float> calc_normal(Tvector3<float> p1, Tvector3<float> p2, Tvector3<float> p3){ Tvector3<float> v1; Tvector3<float> v2; Tvector3<float> cross; float length; Tvector3<float> n; v1 = p1 - p2; v2 = p3 - p2; cross = v2.cross(v1); /* 外積v2×v1の長さ|v2×v1|(= length)を求める */ cross.normalize(); /* 長さ|v2×v1|が0のときは法線ベクトルは求められない */ if (length == 0.0f) { //cout<<"lenth=0"<<endl; //break (); } /* 外積v2×v1を長さ|v2×v1|で割って法線ベクトルnを求める */ //cout<<cross<<endl; return cross; } void calc_all_noraml(Ttexturef dmap, vector<Tvector3<float>> &n){ int w = dmap.getWidth(); int h = dmap.getHeight(); Tvector3<float> pc,pl,pr,pd,pu; int j=0; cout<<j<<endl; for(int i=0; i<w; i++){ vector<Tvector3<float>> temp; pc = Tvector3<float>(i,j,return_depth(dmap,i,j)); if(i-1 > 0)pl = Tvector3<float>(i-1,j,dmap.get(i-1,j).r); if(i+1 < w)pr = Tvector3<float>(i+1,j,dmap.get(i+1,j).r); pu = Tvector3<float>(i,j+1,dmap.get(i,j+1).r); if(i-1 > 0)temp.push_back(calc_normal(pl,pu,pc)); if(i+1 < w)temp.push_back(calc_normal(pc,pu,pr)); Tvector3<float> sum(0,0,0); for(int k=0; k<(int)temp.size(); k++){ sum += temp.at(k); } sum.normalize(); //cout<<sum<<endl; n.push_back(sum); } for(j=1;j<(h-1);j++){ cout<<j<<endl; int i=0; vector<Tvector3<float>> temp; pc = Tvector3<float>(i,j,return_depth(dmap,i,j)); pr = Tvector3<float>(i+1,j,return_depth(dmap,i+1,j)); pu = Tvector3<float>(i,j+1,return_depth(dmap,i,j+1)); pd = Tvector3<float>(i,j-1,return_depth(dmap,i,j-1)); temp.push_back(calc_normal(pc,pu,pr)); temp.push_back(calc_normal(pc,pr,pd)); Tvector3<float> sum(0,0,0); for(int k=0; k<(int)temp.size(); k++){ sum += temp.at(k); } sum.normalize(); n.push_back(sum); temp.clear(); for(int i=1; i<(w-1); i++){ Tvec

専門家に質問してみよう