Please Help!!! Finding length of pointer

on line 55, I get error that like this: error: request for member 'length' in 'number->Pokedex::rest', which is of pointer type 'Pokedex*' (maybe you meant to use '->' ?)
return 1 + number->rest.length();

does anyone know what the problem is?
I just want to do If I have the empty Pokedex, the length is zero, otherwise the
length of the Pokedex is 1 plus the length of the rest of the Pokedex.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
using namespace std;

struct Move
{
  string name;
  int selfHPEffect;
  int otherHPEffect;
  int selfAtkEffect;
  int otherAtkEffect;
  int selfDefEffect;
  int otherDefEffect;
};

struct Pokemon
{
  string name;
  string type;
  int centimeterHeight;
  int gramWeight;
  int hp;
  int attack;
  int defense;
  Move move1;
  Move move2;
};

struct Pokedex
{
  Pokemon* p;
  Pokedex* rest;
};

Pokedex* empty()
{
  return new Pokedex {NULL, NULL};
}

bool isEmpty(Pokedex* head)
{
  return head -> p == NULL;
}

Pokedex* length(Pokedex* number)
{
  if(number -> p == NULL)
  {
    return 0;
  }
  else
  {
    return 1 + number->rest.length();
  }
}
Last edited on
length is not a member function, so neither
 
    number->rest.length()
nor
 
    number->rest->length()
would work.

Instead, pass number->rest as a parameter of the function length().
 
    return 1 + length(number->rest);

Topic archived. No new replies allowed.