2011-04-03 93 views
1

我是C++的新手,我有一个问题。我试图让班级共同的朋友,并在一个从另一个访问不同的成员。我不明白什么是我做wrong.Here是我到目前为止有:第二类没有成员v错误

#ifndef HA_H_ 
#define HA_H_ 
#include"Ha2.h" 
using namespace std; 
class two; 
class one{ 
public: 
    int tip; 
    int timp; 
    one(int t) :tip(t){} 
    friend class two; 
}; 


#endif /* HA_H_ */ 

第二集:

#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_ */ 

和主:

#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(); 
} 

一nd我得到了几个错误,如: 二级没有名为'v'的成员

有什么建议吗?谢谢。

+4

既然你们的班级都没有私人成员,那么你现在并没有真正测试“朋友”做什么。 – 2011-04-03 22:02:07

+2

你也有循环依赖,Ha包括Ha2和Ha2包含Ha。你不能那样做。 – leetNightshade 2011-04-03 22:04:07

+1

发布错误消息。我敢打赌,前面的错误正在引发你提到的错误。 – Matt 2011-04-03 22:04:54

回答

4

main.cpp包括"Ha.h",其在using namespace std;之前包括"Ha2.h"。然后,定义class two,其在std namespace中声明vvector<one>,而没有限定vector

在“Ha.h”中没有必要包含“Ha2.h”,删除它,然后限定向量。

0

另一种方法来去除循环依赖是让一个单独的头文件,就像任何你希望它是decl.h或者,和内,简单地把下面几行:

#ifndef DECL_H_ 
#define DECL_H_ 

class one; 
class two; 

#endif DECL_H_ 

然后在哈。 h和Ha2.h删除#include "Ha.h"#include "Ha2.h"行并将其替换为#include "decl.h"。同样删除Ha.h和Ha2.h中的class one;class two;声明,但保留每个类的定义。

现在你不会有Ha.h取决于Ha2.h,反之亦然。

希望这有助于

杰森

+0

除此之外,只有在其他类中有一个*或两个*时才有帮助。使用固体对象时,编译器需要知道它们的大小,如果它们具有适当的属性等。 – 2011-04-04 09:26:43

4
  • 前向声明时,类定义具有指针或引用使用。而且这两个类别定义都没有它们,所以远期申报是不必要的。

  • 另外,头文件既有前向声明,也有相应的头文件包含,这会使前向声明的整个目的失效。

  • 在源文件中包含标题和using指令。

  • 截至目前,该程序没有方法来测试friend的目的,所以这应该至少使程序编译。


哈。h头:

#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(); 
} 

通过上述修改,您应该摆脱错误。如果弹出任何新错误,请在您的问题中发布确切的错误消息。

+0

@ Em88 - 如果我打算按照我的建议在相应的源文件中定义类方法,则还需要编译所有三个源文件。 – Mahesh 2011-04-03 23:53:12

相关问题