エラー
再定義エラーが起きてしまう。
発生している問題・エラーメッセージ
./Container/Container.cpp:4:12: error: redefinition of 'Container' Container::Container(double x, double y, double w, double h, int size, double mm){ ^ ./Container/Container.cpp:4:12: note: previous definition is here Container::Container(double x, double y, double w, double h, int size, double mm){ ^ In file included from VoronoiTest.cpp:24: ./Container/Container.cpp:9:12: error: redefinition of '~Container' Container::~Container(){ ^ ./Container/Container.cpp:9:12: note: previous definition is here Container::~Container(){ ^ In file included from VoronoiTest.cpp:24: ./Container/Container.cpp:14:17: error: redefinition of 'set' void Container::set(std::vector<Wall> w){ ^ ./Container/Container.cpp:14:17: note: previous definition is here void Container::set(std::vector<Wall> w){ ^ VoronoiTest.cpp:41:13: error: no matching constructor for initialization of 'Container' Container container(40, 250, 200, 230, 30, 1); ^ ~~~~~~~~~~~~~~~~~~~~~~~~ ./Force/../Container/Container.hpp:11:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 6 were provided class Container ^ 4 errors generated.
該当のソースコード
Container.hpp
1#ifndef __CLASS__CONTAINER 2#define __CLASS__CONTAINER 3 4#include <vector> 5#include "../Voronoi/Voronoi.cpp" 6#include "Wall.cpp" 7 8class Container 9{ 10public: 11 std::vector<Wall> wall; 12 13 // コンストラクタ // 14 Container(double x, double y, double w, double h, int size, double mm); 15 16 // 端座標と縦横から容器を設定 // 17 void set(double x, double y, double w, double h, int size, double mm); 18}; 19#endif
Container.cpp
1#include "Container.hpp" 2 3// コンストラクタ // 4Container::Container(double x, double y, double w, double h, int size, double mm){ 5 set(x, y, w, h, size, mm); 6} 7 8// セッタ // 9void Container::set(double x, double y, double w, double h, int size, double mm){ 10 Wall top = Wall(Point(x,y), Point(x+w,y), size, mm); 11 wall.push_back(top); 12 Wall right = Wall(Point(x+w,y), Point(x+w,y-h), size, mm); 13 wall.push_back(right); 14 Wall bottom = Wall(Point(x+w,y-h), Point(x,y-h), size, mm); 15 wall.push_back(bottom); 16 Wall left = Wall(Point(x,y-h), Point(x,y), size, mm); 17 wall.push_back(left); 18}
試したこと
#pragma once
は試しました。
補足情報(FW/ツールのバージョンなど)
clang++ -std=c++17でコンパイルしています。他のクラスではうまくいっています。
うまくいっているもの
T1transition.hpp
1#ifndef __CLASS__T1 2#define __CLASS__T1 3 4#include <vector> 5#include "../Voronoi/Voronoi.cpp" 6 7class T1transition 8{ 9 double min_dis; //縮退を起こすエッジの長さ 10public: 11 // コンストラクタ // 12 T1transition(double mm){ 13 min_dis = mm; 14 } 15 16 // Edgeの配向を変える // 17 Voronoi transition(Voronoi v); 18 19}; 20 21#endif
#include "T1transition.hpp" // Edgeの配向を変える // Voronoi T1transition::transition(Voronoi v){ // 処理省略 }
違いが分からず悩んでます。
力をお貸しいただけるとありがたいです。
Container.hppでcppをインクルードしてますが意図通りですか?(普通はこんなことしません)
意図通りです。
Container.hpp内でWall.cppなどをインクルードしないとWallを使用できないのでインクルードしてます。
#define __CLASS__CONTAINER などを #ifndef __CLASS__CONTAINER の直後に移動すれば良いかも。
(Wall.cpp が #include "Container.hpp" してたら多重定義になるのでは…。)
先ほど結果が変わらなかったと伝えてしまいましたが、全ファイルこの形式に直したところうまく行きました。ありがとうございます。
エラーメッセージをよく見てませんでしたが、Container.hpp ではなく Container.cpp のエラーでしたね。ってことは Container.cpp を複数箇所で #include してるのでは。というか、やはり .cpp を #include するのはどうかと思いますね。そもそも、複数の .cpp ファイルをどうやってビルドしてるのでしょうか?
同時になぜ普通はcppをインクルードしないのか分かった気がします。ozwkさんもありがとうございました
はい、そういう理由でした。
勉強になりました。
あるcppがincludeされる前提のものなら、そのcppにはinclude guardが必要なのでは。そしてそうであればそのファイルにはcppではなく、hppという名前がついている筈なんだけどね
はい、理解できました。ありがとうございます
回答1件
あなたの回答
tips
プレビュー