Undeclared identifiers in a class

I'm trying to modify a class (i.e. I'm hoping it's one small thing that I forgot to change and not actually 38 errors) to use dynamic arrays rather than a static array. However, when compiling, I get 38 (new record!) errors in my program. A lot of them seem to be related to undeclared identifiers:

error C2065: 'size_type' : undeclared identifier
'value_type' : undeclared identifier

Simply put, these seem like odd errors to be getting. Both of these were just typedef statements in the header file given to us. Where am I going wrong?

Here's the implementation file (this was written by me, so look for the error in here. I'll include the header file for the sake of reference):

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>  // provides cout
#include <cassert>   // provides assert
#include <cstdlib>   // provides size_t
#include "sequence2.h"
using namespace std;

namespace main_savitch_4
{
	sequence::sequence(size_type initial_capacity)
	{
		data = new value_type[initial_capacity];	// Creates dynamic array pointed to by data,
		capacity = initial_capacity;				// of size initial_capacity
		current_index = 0;
		used = 0;
	}

	sequence::sequence(const sequence& source)
	{
		data = new value_type[source.capacity];		// Creates dynamic array pointed to by data,
		capacity = source.capacity;					// of size source.capacity
		used = source.used;
		current_index = source.current_index;

		for(size_type i = 0; i<used; ++i)
		{
			data[i]=source.data[i];		// Copies array from array pointed to by source.data
		}								// to array pointed to by data
	}

	sequence::~sequence( )
	{
		delete [ ] data;
	}

	void reserve(size_type new_capacity)
	{
        value_type *larger_array;

        if (new_capacity == capacity)
            return; // The allocated memory is already the right size.

        if (new_capacity < used)
            new_capacity = used; // Can’t allocate less than we are using.

        larger_array = new value_type[new_capacity];
		for(size_type i = 0; i<used; ++i)	// Copy data from array pointed to by data to
		{									// array pointed to by larger_array
			larger_array[i] = data[i];
		}
        delete [ ] data;
        data = larger_array;
        capacity = new_capacity;
    }
    
	void sequence::advance()
	{
		assert(is_item());		// Changes current item to next item in sequence
		++current_index;		// as long as current item is a valid item
	}

	void sequence::insert(const value_type& entry)
	{
		if(used == capacity)			// If dynamic array is currently full, increase size 
			reserve(used+used/10+1);	// by at least 10%

		if (current_index == used)	// If there is no current item, insert new item
			current_index = 0;		// at the beginning of the sequence


		for (size_t i = used; i > current_index; --i)
		{							
			data[i] = data[i-1];	// Shift all items in the array from current_index
		}							// to used one space forward

		data[current_index] = entry;	// Insert new entry at current_index,
		++used;							// increase count of items in array
		return;
	}

	void sequence::attach(const value_type& entry)
	{
		if(used == capacity)			// If dynamic array is currently full, increase size 
			reserve(used+used/10+1);	// by at least 10%

		if (current_index == used)			// If there is no current item, add item at
		{									// current_index and increase used
			data[current_index] = entry;
			++used;
			return;
		}

		for (size_t i = used; i > current_index+1; --i)
		{								// Shift items in array from current_index+1 to used
			data[i] = data[i-1];		// one space forward
		}

		++current_index;				// Increase current_index to identify correct
		++used;							// current item, increase count of items in array,
		data[current_index] = entry;	// insert new entry at current_index
		return;
	}

	void sequence::remove_current()
	{
		assert (is_item());		// Ensure current_index refers to a valid item

		if(current_index == used-1)		// If current item is the last item in the
		{								// sequence, reduce used by 1 to shorten
			--used;						// sequence.
			return;
		}

		for(size_t i = current_index; i < used-1; ++i)
		{							// Shifts items in array beyond current index
			data[i] = data[i+1];	// one space backward, reduce used by 1 to 
		}							// shorten sequence
		--used;
		return;
	}

	void operator=(const sequence& source)
	{
		value_type *new_data;

		// Check for possible self-assignment
		if (this == &source)
			return;

		// If needed, allocate an array with a different size
		if (capacity != source.capacity)
		{
			new_data = new value_type[source.capacity];
			delete [ ] data;
			data = new_data;
			capacity = source.capacity;
		}

		// Copy data from the source array
		used = source.used;
		for(size_type i = 0; i<used; ++i)
		{
			data[i] = source.data[i];
		}
		current_index = source.current_index;
	}



	sequence::value_type sequence::current() const
	{
		assert (is_item());
		return data[current_index];
	}

}


Here's the header:

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
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>  // Provides size_t

namespace main_savitch_4
{
    class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        // typedef std::size_t size_type;
		typedef size_t size_type;
        // static const size_type DEFAULT_CAPACITY = 30;
		enum {DEFAULT_CAPACITY = 30};
        // CONSTRUCTORS and DESTRUCTOR
        sequence(size_type initial_capacity = DEFAULT_CAPACITY);
		sequence(const sequence& source);
		~sequence();
        // MODIFICATION MEMBER FUNCTIONS
		void reserve(size_type new_capacity);
        void start( ) { current_index = 0; };
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
		void operator=(const sequence& source);
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const { return used; };
        bool is_item( ) const { return (current_index < used); };
        value_type current( ) const;
    private:
        value_type *data;
        size_type used;
		size_type capacity;
        size_type current_index;
    };
}

#endif 


Hmmm

try accessing the types if sequence::typename.

This seems like something that SHOULD work, but apparently it doesn't.
Last edited on
hanst99 is right - since those typedefs are defined inside the class, the only way to use them outside of the class is with a using directive or the :: operator. You could define them in the namespace if that is not what you wanted.

The other problem preventing it from compiling is this: void reserve(size_type new_capacity) and this: void operator=(const sequence& source)
You are just missing "sequence::" before both function names.
Last edited on
Yes bran, but within the class itself you really shouldn't have to use the scope operator...
Ah, that's it. As Bran says, some of the functions implementations don't have sequence:: in front of them, so the compiler isn't automatically checking inside the class for those typedefs.
Ah ha. Thanks for the reply. Still not used to writing with classes obviously :S
Topic archived. No new replies allowed.