エラー
再定義エラーが起きてしまう。
発生している問題・エラーメッセージ
./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
#ifndef __CLASS__CONTAINER #define __CLASS__CONTAINER #include <vector> #include "../Voronoi/Voronoi.cpp" #include "Wall.cpp" class Container { public: std::vector<Wall> wall; // コンストラクタ // Container(double x, double y, double w, double h, int size, double mm); // 端座標と縦横から容器を設定 // void set(double x, double y, double w, double h, int size, double mm); }; #endif
Container.cpp
#include "Container.hpp" // コンストラクタ // Container::Container(double x, double y, double w, double h, int size, double mm){ set(x, y, w, h, size, mm); } // セッタ // void Container::set(double x, double y, double w, double h, int size, double mm){ Wall top = Wall(Point(x,y), Point(x+w,y), size, mm); wall.push_back(top); Wall right = Wall(Point(x+w,y), Point(x+w,y-h), size, mm); wall.push_back(right); Wall bottom = Wall(Point(x+w,y-h), Point(x,y-h), size, mm); wall.push_back(bottom); Wall left = Wall(Point(x,y-h), Point(x,y), size, mm); wall.push_back(left); }
試したこと
#pragma once
は試しました。
補足情報(FW/ツールのバージョンなど)
clang++ -std=c++17でコンパイルしています。他のクラスではうまくいっています。
うまくいっているもの
T1transition.hpp
#ifndef __CLASS__T1 #define __CLASS__T1 #include <vector> #include "../Voronoi/Voronoi.cpp" class T1transition { double min_dis; //縮退を起こすエッジの長さ public: // コンストラクタ // T1transition(double mm){ min_dis = mm; } // Edgeの配向を変える // Voronoi transition(Voronoi v); }; #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という名前がついている筈なんだけどね
はい、理解できました。ありがとうございます
まだ回答がついていません
会員登録して回答してみよう