### First Program Using Classes

Hey everyone,

I'm a bit confused as to how to use classes and arrays together, and then calling functions to use on them, as this is my first experience with classes. Please help me in fundamental things I may be doing wrong from the start. Thanks.

Here is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180`` `````` #include #include using namespace std; class Box { public: Box(); int getDepth(int x[4],int y[4]); int getMaxX(int x[4]); int getMaxY(int y[4]); int getMinX(int x[4]); int getMinY(int y[4]); string getType(string type, int x[4], int y[4]){return type;} int getVolume(int width, int height, int depth); int getWidth(int x[4], int y[4]); void setData(int x[4], int y[4], int z[4]); private: int width; int depth; int height; int Volume; string type, temp; }; Box::Box() { width = 5; // initialize the data members of the class object here depth = 14; height = 1; } int Box::getDepth(int x[4], int y[4]) { for (int i=0;i<4;i++) { while (x[i+1]-x[i]==0) //While x values remain constant, y values will determine depth of box { depth = y[i+1]-y[i]; } } } int Box::getMaxX(int x[4]) // Highest Value of X from the 4 X and Y pairs { for (int i=0;i<4;i++) { for (int j = 0; j<3;j++) { temp = maxX[j]; maxX[j]=maxX[j+1]; maxX[j+1] = temp; } } } int Box::getMaxY(int y[4]) // Highest Value of YX from the 4 X and Y pairs { for (int i=0;i<4;i++) { for (int j = 0; j<3;j++) { temp = maxY[j]; maxY[j]=maxY[j+1]; maxY[j+1] = temp; } } } int Box::getMinX(int x[4]) // Lowest Value of X from the 4 X and Y pairs { for (int i=0;i<4;i++) { for (int j = 0; j<3;j++) { while (x[j]>=x[j+1]) { temp = minX[j]; minX[j]=minX[j+1]; minX[j+1] = temp; } } } } int Box::getMinY(int y[4],minY[]) //Lowest Value of Y the 4 X and Y pairs { for (int i=0;i<4;i++) { for (int j=0; j<3; j++) { while (y[j]>=y[j+1]) { temp = minY[j]; minY[j]=minY[j+1]; minY[j+1] = temp; } } } } string Box::getType(string type, int x[4],int y[4]) //If sides are equal, Cube, otherwise, Rectangle { for (int i=0;i<4;i++) { if (depth == width) { type = "Cube"; } else type = "Rectangle"; } } int Box::getVolume(int width, int height, int depth) { Volume = width * height * depth; } int Box::getWidth(int x[4], int y[4]) { for (int i=0;i<4;i++) { while (y[i+1]-y[i]==0) //While y values remain constant, x values will determine width of box { width = x[i+1]-x[i]; } } } void Box::setData(int x[4], int y[4], int z[4]) { for (int i=0;i<4;i++) //taking 4 sets of x and y inputs for coodinates and storing to x[i],y[i],z[i] for object r { cout<<"Enter x and y separated by spaces: " <>x[]>>y[]; cout<<"Enter z: "<>z[]; } } int main() { Box r([x[4],y[4],z[1]); // declaring array of x and y coordinates of object r { for (i = 0; i<4; i++) { width = x[i]; depth = y[i]; height = z[i]; } } cout<<"Width of r: " << r.getWidth()<

 Errors: 1>------ Rebuild All started: Project: aasdfs, Configuration: Debug Win32 ------ 1> asdf.cpp 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(61): error C2065: 'maxX' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(62): error C2065: 'maxX' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(63): error C2065: 'maxX' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(75): error C2065: 'maxY' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(76): error C2065: 'maxY' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(77): error C2065: 'maxY' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(90): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(91): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(92): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(106): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(107): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(108): error C2109: subscript requires array or pointer type 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(115): error C2084: function 'std::string Box::getType(std::string,int [],int [])' already has a body 1> c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(21) : see previous definition of 'getType' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(148): error C2059: syntax error : ']' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(150): error C2059: syntax error : ']' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2337: 'x' : attribute not found 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2143: syntax error : missing ']' before '[' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C3409: empty attribute block is not allowed 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2143: syntax error : missing ']' before 'constant' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(156): error C2059: syntax error : 'constant' 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(158): error C2065: 'i' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'width' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'x' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(160): error C2065: 'i' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'depth' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'y' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(161): error C2065: 'i' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'height' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'z' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(162): error C2065: 'i' : undeclared identifier 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(166): error C2228: left of '.getWidth' must have class/struct/union 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(167): error C2228: left of '.getDepth' must have class/struct/union 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(168): error C2228: left of '.getType' must have class/struct/union 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(169): error C2228: left of '.set_width' must have class/struct/union 1>c:\users\frederic\desktop\c++ fall 2012\homework\homework 2\aasdfs\aasdfs\asdf.cpp(170): error C2228: left of '.set_depth' must have class/struct/union ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Can you guys please point out the most important flaws in logic/syntax of mine? Thanks..
Most of these errors are a result of undeclared identifiers and you methods not return a value.

 ``123456789101112`` ``````int Box::getMaxY(int y[4]) // Highest Value of YX from the 4 X and Y pairs { for (int i=0;i<4;i++) { for (int j = 0; j<3;j++) { temp = maxY[j]; maxY[j]=maxY[j+1]; maxY[j+1] = temp; } } }``````

You are not returning an integer and maxY and temp are both undeclared. The same applies to most of the methods defined inside of your class.

EDIT: Who's teaching you C++?
Last edited on
I know it may be homework, but if you don't understand classes maybe you should start with something a bit smaller, if you can get a small class to compile you will find it easier to get a bigger class to compile.
Im just working on the assignment really early, its not due for at least another week, were supposed to ask questions about it in class on wednesday after getting stumped by it on the weekend. I was just hoping I could get a leg up on my fellow classmates by coming in with some extra knowledge/experience =))
Topic archived. No new replies allowed.