473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overloading the + operator

Hello,

I have a short matrix class I am trying to set up for a small project. I would like to overload the + operator to simplify the code syntax. However I am running into a compilation issue that I am not familiar with. Here is a snippet of the relevant code:

Expand|Select|Wrap|Line Numbers
  1. // File Matrix.h
  2.  
  3. class Matrix
  4.     {
  5.     public:
  6.         Matrix(int Rows);
  7.         Matrix(int Rows, int Columns);
  8.         ~Matrix();
  9.  
  10.          double &operator() (int Row, int Column)
  11.         {
  12.            if(Row < 0 || Column < 0 || Row > mRows || Column > mColumns)
  13.            {
  14.               // Out of range error
  15.            }
  16.  
  17.            return(mData[(mColumns * Row) + Column]);
  18.         }
  19.  
  20.         Matrix operator + (Matrix &B)
  21.         {
  22.            Matrix *A = this;
  23.            if((A->GetRowCount() != B.GetRowCount()) ||
  24.               (A->GetColCount() != B.GetColCount()))
  25.            {
  26.               // Matrices do not have the same dimensions
  27.               return(*A);
  28.            }
  29.  
  30.            Matrix C(A->GetRowCount(), A->GetColCount());
  31.            for(int i = 0; i < A->GetRowCount(); ++i)
  32.            {
  33.               for(int j = 0; j < A->GetColCount(); ++j)
  34.               {
  35.                  C(i, j) = A(i, j) + B(i, j); // This is line 78 as referenced by the error below
  36.               }
  37.            }
  38.            return(C);
  39.         }
  40.  
  41.      private:
  42.         int mRows;
  43.         int mColumns;
  44.         double *mData;
  45.    }
  46.  
  47.  
And here is the error I am receiving from the compiler:

Matrix.h: In member function `FlightSim::MatrixFlightSim::Matrix::operator+(Fli ghtSim::Matrix&)':
Matrix.h:78: `A' cannot be used as a function
make: *** [Matrix.o] Error 1

I should mention that I am fairly new at C++ so I recognize my inexperience is sure to be a factor here.

Thanks!
Mar 17 '08 #1
3 1318
Ganon11
3,652 Expert 2GB
A is a pointer to a Matrix, B is a Matrix, so it should be evident you need to treat them differently. You have been in the rest of the function, but here you write both A(i, j) and B(i, j). You may have to use A->(i, j) or some similar syntax, I'm not sure which.
Mar 17 '08 #2
scruggsy
147 100+
A is a pointer to a Matrix, B is a Matrix, so it should be evident you need to treat them differently. You have been in the rest of the function, but here you write both A(i, j) and B(i, j). You may have to use A->(i, j) or some similar syntax, I'm not sure which.
You need to dereference A to get at the object it points to:
Expand|Select|Wrap|Line Numbers
  1. (*A)(i, j)
Your compiler will probably reject A->(i, j).
If you want to make your code more readable, I suggest:
Expand|Select|Wrap|Line Numbers
  1. Matrix A = *this;
Mar 18 '08 #3
You need to dereference A to get at the object it points to:
Expand|Select|Wrap|Line Numbers
  1. (*A)(i, j)
Your compiler will probably reject A->(i, j).
If you want to make your code more readable, I suggest:
Expand|Select|Wrap|Line Numbers
  1. Matrix A = *this;
Thanks scruggsy. I certainly had tried A->(i, j) and as you mentioned the compiler did not like it either. Anyhow, I like the suggestion to dereference "this" so that's what I tried and it works like a charm.
Mar 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.