-1

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

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
Many Questions
  • 125
  • 1
  • 10
  • Can you post the error you are receiving? Thanks – the happy mamba Nov 01 '15 at 04:19
  • I tried, but it is too long to post here – Many Questions Nov 01 '15 at 04:21
  • structure.cc:11:40: error: 'Box Box::operator+(Box, Box)' must take either zero or one argument structure.cc:22:44: error: 'Box Box::operator+(Box, Box)' must take either zero or one argument structure.cc: In function 'int main()': structure.cc:34:14: error: no match for 'operator+' in 'a + b' structure.cc:34:14: note: candidates are: /usr/include/c++/4.6/bits/stl_iterator.h:327:5: note: template std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const – Many Questions Nov 01 '15 at 04:21
  • Those were the first few lines, if taht helps. Sorry about that. – Many Questions Nov 01 '15 at 04:22
  • `struct` and `class` are almost identical keywords. The only two differences are default access levels for members and base classes. In a `struct`, everything is `public` by default; in a `class`, everything is `private` by default. There is not much to "learn" here. – Christian Hackl Nov 01 '15 at 10:43

2 Answers2

0

operator+ which belongs to the class/struct should receive only one parameter of type Box (from the right side of +) which should be added to the current object (from the left side of +):

Box Box::operator+(const Box& a) {
    Box box(a.l + l, a.w + w);
    return box;
}

Also in the cout line it should be b.area() and c.area() instead of (b.area) and (c.area).

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • structure.cc: In function 'int main()': structure.cc:37:12: error: no match for 'operator<<' in 'std::cout << c.Box::area' structure.cc:37:12: note: candidates are: /usr/include/c++/4.6/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream] /usr/include/c++/4.6/ostream:1 – Many Questions Nov 01 '15 at 04:45
  • @ManyQuestions Because it should be `b.area()` and `c.area ()` in the last line of `cout`. – Alex Lop. Nov 01 '15 at 04:50
  • Can't believe I didn't see that. I must be getting tired. Thanks for your help – Many Questions Nov 01 '15 at 04:55
0

Here's your code modified a bit. I had to put the operator overload into the struct due to the compiler not using NRVO (See here)

#include <iostream>
#include <string>
using namespace std;

struct Box {
    int l;
    int w;
    Box();
    Box(int a, int b);
    int area();
    Box operator+(const Box a)
    {
      return Box(a.l + l, a.w + w);
    }
};


Box::Box() {
    l = 0;
    w = 0;
}

Box::Box(int a, int b) {
    l = a;
    w = b;
}


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;
}

Result:

Total area is: 2 + 12 = 24
Community
  • 1
  • 1
the happy mamba
  • 468
  • 2
  • 6