array, classes struct and dynamic alloc not getting the layers?


How does array and struct classes work with dynamic alloc?
I do not want to use vectors yet.. just dynamic alloc with 3 simple arrays.. this is useful for reciepts.. and sorts..Am trying to test this for 3 simple data.. could replace it with something else this one is easy to trace.. but I admit this one is getting a little bit layered.. it will give me

1
2
3
4
5
C4183: 'setLength': member function definition looks like a ctor, but name 

does not match enclosing class error C2228: left of '.setWidth' must have 

class/struct/union type

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
#include<iostream>
using namespace std; 

class Yard
{
private: 
	int length, width;
public:
	Yard()
	   {length =0; width=0;}
	setLength(int len)
		{length=len;}
    setWidth(int w)
		{width=w;}
	};


	int main()

	{
		 int yrd_size,yardlength,yardwidth;
		 int *yrd_count;
		 cout<<"How many yards?  ";
		 cin>>yrd_size;
		
	     yrd_count=new int[yrd_size];

		 for(int i=0; i<yrd_size; i++)
		 {
		     cout<<"Yard "<<(i+1)<<":"<<endl;
			 
			 cout<<"Yard length "<<(i+1)<<":"<<endl;
	          cin>>yrd_count[i].setLength();
		   
			 cout<<"Yard width "<<(i+1)<<":"<<endl;
    	      cin>>yrd_count[i].setWidth();
			 
			  cout<<endl;
		
		}

	return 0;
	}
	



You need to specify return types for your methods (setHeight and setWidth). In this case they don't need to return anything so the return type is void. Also if you want to actually access the height and width of each Yard, you will need to write accessor functions (e.g. getHeight and getWidth). Remember that these need to have a return type of int.

1
2
3
4
5
6
7
8
void setLength(int len)
{
    length=len;
}
void setWidth(int w)
{
    width=w;
}
thanks will work on asap
Topic archived. No new replies allowed.