"Error 1 error C2228: left of '.size' must have class/struct/union"

Doing a sorting algorithm project and I am currently running into an error that I cannot fix.

I have only attempted to implement one of the sorting algorithms and I wanted to ask and hopefully resolve this before I go to implement the rest.

here is the error:
"Error 1 error C2228: left of '.size' must have class/struct/union"

Here are the code snippets:
My Code
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
#include <iostream>
#include "SortingHelper.h"
using namespace std;



template <class T>
class Sorting{

public:
	T selectionsort(T* data, int size);
	T insertionsort(T* data, int size);
	T mergesort(T* data, int size, T* temp);
	T quicksort(T* data, int size);

	T data;

};

template <class T> 
T selectionsort(T* data, int size)
{
	T arr[] = { *data };
	size = arr.size();

	if (isSorted(data, size) == true)
	{
		return *data;
	}
	else{
		for (int i = 0; i < size - 1; i++)
		{
			T m = data[i + 1];
			if (m < i)
			{
				Swap(m, i);
			}
		}
		}
	return *data;
}
template <class T> 
T insertionsort(T* data, int size)
{
	if (isSorted(*data, size) == true)
	{
		return *data;
	}
}
template <class T> 
T mergesort(T* data, int size, T* temp)
{
	if (isSorted(*data, size) == true)
	{
		return *data;
	}
}
template <class T> 
T quicksort(T* data, int size)
{
	if (isSorted(*data, size) == true)
	{
		return *data;
	}
}


THESE ARE TWO SEGMENTS OF THE PROVIDED CODE:
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
#ifndef __SORTING_HELPER_H
#define __SORTING_HELPER_H

#include <iostream>
using namespace std;

//Functions for allocating various integer arrays of length n
int* increasingArray(int n);
int* decreasingArray(int n);
int* zeroArray(int n);
int* randomArray(int n);

/**
Returns whether an array is in sorted order
Note:  the values in the array must be comparable with <; compilation will fail otherwise.

@param arr the array to test
@param n the length of the array
@return whether the array is sorted
*/
template <class T> bool isSorted(T* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
		if (arr[i + 1] < arr[i])
			return false;
	return true;
}

/**
Prints an array of size n, with spaces in between and a newline at the end

@param arr the array to print
@param n the length of the array
*/
template <class T> void printArray(T* arr, int n)
{
	for (int i = 0; i < n; i++)
		cout << arr[i] << ' ';
	cout << endl;
}

/**
Compares 3 values and returns which is the median
When two values are equal, one of the two will be indicated
Note:  these values must be comparable with <; compilation will fail otherwise.

@param a,b,c  3 values
@return 1, 2, or 3, depending on whether a, b, or c is the median
*/
template <class T> int medianof3(T a, T b, T c)
{
	if (a < c)
	{
		if (b < a)
			return 1;
		else if (c < b)
			return 3;
		else
			return 2;
	}
	else if (b < c)
		return 3;
	else if (a < b)
		return 1;
	else
		return 2;
}

/**
Swaps two values
Name capitalized to avoid potential naming conflicts

@param a,b  values to swap
*/
template <class T> void Swap(T& a, T& b)
{
	T t = a;
	a = b;
	b = t;
}

#endif 

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
#include "SortingHelper.h"
#include <stdlib.h>
#include <time.h>
using namespace std;

/**
Creates an array of integers 1 to n

@param n the size of the array
@return the array
*/
int* increasingArray(int n)
{
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
		arr[i] = i + 1;
	return arr;
}

/**
Creates an array of integers n down to 1

@param n the size of the array
@return the array
*/
int* decreasingArray(int n)
{
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
		arr[i] = n - i;
	return arr;
}

/**
Creates an array of n zeros

@param n the size of the array
@return the array
*/
int* zeroArray(int n)
{
	int* arr = new int[n];
	memset(arr, 0, n*sizeof(int));
	return arr;
}

/**
Creates a random permutation of the integers 1 to n

@param n the size of the array
@return the array
*/
int* randomArray(int n)
{
	srand(time(NULL));
	int* arr = increasingArray(n);
	for (int i = n; i > 1; i--)
	{
		int swap = rand() % i;
		Swap(arr[swap], arr[i - 1]);
	}

	return arr;
}


It's probably something simple that I am just not realizing, but any assistance would be greatly appreciated.
Last edited on
Code tags use [] not <>. Please edit your post and fix your code tags.
The following line numbers assume one pair of code tags.

Line 39: You're not including any C++ headers, therefore there is no std namespace. using namespace std; is invalid since there is no std namespace.

Line 78: memset requires the <cstring> header.

Line 90: This is the wrong place to be calling srand(). srand() should be called ONCE at the top of main(). If randomArray were to be called more than once in the same second, you would be resetting the RNG back to the same sequence of numbers.

Line 18,95: Where is Swap defined? If you want the swap function in the std library, it's in the <algorithm> header in C++98 and the <utility> header in C++11 and it's not capitalized.
this is not std::array<> , it is a c array / build-in array.
1
2
3
int i;
T arr[] = { *data };
size = arr.size();
I fixed the code tag issue thank you for that, hopefully now it looks clearer.

Also I included the rest of the provided code segments.
Last edited on
:)
@Ericool

So it would have to be this?

 
T arr<> = <*data>;
std::array<T*> myarray = data;
ah, simple mistake, got it, Thank you ericool
Topic archived. No new replies allowed.