473,395 Members | 1,694 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,395 software developers and data experts.

C++: Operator overload syntax error

Hi,

I am attempting to overload the '<' operator in order to sort an array structure, but get a syntax error in Borland Builder5.
Code as follows:

Expand|Select|Wrap|Line Numbers
  1. struct GroupTeamType
  2. {
  3.    AnsiString Name;
  4.    int Played;
  5.    int Won;
  6.    int Drawn;
  7.    int Lost;
  8.    int GoalsFor;
  9.    int GoalsAgainst;
  10.    int GoalDiff;
  11.    int Points;
  12. };
  13.  
  14. GroupTeamType Group[4];
  15.  
  16. // This line gives 'Declaration syntax error
  17. bool operator <(const GroupTeamType &first, const GroupTeamType &second)
  18. {
  19.    return first.Points < second.Points;
  20. }
As far as I can see, this syntax matches several examples I have found on the Web. Any idea what I'm doing wrong?

Thanks
Doug
Feb 8 '06 #1
2 5305
Unfortunately, the operator overloading function has to defined in a class.
The calling object then uses it like any other member function.
e.g.When u say a<b where a and b are objects, the operator function of class of object 'a' is called and 'b' becomes the parameter for that function.
Nov 16 '06 #2
Banfa
9,065 Expert Mod 8TB
Unfortunately, the operator overloading function has to defined in a class.
This statement is not true you can overload an operator using a global function like DouggieC has tried to.


DouggieC

the only problem I had compiling your code is that the type AnsiString was not defined on my system, when a changed the definition of GroupTeamType to use a string (from the STL) instead of AnsiString it compiled without error.

If you have code that produces errors and you post asking for help with it then it is best to post the errors as well and indicate which lines of code they occured on.
Nov 16 '06 #3

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

Similar topics

5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
3
by: Rama | last post by:
wht is operator overloading, how you overload an operator
1
by: sd2004 | last post by:
Could someone please help ? I am getting compiling error but do not understand why ? below is snipset of code: istream& operator>>(istream& is, test_ins& s){ is >>s.pass>>s.put>>s.type; ...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: Fei Liu | last post by:
Hi, I have a interesting problem here, class absOP{ template<class T> T operator(T val) { return val < 0 ? -val : val; } }; Now the problem is I can't seem to use this overloaded operator, ...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
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,...
14
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.