-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

5
  • Can you post the error you are receiving? Thanks Commented Nov 1, 2015 at 4:19
  • I tried, but it is too long to post here Commented Nov 1, 2015 at 4: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<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const Commented Nov 1, 2015 at 4:21
  • Those were the first few lines, if taht helps. Sorry about that. Commented Nov 1, 2015 at 4: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. Commented Nov 1, 2015 at 10:43

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

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<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>] /usr/include/c++/4.6/ostream:1
@ManyQuestions Because it should be b.area() and c.area () in the last line of cout.
Can't believe I didn't see that. I must be getting tired. Thanks for your help
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.