質問事項
ヘッダファイル内で定義したクラスの変数の扱い方がわかりません.
プログラム
ヘッダファイルheader_practice.hpp
の中身をheader_practice.cpp
にて定義しています.最終的に実行したいプログラムはhp.cpp
です.
c++
1//header_practice.cpp 2// ヘッダーファイルの書き方テンプレ 3 4#include<iostream> 5#include "header_practice.hpp" 6 7using namespace std; 8 9void say(){ 10 cout<<"Hello!"<<endl; 11} 12 13struct Point{ 14 double x,y; 15}; 16 17void p_print(Point p){ 18 cout<<p.x<<","<<p.y<<endl; 19} 20 21int main() 22{ 23 Point p; 24 cin>>p.x>>p.y; 25 say(); 26 p_print(p); 27}
c++
1//header_practice.hpp 2class hp{ 3 public: 4 5 struct Point 6 { 7 double x,y; 8 }; 9 10 11 void say(); 12 void p_print(Point p); 13};
c++
1//hp.cpp 2#include<iostream> 3#include "header_practice.hpp" 4 5using namespace std; 6 7struct Point{ 8 double x,y; 9}; 10 11int main() 12{ 13 Point p; 14 hp hdp; 15 cin>>p.x>>p.y; 16 hdp.say(); 17 hdp.p_print(p); 18}
エラー
コンパイルした結果定義したクラスの変数を扱うことができませんでした.どのようにしたら扱うことができるようになるのか教えていただきたいです.
hp.cpp: In function ‘int main()’: hp.cpp:16:17: error: cannot convert ‘Point’ to ‘hp::Point’ 16 | hdp.p_print(p); | ^ | | | Point In file included from hp.cpp:2: header_practice.hpp:11:24: note: initializing argument 1 of ‘void hp::p_print(hp::Point)’ 11 | void p_print(Point p); | ~~~~~~^
struct Point が class hp の中に書いてあるから、かな。

回答2件
あなたの回答
tips
プレビュー