简体   繁体   中英

request for a member of non-aggregate type?

I'm trying to create a very simple classe to stock a tree. I've created a class for the nodes and a class for the tree which consist in a tab of pointers. My problem is that i cannot access to the public members of my class of nodes(I think).

That's the code of my class:

#include <cmath> 
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>


using namespace std;

template <class T>
class Node : public vector<T> {

public:
     vector<T> enfants;
     T pere; 
     T valeur; 

  Node(){valeur=T(0); pere=T(0); enfants= vector<T>();}
  Node(T v){valeur=v; pere=T(0); enfants= vector<T>();}
  Node(T v, T p) {valeur= v; pere=p; enfants=vector<T>();}     

void accouche(const T& x)
{
   enfants.push_back(x);
}



template <class T>
class Arbre: public Node<T> {

public:

      Node<T> * arbre;

      Arbre(int n){arbre= new Node<T> [n];}
      Arbre(int n, T a) {arbre= new Node<T> [n]; arbre[0]=Node<T>(a);}

void ajouter(const T& enfant, const int& posf, const T& per, const int& posp )      
{
arbre[posp].accouche(enfant);
arbre[posf]=Node<T>(enfant,per);
}


 };

And that's my main code

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include "classenode.hpp"


using namespace std;

int main()

{
Arbre<int> a1(4,0);

a1.ajouter(1,1,0,0);
a1.ajouter(2,2,1,1);
a1.ajouter(3,3,0,0);

cout<<a1[0].enfants.size()<<endl;
system("pause");       

}

I'ts suppose to have the result "2". But I have this error

32 `enfants' has not been declared 
32  request for member of non-aggregate type before '.' token 

I've tried to use "a1[0]->enfants.size()" but the programe sais that the base operand is not a pointer.

Any tips? Thank you!!

a1[0]的类型是int ,因此a1[0].enfants无效。

a1 is an Arbre , but a1[0] is an int , so it doesn't have have any enfants .

Inheriting from vector is usually a bad idea; I'm not sure it's what you meant to do here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM