質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

コンストラクタ

オブジェクト指向言語において、オブジェクトを生成時に呼び出され、データの初期化などを行なう関数・メソッドのことである。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

3回答

3215閲覧

オブジェクト指向についての勉強が浅くなぜ未定義エラーが出ているのか分かりません

jagaimo0

総合スコア33

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

コンストラクタ

オブジェクト指向言語において、オブジェクトを生成時に呼び出され、データの初期化などを行なう関数・メソッドのことである。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/10/14 10:32

編集2021/10/15 05:02

質問

C++でのエラーについて
コンストラクタを設定しているはずなのに変数やメソッドのエラーが出ます。
オブジェクト指向についての勉強が浅くなぜコンストラクタでセットしている部分の未定義エラーが出ているのか分かりません
教えていただけると助かります。

実行環境

・C++バージョン
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

・コンパイル
/opt/homebrew/opt/llvm/bin/clang++ -std=c++17 Interaction.cpp

*の位置を修正した後のエラーメッセージ

Interaction.cpp:72:10: warning: address of stack memory associated with local variable 'dv_sep' returned [-Wreturn-stack-address] return dv_sep; ^~~~~~ Interaction.cpp:111:10: warning: address of stack memory associated with local variable 'dv_coh' returned [-Wreturn-stack-address] return dv_coh; ^~~~~~ 2 warnings generated. "/usr/bin/ld" -demangle -lto_library /opt/homebrew/Cellar/llvm/12.0.0_1/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 11.0.0 0.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o a.out /var/folders/k_/4f4mn0653xg82mqzygyrfqlr0000gn/T/Interaction-23c16d.o -lc++ -lSystem /opt/homebrew/Cellar/llvm/12.0.0_1/lib/clang/12.0.0/lib/darwin/libclang_rt.osx.a Undefined symbols for architecture arm64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture arm64 clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

発生している問題・エラーメッセージ

