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: -
// File Matrix.h
-
-
class Matrix
-
{
-
public:
-
Matrix(int Rows);
-
Matrix(int Rows, int Columns);
-
~Matrix();
-
-
double &operator() (int Row, int Column)
-
{
-
if(Row < 0 || Column < 0 || Row > mRows || Column > mColumns)
-
{
-
// Out of range error
-
}
-
-
return(mData[(mColumns * Row) + Column]);
-
}
-
-
Matrix operator + (Matrix &B)
-
{
-
Matrix *A = this;
-
if((A->GetRowCount() != B.GetRowCount()) ||
-
(A->GetColCount() != B.GetColCount()))
-
{
-
// Matrices do not have the same dimensions
-
return(*A);
-
}
-
-
Matrix C(A->GetRowCount(), A->GetColCount());
-
for(int i = 0; i < A->GetRowCount(); ++i)
-
{
-
for(int j = 0; j < A->GetColCount(); ++j)
-
{
-
C(i, j) = A(i, j) + B(i, j); // This is line 78 as referenced by the error below
-
}
-
}
-
return(C);
-
}
-
-
private:
-
int mRows;
-
int mColumns;
-
double *mData;
-
}
-
-
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!
3 1318
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.
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:
Your compiler will probably reject A->(i, j).
If you want to make your code more readable, I suggest:
You need to dereference A to get at the object it points to:
Your compiler will probably reject A->(i, j).
If you want to make your code more readable, I suggest:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 )
|
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...
|
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: (), ,...
|
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,...
|
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...
|
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 &...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |