• 締切済み

C++ ポインタについて質問です

c++ で三角形の周囲を計算で出したいのですが、エラーがでてしまいます。うまくpoint が機能していないようなのですがどこが悪いのでしょうか?? main.cppの中身 #ifdef TRIANGLE_DONE { // Define three points Point p1 = {1, 3}, p2 = {-2, -2}, p3 = {3, -1}; // Create a Triangle variable Triangle t = {p1, p2, p3}; // Now, test the perimeter() function assertDoubleEqualsMsg("Check perimeter of Triangle", 15.4021, perimeter(t), 1E-5); } #else cerr << "Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h" << endl; #endif hの中身 #define TRIANGLE_DONE struct Triangle{ Point a, b, c; }; #ifdef TRIANGLE_DONE /** * Calculates the perimeter of the Triangle t. * @param t the Triangle to examine. * @return the perimeter. */ double perimeter(const Triangle& t); #endif h19.cppのなかみ double premiter(const Triangle& t) { double p = 0; p = t.p1 + t.p2 + t.p3; return p; } 必ず "Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h" のコメントがでてしまいます。どのように改善すれば良いでしょうか?

  • kko00
  • お礼率0% (0/10)

みんなの回答

回答No.3

多分、何かの演習で、回答者に求められているのが、構造体 Triangle を定義すると言うことかなと思います。 で、Triangle を定義した後で、コメントアウトされていた、#define TRIANGLE_DONE のコメントを取ると、全体として、動くプログラムになるようになっているのかなと。 で、これで、Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h が出力されていないと言うことは、inh19.h がインクルードされてないのでしょう。 さて、 struct Triangle{ Point a, b, c; }; となっています。 で、struct Point は、いかにも、「点」を示す、double のペアだという雰囲気が漂っています。 一方で、double premiter(const Triangle& t) で、struct Triangle のメンバーであるはずの、p1, p2, p3 がアクセスされています。 ということは、この時点で、Triangle の定義は間違っているか、足りないかです。 C++の世界では、struct も class と同じ能力を持つので、 struct Triangle { Point a, b, c; // こっちは座標で定義 double p1, p2, p3 // こっちは辺の長さ }; という定義で、a, b, c を決めたら、p1, p2, p3 も自動的に決めるようなこともできますが、それを求めているのかなぁ?

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.2

あと, 何がなんだかさっぱりわからない Point の中身もほしいが.... いったいどこに「ポインタ」の出てくる余地があるというのか.

  • Wr5
  • ベストアンサー率53% (2177/4070)
回答No.1

>必ず "Define the Triangle structure. Then, uncomment #define TRIANGLE_DONE in h19.h" のコメントがでてしまいます。 main.cppでTRIANGLE_DONEが定義されていないからでしょう。 で……こういう「ファイルの一部分」ではなく、全体を掲示した方がいいんじゃないですかね。 main.cppでh19.hをincludeしているのかどうかも不明ですよ?