Interaction.cpp:41:22: error: use of undeclared identifier 'n' for(int j = 0; j < n; j++) { ^ Interaction.cpp:46:5: error: use of undeclared identifier 'dis'; did you mean 'div'? dis[j] = E_dis(my_posx, posx[j], my_posy, posy[j]); ^~~ div /opt/homebrew/opt/llvm/bin/../include/c++/v1/stdlib.h:146:42: note: 'div' declared here inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x, ^ Interaction.cpp:46:5: error: subscript of pointer to function type 'lldiv_t (long long, long long) noexcept' dis[j] = E_dis(my_posx, posx[j], my_posy, posy[j]); ^~~ Interaction.cpp:46:14: error: use of undeclared identifier 'E_dis' dis[j] = E_dis(my_posx, posx[j], my_posy, posy[j]); ^ Interaction.cpp:47:5: error: use of undeclared identifier 'angle' angle[j] = E_angle(my_posx, posx[j], my_posy, posy[j]); ^ Interaction.cpp:47:16: error: use of undeclared identifier 'E_angle' angle[j] = E_angle(my_posx, posx[j], my_posy, posy[j]); ^ Interaction.cpp:50:8: error: use of undeclared identifier 'dis'; did you mean 'div'? if(dis[j] <= sep_dis && angle[j] <= sep_angle){ ^~~ div /opt/homebrew/opt/llvm/bin/../include/c++/v1/stdlib.h:146:42: note: 'div' declared here inline _LIBCPP_INLINE_VISIBILITY lldiv_t div(long long __x, ^ Interaction.cpp:50:8: error: subscript of pointer to function type 'lldiv_t (long long, long long) noexcept' if(dis[j] <= sep_dis && angle[j] <= sep_angle){ ^~~ Interaction.cpp:50:18: error: use of undeclared identifier 'sep_dis' if(dis[j] <= sep_dis && angle[j] <= sep_angle){ ^ Interaction.cpp:50:29: error: use of undeclared identifier 'angle' if(dis[j] <= sep_dis && angle[j] <= sep_angle){ ^ Interaction.cpp:50:41: error: use of undeclared identifier 'sep_angle' if(dis[j] <= sep_dis && angle[j] <= sep_angle){ ^ Interaction.cpp:64:17: error: use of undeclared identifier 'sep_force' dv_sep[0] = sep_force * (my_posx - that_ave[0]); // that_sum[0][i]); ^ Interaction.cpp:65:17: error: use of undeclared identifier 'sep_force' 文字数の問題で省略しています。

該当のソースコード

C++

1/* Interaction.hpp */ 2#ifndef __CLASS__INTERACTION 3 4#include <cmath> 5#include <vector> 6 7#define __CLASS__INTERACTION 8 9//===== 相互作用クラス =====// 10class Interaction 11{ 12public: 13 int n; 14 double sep_force; 15 double sep_dis; 16 double sep_angle; 17 double coh_force; 18 double coh_dis; 19 double dis[36] = {}; 20 double angle[36] = {}; 21 22 //--- コンストラクタ ---// 23 Interaction(int nn, double sf, double sd, double sa, double cf, double cd); 24 ~Interaction(); // デストラクタ 25 26 //--- セッタ ---// 27 void set(int nn, double sf, double sd, double sa, double cf, double cd); 28 29 //--- 分離力計算 ---// 30 double *CalculateSeparation(const int num, const double posx[], const double posy[], const bool is_seated, const bool is_group); 31 32 //--- 結合力計算 ---// 33 double *CalculateCohesion(const int num, const double posx[], const double posy[], const int friends_num, const int friends[]); 34 35 //--- 一緒にいる人数の判定 ---// 36 int CountWithNum(const int num, const double posx[], const double posy[], const int friends_num, const int friends[]); 37 38 //--- 距離計算 ----// 39 double E_dis (double xi, double xj, double yi, double yj){ 40 double dis = 0; 41 dis = sqrt((xi-xj)*(xi-xj)+(yi-yj)*(yi-yj)); 42 return dis; 43 } 44 45 //--- 角度計算 ---// 46 double E_angle (double xi, double xj, double yi, double yj){ 47 double theta = 0; 48 double rx = 0; // x距離 49 double ry = 0; // y距離 50 rx = xi - xj; 51 ry = yi - yj; 52 theta = atan2(ry, rx); // 個体間角度計算 53 54 return theta; 55 } 56}; 57 58#endif

C++

1/* Interaction.cpp */ 2#include "Interaction.hpp" 3 4//--- コンストラクタ定義 ---// 5Interaction::Interaction(int nn, double sf, double sd, double sa, double cf, double cd){ 6 set(nn, sf, sd, sa, cf, cd); 7} 8 9/* 10//--- デストラクタ定義 ---// 11Interaction::~Interaction(){ 12 if(dis != NULL) delete [] dis; 13 if(angle != NULL) delete [] angle; 14} 15*/ 16 17//--- セッタ ---// 18void Interaction::set(int nn, double sf, double sd, double sa, double cf, double cd){ 19 n = nn; 20 sep_force = sf; 21 sep_dis = sd; 22 sep_angle = sa; 23 coh_force = cf; 24 coh_dis = cd; 25} 26 27//--- 分離力計算 ---// 28double Interaction::*CalculateSeparation(const int num, const double posx[], const double posy[], const bool is_seat, const bool is_group){ 29 double that_sum[2] = {}; 30 double that_count = 0; 31 double that_ave[2] = {}; 32 double dv_sep[2] = {}; 33 double my_posx = posx[num]; 34 double my_posy = posy[num]; 35 36 //--- 初期化 ---// 37 that_sum[0] = 0; 38 that_sum[1] = 0; 39 that_count = 0; 40 41 // 個体間距離と角度の計算 // 42 for(int j = 0; j < n; j++) { 43 if(num == j) continue; 44 if(is_seat) continue; // 着席した人は力の影響を受けない 45 if(is_group == false) continue; // 合流していない人は分離力の影響を受けない 46 // 個体間距離と角度を計算 // 47 dis[j] = E_dis(my_posx, posx[j], my_posy, posy[j]); 48 angle[j] = E_angle(my_posx, posx[j], my_posy, posy[j]); 49 50 // 分離力の範囲内なら // 51 if(dis[j] <= sep_dis && angle[j] <= sep_angle){ 52 // 範囲内の個体の座標を合計 // 53 that_sum[0] += posx[j]; 54 that_sum[1] += posy[j]; 55 that_count += 1; 56 } 57 } 58 59 // 範囲内の個体の平均座標 // 60 if(that_count != 0){ 61 that_ave[0] = that_sum[0] / that_count; 62 that_ave[1] = that_sum[1] / that_count; 63 64 // 分離力 // 65 dv_sep[0] = sep_force * (my_posx - that_ave[0]); // that_sum[0][i]); 66 dv_sep[1] = sep_force * (my_posy - that_ave[1]); // that_sum[0][i]); 67 }else{ 68 // 分離力 // 69 dv_sep[0] = 0; 70 dv_sep[1] = 0; 71 } 72 73 return dv_sep; 74} 75 76//--- 結合力計算 ---// 77double Interaction::*CalculateCohesion(const int num, const double posx[], const double posy[], const int friends_num, const int friends[]){ 78 double that_sum[2] = {}; 79 double that_count = 0; 80 double that_ave[2] = {}; 81 double dv_coh[2] = {}; 82 double dis = 0; 83 double angle = 0; 84 double my_posx = posx[num]; 85 double my_posy = posy[num]; 86 87 for(int k=0; k<friends_num; k++){ 88 if(friends[k] == -1) continue; 89 dis = E_dis(my_posx, posx[friends[k]], my_posy, posy[friends[k]]); 90 angle = E_angle(my_posx, posx[friends[k]], my_posy, posy[friends[k]]); 91 if(dis >= coh_dis){ 92 that_sum[0] += posx[friends[k]]; 93 that_sum[1] += posy[friends[k]]; 94 that_count++; 95 } 96 } 97 98 // 範囲内の個体の平均座標 // 99 if(that_count != 0){ 100 that_ave[0] = that_sum[0] / that_count; 101 that_ave[1] = that_sum[1] / that_count; 102 103 // 結合力 // 104 dv_coh[0] = coh_force * (that_ave[0] - my_posx); // that_sum[0][i]); 105 dv_coh[1] = coh_force * (that_ave[1] - my_posy); // that_sum[0][i]); 106 }else{ 107 // 結合力 // 108 dv_coh[0] = 0; 109 dv_coh[1] = 0; 110 } 111 112 return dv_coh; 113} 114 115//--- 一緒にいる人数の判定 ---// 116int Interaction::CountWithNum(const int num, const double posx[], const double posy[], const int friends_num, const int friends[]){ 117 double my_posx = posx[num]; 118 double my_posy = posy[num]; 119 int withNum = 0; 120 121 for(int j=0; j<n; j++) { 122 if(num == j) continue; 123 // 個体間距離と角度を計算 // 124 dis[j] = E_dis(my_posx, posx[j], my_posy, posy[j]); 125 angle[j] = E_angle(my_posx, posx[j], my_posy, posy[j]); 126 if(dis[j] <= coh_dis + friends_num+1){ // 範囲内にいれば 127 for(int k=0; k<friends_num; k++){ // 友人リストに含まれていたら 128 if(friends[k] == j) withNum++; // 合流人数 129 } 130 } 131 } 132 133 return withNum; 134}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2021/10/14 10:34

浅いと原因がはっきりしてるならそのボトルネックをなんとかするのが良いと思うんですが。 そんなに突貫工事で作っても使い物にはならないでしょう。
jagaimo0

2021/10/14 11:00

すいません。勉強のために作っているので急に必要なものではありません
BeatStar

2021/10/14 11:15

とりあえず、エラーメッセージは省略せずにちゃんと張った方がいいです。 たまに勘違いしている場合もありますし。
y_waiwai

2021/10/14 11:33

エラーが出たなら、エラーメッセージを提示しましょう エラーメッセージは、いらぬ翻訳省略しないで、出たそのママをコピペで提示してください
jagaimo0

2021/10/14 13:04

ご指摘通り伝わりにくかったと思います。 原文のままに修正しました。ありがとうございます
BeatStar

2021/10/14 13:46

このメッセージはozwkさんの仰るように#ifndef(← nが付いている方)に変えた時のものでいいんですよね?
jagaimo0

2021/10/14 14:08

はい。その通りです
BeatStar

2021/10/14 14:17

さすがにhppファイルとcppファイルが別のところにあるってことは無いですよね?
jagaimo0

2021/10/14 14:55

はい、同じところにあります。
yuki23

2021/10/14 15:30

実行環境とコンパイルに使ったコマンドを記載してください
jagaimo0

2021/10/14 16:20

記載しました
dodox86

2021/10/15 00:07 編集

問題としているエラーとは直接関係ないのですが、提示のコードで、 > #Interaction.hpp が気になります。不正なプリプロセッサ命令としてエラーになると思うのですが、これは直っている前提で話が進んでいるのでしょうか。※もしかすると質問文に見出し(マークダウン)で含むつもりだったけど、コードの方に入っちゃった、ということかな?
jagaimo0

2021/10/15 02:56

分かりにくくてすいません。ファイル名を記載した方がいいかなと思って書きました。 その通りで、実際のコードには含まれていません。
dodox86

2021/10/15 02:59

> 分かりにくくてすいません。ファイル名を記載した方がいいかなと思って書きました。 そういう場合はC++の文法にのっとってコメントの一部などとして書きます。 // Interaction.hpp とか /* Interaction.hpp */ です。マークダウン記法でかかれたコードブロックのコードは、多くの方がソースコードそのものとして読みます。そこへ注釈も無しに独自の記法を混ぜると、混乱や誤解を呼びます。
jagaimo0

2021/10/15 03:15

修正しておきます。ありがとうございます
BeatStar

2021/10/15 03:31

他の回答者のifndefに書き換えに対するコメントで『排除されているメッセージもある』とのことなので、『修正後のメッセージも』載せた方がいいですね。 前提が変わっている可能性もあるので。(一つ目のエラーを直すと二つ目以降の理由ががらりと変わることもしばしば)
jagaimo0

2021/10/15 04:14

はい。修正後のエラーメッセージが上記に記載したものです。
fana

2021/10/15 04:23

とりあえず, 「それは delete できねぇだろ」 だとか 「is_seated って何ですか? 意味わかんないんですけど.よもや is_seat の間違いでは?」 だとかいう, しょーもないエラーくらいはさっさと自分で潰せばよいのでは????????
fana

2021/10/15 04:26

> use of undeclared identifier 'n' とかいう素朴すぎるエラーの{意味,これが出てきた原因,対処方法}等,とにかく全くわからない! とかいう話なのですか? ??? ?????
jagaimo0

2021/10/15 04:31

すみません。 聞きたいところは未定義エラーがなぜ出てきているかです。 エラーの意味は理解していますが、なぜそのエラーが出てきているのかが分からないです。
jagaimo0

2021/10/15 04:34

ご指摘通り、自力で解決できる部分については解決して訂正しました。
Zuishin

2021/10/15 04:36

> Interaction.cpp:41:22: error: use of undeclared identifier 'n' これは自力でできませんか?
Zuishin

2021/10/15 04:38

> 聞きたいところは未定義エラーがなぜ出てきているかです。 上のエラーは n が宣言されていないので出ています。 どこで宣言されていますか? 探してみてみつからなければ宣言されていないのでは?
ozwk

2021/10/15 04:44

> Zuishinさん n はメンバ変数です。 宣言されているはず(ifndefにしたことで解決済み)なのに なぜ宣言されていないことになっているんだ?ということかなと (そしてその答えが*の位置が違う)
BeatStar

2021/10/15 04:53

あら、メッセージが変わってたわ。
Zuishin

2021/10/15 04:55

どこで宣言されているか探すのも面倒だったので聞きましたが、メンバ変数だったんですね。 メソッドの書き方がおかしいためにメソッドと認識されていなかったということですか。
jagaimo0

2021/10/15 04:56

はい。*の位置を直して未定義関係のエラーは消えました。
matukeso

2021/10/15 05:18

もう質問自体は解決してるよね? ifdef違いでhppが消えていた+ポインタの*の位置が違ってメンバ関数扱いされなかった、が理由。
dodox86

2021/10/15 05:21

なぜこんなに引っ張る・解決しないのかわかりませんが、コードを書くときはコンパイルしないままいきなり全部書くのではなく、少し書いて(<コンストラクタだけ、デストラクタだけとか)コンパイル、実行を確認し、少しずつ追加して進めます。間違えたままたくさんコードを書いて大量のエラーに悩まされることが少なくなります。
fana

2021/10/15 05:23

> n はメンバ変数です 他の未定義はあからさまに * 関連だと見えても,nだけはローカル変数の未定義だと思っていた. 今は反省している.
jagaimo0

2021/10/15 05:24

はい。最初にわからなかった部分は解決できました。 おっしゃる通りです。最初warningメッセージが出ていたんですけど、エラーが出ていなくて、無視して進めてしまったために、たくさんエラーが出てしまいました。
guest

回答3

0

ベストアンサー

ifdefをifndef


double Interaction::*CalculateCohesion(...)などポインタを返したいであろう関数は
double *Interaction::CalculateCohesion(...)

投稿2021/10/14 10:39

編集2021/10/15 04:33
ozwk

総合スコア13528

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jagaimo0

2021/10/14 11:07

ありがとうございます。ただ、結果は変わりませんでした。
ozwk

2021/10/14 11:52

エラーメッセージも変化してませんか?
jagaimo0

2021/10/14 13:05

はい。変化していません。
matukeso

2021/10/14 22:31

手元のclang++でコンパイルすると、最初のエラーって下記のように変わるけど、そういう変化がないって事? (#ifdefの時) Interaction.cpp:4:1: error: use of undeclared identifier 'Interaction' (#ifndefの時)./Interaction.hpp:39:11: error: use of undeclared identifier 'sqrt' いっぺん#ifdef,#endifの行を取っ払ってみたら。
jagaimo0

2021/10/15 03:08

すみません。ifdef -> ifndefで Interaction.cpp:4:1: error: use of undeclared identifier 'Interaction' のエラーは取れていました。見逃していました。申し訳ないです 現在のエラーメッセージは上記の通りになっています。
ozwk

2021/10/15 03:39

じゃあsqrtとか数学関数系をincludeすれば解決ですね
jagaimo0

2021/10/15 04:02

ありがとうございました。 ただ現状では コンストラクタでセットしていると思っているsep_forceなどに対する error: use of undeclared identifier 'sep_force' などのエラーが今の所取れていない状態です。
ozwk

2021/10/15 04:11

それはifndefへの書き換えで解決しませんか?
jagaimo0

2021/10/15 04:13

はい。書き換えた後が現在のエラーメッセージに載せているものです
fana

2021/10/15 04:38

> double Interaction::*CalculateCohesion(...) これってどういう意味に解釈されるんだろう…?
ozwk

2021/10/15 04:39

> fanaさん 私も気になって調べてるんですがどうなるんでしょうね?
fana

2021/10/15 04:44

class Test{}; double Test::* XXX(){ return 2; } //※てきとーに戻り値を書いてエラーを出してみる とかやってみると, error C2440: 'return': 'int' から 'double Test::* ' に変換できません。 とかおっしゃるのだが,「double Test::*」とかいうのは,一体どういう型なのか…?
matukeso

2021/10/15 04:51

「interactionクラスのメンバ変数へのポインタ」(double型)を返す関数Calculateナントカの定義ですね
jagaimo0

2021/10/15 04:52

ありがとうございます。上記の double Interaction::*CalculateCohesion(...)などポインタを返したいであろう関数は double *Interaction::CalculateCohesion(...) を実行したところ 未定義関係のエラーがかなり消えてエラーメッセージが、 Interaction.cpp:72:10: warning: address of stack memory associated with local variable 'dv_sep' returned [-Wreturn-stack-address] return dv_sep; ^~~~~~ Interaction.cpp:111:10: warning: address of stack memory associated with local variable 'dv_coh' returned [-Wreturn-stack-address] return dv_coh; ^~~~~~ 2 warnings generated. Undefined symbols for architecture arm64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture arm64 clang-12: error: linker command failed with exit code 1 (use -v to see invocation) になりました。 -vを使った表示がこちらです clang version 12.0.0 Target: arm64-apple-darwin20.3.0 Thread model: posix InstalledDir: /opt/homebrew/opt/llvm/bin "/opt/homebrew/Cellar/llvm/12.0.0_1/bin/clang-12" -cc1 -triple arm64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Interaction.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-rounding-math -munwind-tables -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-a12 -target-feature +v8.3a -target-feature +fp-armv8 -target-feature +neon -target-feature +crc -target-feature +crypto -target-feature +fullfp16 -target-feature +ras -target-feature +lse -target-feature +rdm -target-feature +rcpc -target-feature +zcm -target-feature +zcz -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -debugger-tuning=lldb -target-linker-version 650.9 -v -resource-dir /opt/homebrew/Cellar/llvm/12.0.0_1/lib/clang/12.0.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -stdlib=libc++ -internal-isystem /opt/homebrew/opt/llvm/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm/12.0.0_1/lib/clang/12.0.0/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /Users/mahiro/clusterParticleModel/test -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -o /var/folders/k_/4f4mn0653xg82mqzygyrfqlr0000gn/T/Interaction-23c16d.o -x c++ Interaction.cpp clang -cc1 version 12.0.0 based upon LLVM 12.0.0 default target arm64-apple-darwin20.3.0 ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks" #include "..." search starts here: #include <...> search starts here: /opt/homebrew/opt/llvm/bin/../include/c++/v1 /opt/homebrew/Cellar/llvm/12.0.0_1/lib/clang/12.0.0/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory) End of search list.
matukeso

2021/10/15 04:55

return &Interaction::sep_force;とかかける。
fana

2021/10/15 05:10 編集

> メンバ変数へのポインタ なるほど! 感謝! //確認完了 class Test{ public:double X=39.0831; }; double Test::* XXX(){ return &Test::X; } std::cout << Test().*XXX(); //Usage
ozwk

2021/10/15 05:12

> jagaimo0 > warning: address of stack memory associated with local variable 要約 : ローカル変数のアドレスを返してはいけません。
jagaimo0

2021/10/15 05:19

なるほど。訂正してwarningの方は取れました。ありがとうございます。 errorの方は残っている状態です。 質問ばかりで申し訳ないのですが、このエラーって何で出ているんですかね? 少し調べてみたんですけど実装忘れの可能性があるとのことで一応ヘッダのデストラクタ部分はコメントアウトしたんですけど改善されませんでした。
jagaimo0

2021/10/15 06:01

丁寧に教えていただきありがとうございました。 最初疑問に思っていたところはおかげさまで解決することができたので、 リンクエラーに関しては別に質問を立てようと思います。
jagaimo0

2021/10/15 06:17

理由はよく分かりませんが、mainがあるところから実行したら治りました。重ねてありがとうございました
guest

0

C++がオブジェクト指向プログラミングにしばしば使われる道具であるというのは事実ですが、質問内容は「オブジェクト指向」とは何の関係もありません。なので、オブジェクト指向についていくら勉強しても直接的にはその解は得られません。単純に、C/C++の使い方として勉強すればいい範囲です。
(オリンピック出場を目指していますが、近所の体育館の予約の仕方はどうすればいいですか...と聞かれたみたいな感じ?)

ifdefをifndef

で変化なかったとして、その次としてヘッダファイルの冒頭に

#ifndef __CLASS__INTERACTION #include <vector> #define __CLASS__INTERACTION ここでわざとエラーを起こす aaa; //===== 相互作用クラス =====// class Interaction { public: <以下略>

とでもしてコンパイルを試し、その部分が確実にエラーになるか確認してください。

疑っているのは、実際にコンパイルに使っているのではないファイルを修正して「コンパイル結果が変わらない」と悩んでいること。意外とよくある話です。

投稿2021/10/14 23:24

thkana

総合スコア7639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jagaimo0

2021/10/15 02:59

aaa;の方はエラー出ませんでしたが、 アイウエオと単純に書きましたがそっちはエラーがしっかり出ました。
jagaimo0

2021/10/15 03:09

すみません。ifdef -> ifndefについては Interaction.cpp:4:1: error: use of undeclared identifier 'Interaction' のエラーは取れていました。見逃していました。申し訳ないです 自分の勘違いでした。
guest

0

あとはメッセージを読んでください。

Interaction.cpp:72:10: warning: address of stack memory associated with local variable 'dv_sep' returned [-Wreturn-stack-address] [訳] Interaction.copの72行目。 ワーニングです。ローカルな配列系の値を返すのは大変危険です。

ヒント: ここ

ヒントをもとに考えてみてください。

つまり、配列を返すのではなく、『ポインタとして』返すか、『引数を戻り値扱いにして』返すかでしょうね。

投稿2021/10/15 05:21

BeatStar

総合スコア4958

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jagaimo0

2021/10/15 05:29

ありがとうございます。 ポインタとして返して、こちらのエラーは解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問