I am new to C++ and I have a problem.I am trying to make to classes mutual friend,and accessing different members in one from the other one.I can't figure out what is that I do wrong.Here is what I have so far:
#ifndef HA_H_
#define HA_H_
#include"Ha2.h"
using namespace std;
class two;
class one{
public:
int tip;
in开发者_开发知识库t timp;
one(int t) :tip(t){}
friend class two;
};
#endif /* HA_H_ */
The second header:
#ifndef HA2_H_
#define HA2_H_
#include"Ha.h"
#include<vector>
class one;
class two{
public:
vector<one> v;
vector<int> x;
inline void create_x(vector<one> v){
// vector<one>::iterator it=v.begin();
int i;
for(i=0;i<v.size();i++){
x.push_back(v.at(i).tip);
}
}
friend class one;
};
#endif /* HA2_H_ */
And the main:
#include<vector>
#include<iostream>
#include"Ha.h"
#include"Ha2.h"
int main()
{
one o(3);
two x;
x.v.push_back(o);
x.create_x(x.v);
cout<< x.x.back();
}
And I get several errors like: class two has no member named 'v'
Any advice?Thank you.
main.cpp includes "Ha.h"
, which, before using namespace std;
, includes "Ha2.h"
. Then, class two
is defined, which declares v
as a vector<one>
without qualifying vector
in the std namespace
.
There is no need to include "Ha2.h" in "Ha.h", remove that, then qualify vector.
Forward declarations are used when the class definition has pointers or references. And neither of the class definitions has them and so forward declarations are unnecessary.
Also the headers has both forward declarations and it's corresponding header inclusion which voids the whole purpose of forward declarations.
Include the headers and also the
using
directive in the source files.As of now, the program has no methods to test the purpose of
friend
and so this should at least make the program compile.
Ha.h header :
#ifndef HA_H_
#define HA_H_
class two ; // No pointers or references of two in one.
// So, remove it and place when you actually have
// a method taking reference of two.
class one{
public:
int tip;
int timp;
one(int t) :tip(t){}
friend class two;
};
#endif /* HA_H_ */
Ha.cpp
#include "Ha2.h" // Place this only when have a method accessing
// members of two. Else this is unnecessary.
#include "Ha.h"
using namespace std ;
Ha2.h
#ifndef HA2_H_
#define HA2_H_
class one ; // No pointers or references of one in two.
// So, remove it and place when you actually have
// a method taking reference of one.
class two{
public:
vector<one> v;
vector<int> x;
inline void create_x(vector<one> v)
{
// vector<one>::iterator it=v.begin();
int i;
for(i=0;i<v.size();i++)
{
x.push_back(v.at(i).tip);
}
}
friend class one;
};
#endif /* HA2_H_ */
Ha2.cpp
#include <vector>
#include "Ha.h" // Place this only when have a method accessing
// members of two. Else this is unnecessary.
#include "Ha2.h"
using namespace std ;
// .....
main.cpp
#include <vector> // Definitely need this because in the current unit, Ha2.h is
// included which has a data type of vector<int>
#include <iostream>
#include"Ha.h"
#include"Ha2.h"
using namespace std ; // vector and the other standard definitions are specified
// in std namespace
int main()
{
one o(3);
two x;
x.v.push_back(o);
x.create_x(x.v);
cout<< x.x.back();
}
With the above modifications, you should get rid of errors. If any new errors pop up, post the exact error messages in your question.
Another alternative to remove the circular dependency is to make a separate header file, like decl.h
or whatever you want it to be, and inside of that simply put the following lines:
#ifndef DECL_H_
#define DECL_H_
class one;
class two;
#endif DECL_H_
Then in Ha.h and Ha2.h remove the #include "Ha.h"
and #include "Ha2.h"
lines and replace them with #include "decl.h"
. Also remove the class one;
and class two;
declarations in Ha.h and Ha2.h, but keep the definitions of each respective class.
Now you won't have Ha.h depending on Ha2.h and vice versa.
Hope this helps,
Jason
精彩评论