関連するQ&A

  • 複数のライブラリをリンクするときに構造体が衝突する

    C++,開発環境はVisual Studio Professional 2013です, ライブラリ1にxy座標の構造体 struct point { int x; int y; }; を定義しています. ライブラリ2にも全く同じ構造体を定義しています. ここで,あるプロジェクトからこれら2つのライブラリをリンクしようとしたら, error C2011: 'point' : 'struct' 型の再定義 というコンパイルエラーが出ます. ライブラリ1とライブラリ2にpoint構造体が記述された共通用のヘッダを参照させるという方法をとれば解決できるのでしょうが,この方法以外でこのエラーをなんとか回避する方法はないでしょうか. 構造体宣言は只の宣言であって実体をもたないため,中身が同じであればいくら記述が重複しても問題はないと聞いたことがあったので大丈夫だと思っていました. また,ライブラリ1とライブラリ2のpoint構造体の名前それぞれpoint1,point2にするなど違うものにするという方法でも回避できるのでしょうが,下記のメインコード内でコメントアウトされている部分のように,構造体の中身が全く同じなので互換性を持たせられるようにしたいのです. ちなみに,ライブラリ1,2はコンソールアプリケーション・空のプロジェクトで作成したあと,ソリューションのプロパティで「構成の種類」を「スタティックライブラリ(.lib)」にするという手順で作成しています. 以下,簡略コード ====ライブラリ1==== ----lib1.h---- #ifndef _H1_ #define _H1_ struct point { int x; int y; }; void print_lib1(struct point p); #endif ----lib1.cpp---- #include "lib1.h" #include <iostream> void print_lib1(struct point p) { std::cout << "lib1 : " << p.x << ", " << p.y << std::endl; } =====ライブラリ2====== ----lib2.h---- #ifndef _H2_ #define _H2_ struct point { int x; int y; }; void print_lib2(struct point p); #endif ----lib2.cpp---- #include "lib2.h" #include <iostream> void print_lib2(struct point p) { std::cout << "lib2 : " << p.x << ", " << p.y << std::endl; } ===メインコード=== ----main.cpp---- #include "../../lib1/lib1/lib1.h" #include "../../lib2/lib2/lib2.h" int main(void) { struct point val; val.x = 2; val.y = 3; print_lib1(val); //print_lib2(val); }

  • コンストラクタの中でメンバクラスをnewしてはだめなのですか?

    C#2008EXPRESS EDITIONです。 以下のようなソースで、★1ではT.aがちゃんと生成されてるのに、★2のようにすると、Tの各メンバがNULLになってしまいます。 こういうやり方は、だめなのでしょうか?デバッガでは、引数無しのコンストラクタの内容を実行しているようなのですが・・・ なお、当方はCは理解していますが、C++を随分前に落伍した程度の者で、今回C#に挑戦してみようとしています。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { public class point { public double x; public double y; public point(double a, double b) { x = a; y = b; } } public class Triangle { public point a; public point b; public point c; public Triangle() { point a = new point(0,0); point b = new point(0,0); point c = new point(0,0); } public Triangle(point a,point b,point c){ this.a = a; this.b = b; this.c = c; } } static void Main(string[] args) { ★1 Triangle T = new Triangle(new point(1,2),new point(3,4),new point(5,6)); ★2 Triangle T = new Triangle(); Console.WriteLine("T.a.x={0} T.a.y={1}",T.a.x,T.a.y); } } } すみません、インデントを付ける方法がわからなくて、見にくいソースになってしまって。

  • C言語 ポインタのポインタ

    失礼します。現在ある確保したメモリに対してCSVから得た値を配列として入れていきたいのですが、うまくいきません。エラーとしてはコマンドプロンプトに何も表示がされていない状態です。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include<string.h> int main(void) { FILE *fp; double *fname = "./testfile.csv"; int i = 0; double **p; p = (double *)malloc(1000); //1000個のdouble(11行分) for (i = 0; i < 11; i++) { //行の先頭要素にアクセスして1000個のメモリを確保 p[i] = malloc(1000 * sizeof(double)); } fp = fopen(fname, "r"); if (fp == NULL) { printf("%sファイルが開けません¥n", fname); return -1; } double c; int column = 0; int row = 0; while ((c = fgetc(fp)) != EOF) { if (c == ',') { column++; //putchar('\n'); } else if (c == '\n') { row++; column = 0; //putchar('\n'); } else { p[row][column] = c; } } printf("%1f", p[0][1]); free(p); fclose(fp); return 0; } 何卒よろしくお願いします。 CSVの内容は、 gagdgj,gasgag,fgdagg fgaafgaf,gfaggfag,gfagd といった形です。 通報する

  • VC++2010 EE で #ifdef _MSC_VER #endi

    VC++2010 EE で #ifdef _MSC_VER #endif でエラー ソース始まり // _msc_ver.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #ifdef _MSC_VER #include "stdafx.h" #endif int _tmain(int argc, _TCHAR* argv[]) { return 0; } ソース終わり > fatal error C1020: 予期しない #endif です。 と出ます。なぜでしょうか?

  • C++の組込みマクロのassert()が使えない

    こんにちは。 CygwinでC++開発を行っているのですが、C++の関数型マクロであるassert()が使えなくて困っています。 main関数を含むソースファイルに、 #include assert.h を記述しておけば使えるはずなのですが、そのソースファイルから実行ファイルを、 g++コマンドでビルドしようとすると、以下のようなエラーが出ます。 ------------------------------------------------------------ $ g++ test03-STLの使用.cpp /cygdrive/c/Users/Kei/AppData/Local/Temp/cckwDP4e.o:test03-STLの使用.cpp:(.text+0x904): undefined reference to `___assert_func' collect2: ld はステータス 1 で終了しました ------------------------------------------------------------ test03-STLの使用.cpp のmain関数は、以下の通りです。 ------------------------------------------------------------ int main() { string s="ABC DEF\nGH\tIJ"; reverse(s.begin(), s.end() ); assert(s=="JI\tHG\nFED CBA"); cout<<s return 0; } ------------------------------------------------------------ ちなみに、実際にインクルードされる /usr/include/assert.h の内容は以下のようになっていました。 ------------------------------------------------------------ /* assert.h */ #ifdef __cplusplus extern "C" { #endif #include "_ansi.h" #undef assert #ifdef NDEBUG /* required by ANSI standard */ # define assert(__e) ((void)0) #else # define assert(__e) ((__e) ? (void)0 : __assert_func (__FILE__, __LINE__, \ __ASSERT_FUNC, #__e)) # ifndef __ASSERT_FUNC /* Use g++'s demangled names in C++. */ # if defined __cplusplus && defined __GNUC__ # define __ASSERT_FUNC __PRETTY_FUNCTION__ /* C99 requires the use of __func__. */ # elif __STDC_VERSION__ >= 199901L # define __ASSERT_FUNC __func__ /* Older versions of gcc don't have __func__ but can use __FUNCTION__. */ # elif __GNUC__ >= 2 # define __ASSERT_FUNC __FUNCTION__ /* failed to detect __func__ support. */ # else # define __ASSERT_FUNC ((char *) 0) # endif # endif /* !__ASSERT_FUNC */ #endif /* !NDEBUG */ void _EXFUN(__assert, (const char *, int, const char *) _ATTRIBUTE ((__noreturn__))); void _EXFUN(__assert_func, (const char *, int, const char *, const char *) _ATTRIBUTE ((__noreturn__))); #ifdef __cplusplus } #endif ------------------------------------------------------------ Borland C++ Compilerのbcc32コマンドでは、先ほどのソースファイルから実行ファイルをビルドすることができたので、何が問題なのかが分かりません。 以上の件について何かご存知の方がいらっしゃれば、是非教えて頂きたいと思います。 では、よろしくお願い致します。

  • C++でConvertSidToStringSid関数を使うために、sddl.hを使用したい

    お世話になります。 現在C++ 6.0でソフトを作成しています。 その中でConvertSidToStringSid関数を使うために、sddl.hを使用したいのですが、下記のようなエラーが出ます。 -----------インクルード------------------ #include <stdlib.h> #pragma comment(lib,"netapi32.lib") //////////ここで定義しているつもりです。 #define _WIN32_WINNT 0x0500 #include <windows.h> #include <sddl.h> //////////ここで定義しているつもりです。 #include <tchar.h> #include <lmaccess.h> #include <lmapibuf.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ----------デバックエラー J:\Del.cpp(17) : fatal error C1083: インクルード ファイルがオープンできません。'sddl.h': No such file or directory どのようにすればConvertSidToStringSid関数を使うために、sddl.hを使用できますでしょうか? 何卒よろしくお願いします。

  • condefs.hはどこにあるの?

    #include <stdio.h> #include <windows.h> #ifdef __BORLANDC__ #include <condefs.h> #endif をヘッダに含むプログラムspitest.cppを無償コンパイラのボーランドC++5.5でコンパイルすると Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland spitest.cpp: エラー E2209 spitest.cpp 8: インクルードファイル 'condefs.h' をオープンできない *** 1 errors in Compile *** とエラーがでました。 どうしたらいいのでしょうか?

  • DirectX開発中のinclude ファイルを開けません。

    DirectX開発中に c:\documents and settings\a022\my documents\visual studio 2008\projects\map\map\stdafx.h(10) : fatal error C1083: include ファイルを開けません。'WinLib.h': No such file or directory MapModel.cpp 上のソースコードは、 #ifdef _MSC_VER // 警告の抑制 #pragma warning(disable: 4201) #pragma warning(disable: 4100) #pragma warning(disable: 4786) #endif #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x400 #endif #include <WinLib.h> ←ここがよめないらしい というエラーが出ます。 解決法がわかる方はお教えください。

  • シンボルをエクスポートするDLLの初歩的トラブル

    VC++6.0で簡単なDLL作成にチャレンジしたところ、ソースファイルの最後のところで、エンドオブファイルのエラーが出ます。教本と同じように書いたつもりですが、何処が異なっているのか分かりません。エラーを出す原因となっている箇所を御指摘下さい。 ↓ソースファイル #include "stdafx.h" #include "SUB.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } SUB_API LONG _stdcall subtract( LONG sub1 , LONG sub2 ) { return( sub1 - sub2 ); } ↓ここからはヘッダーファイル #ifdef SUB_EXPORTS #define SUB_API __declspec(dllexport) #else #define SUB_API __declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif SUB_API LONG __stdcall subtract( LONG sub1, LONG sub2 ); #ifdef _cplusplus } #endif --------------------構成: SUB - Win32 Debug-------------------- コンパイル中... StdAfx.cpp コンパイル中... SUB.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\SUB\SUB.cpp(38) : error C2059: 構文エラー : 'end of file' cl.exe の実行エラー SUB.dll - エラー 1、警告 0

  • ポインタの指しているアドレスは同じなのに表示される内容が違う。

    行列に関するプログラムです。 ライブラリのヘッダファイル matrix.h #ifndef MATRIX_H #define MATRIX_H #ifdef __cplusplus extern "C"{ #endif typedef struct{ int row,col; double *elements; } MATRIX; extern int matrix_error_code; extern MATRIX zero_matrix(MATRIX a); extern MATRIX identity_matrix(MATRIX a); extern void func_matrix(MATRIX a); #ifdef __cplusplus } #endif #endif ライブラリのソースファイル matrix.c #include <stdio.h> #include <stdlib.h> #include <math.h> #include "matrix.h" int matrix_error_code = 0; MATRIX zero_matrix(MATRIX a) /*零行列をつくる*/ { int i,size; double *p; size=a.row*a.col; for(i=0;i<size;i++) { if((p = malloc(sizeof(double))) == NULL) { puts("メモリを確保できません."); exit(0); } *p=0; } a.elements=p-size; return a; } MATRIX identity_matrix(MATRIX a) /*単位行列をつくる*/ { int i; a=zero_matrix(a); for(i=0;i<a.row;i++) { *(a.elements+i*a.col+i)=1.0000; } return a; } void func_matrix(MATRIX a) { int i,j; double *p; printf("b.elementsの値:%p\n",a.elements); printf("b[1,1]の値:%lf\n",a.elements); } 動作確認用のアプリケーションファイル test_matrix.c #include <stdio.h> #include "matrix.h" int main(void) { int i,j; MATRIX b; b.row=2; b.col=2; b=identity_matrix(b); for(i=0;i<b.row;i++) { for(j=0;j<b.col;j++) { printf("%3.4lf ",*(b.elements+i*b.col+j)); } printf("\n"); } printf("\n\n"); printf("b.elementsの値:%p\n",b.elements); printf("b[1,1]の値:%lf\n",b.elements); func_matrix(b); return 0; } void func_matrix(MATRIX a)内のprintf("b.elementsの値:%p\n",a.elements);とtest_matrix.c内のprintf("b.elementsの値:%p\n",b.elements);で表示される値は同じなので、main関数のbがvoid func_matrix(MATRIX a)内のaに正しく引き渡されていると思うのですが、それぞれの次の行での、printf("b[1,1]の値:%lf\n",b.elements);とprintf("b[1,1]の値:%lf\n",a.elements);とでは表示される値が違い、理由がわからず困っています。 どうか回答をよろしくお願いします。

専門家に質問してみよう