質問
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}
回答3件
あなたの回答
tips
プレビュー