I get a weird error when I try and compile the following code: I need to use structs (I was taught classes with the struct keywor, and am trying to learn it that way. I also need to put the function definitions outside the struct block.
#include <iostream> #include <string> using namespace std; struct Box { int l; int w; int area(); Box(); Box(int a, int b); Box operator+(const Box a, const Box b); }; Box::Box() { l = 0; w = 0; } Box::Box(int a, int b) { l = a; w = b; } Box Box::operator+(const Box a, const Box b) { Box box(a.l + b.l, a.w + b.w); return box; } int Box::area() { return l * w; } int main() { Box a(1, 2); Box b; b.l = 3; b.w = 4; Box c = a + b; cout << "Total area is: " << a.area() << " + " << (b.area) << " = " << (c.area) << endl; } Could someone help me out? Thanks
structandclassare almost identical keywords. The only two differences are default access levels for members and base classes. In astruct, everything ispublicby default; in aclass, everything isprivateby default. There is not much to "learn" here.