Hi,
I came acrosss g++ compile errors whenever I make a function call by
reference and found out from the test program that compiler is
treating the function argument differently when another function call
funcRet()is made which returns the expected argument type for the
function call by reference funcByRef(class A&);
The only way to get around this probelm is to first call the
funcRet(), assign its value to a variable and pass that variable to
the function by reference.
But I have these thousands of places where such calls were made and
thought there would be an easier solution to this rather than going to
each and every single place and split the function call in to two
statements ( class var=funcRet();
funcByRef(var);
For example, see the below test program on Linux with g++ version
3.2.2.
This program just demonstrates the compiler problem but doesn't
reflect the actual function calls in the product that I am working on.
1 #include<iostre am>
2
3 using std::cout;
4 using std::cin;
5 using std::endl;
6
7 class ref {
8 public:
9 void getNumber();
10 ref getObj();
11 void neg(ref& Obj);
12
13 //private:
14 int i;
15 };
16
17
18 void ref::getNumber( ){
19 cout<<"Please enter a number "<<endl;
20 cin>>this->i;
21 cout<<"Assigned value is "<<this->i<<endl;
22 }
23
24 ref ref::getObj(){
25 return (*this);
26 }
27
28 void ref::neg(ref& Obj){
29
30 Obj.i=-Obj.i;
31 cout<<"In the neg func by reference "<<Obj.i<<e ndl;
32
33 }
34
35
36 //#define refObj.neg(x) \
37 //{ \
38 //ref tmpObj; refObj.neg((tmp Obj=x)); \
39 //}
40
41 int main() {
42
43 ref refObj;
44
45 refObj.getNumbe r();
46
47 //refObj.neg(tmpO bj=refObj.getOb j());
48 refObj.neg(refO bj.getObj());
49
50 ref tmpObj=refObj.g etObj();
51 refObj.neg(tmpO bj);
52
53 cout<<"neg of the number in tmpObj is "<<tmpObj.i<<en dl;
54 cout<<"neg of the number in refObj is "<<refObj.i<<en dl;
55
56 return 0;
57
58 }
When I compile, I get the below compile errors.
ref.cxx: In function `int main()':
ref.cxx:48: no matching function for call to `ref::neg(ref)'
ref.cxx:28: candidates are: void ref::neg(ref&)
You can see that if I call the neg() function with another function
call as as argument, copiler is giving mismatch, but If I split it up
(line 50 and 51), it works fine.
Please let me know any thoughts on this....easier way to get around
this problem as I have 1000's of such function calls in a product
which I am porting from windows to Linux. I wouldn't want to go to
each and every place and split it up into two statements.
Your help is highly appreciated.
Thanks,
Gary. 4 3395
<ma*****@ca.com > wrote in message
news:5a******** *************** ***@posting.goo gle.com... Hi,
I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another function call funcRet()is made which returns the expected argument type for the function call by reference funcByRef(class A&);
You cannot bind a temporary to a non-const reference. It's a rule of C++.
Function return values are kind of temporary.
If you can use a const reference instead
void funcByRef(const A&);
but this might mean even more work than your solution, or may not even be
possible. It depends on the function.
The only way to get around this probelm is to first call the funcRet(), assign its value to a variable and pass that variable to the function by reference.
Not the only way, a const reference would be the preferred solution.
But I have these thousands of places where such calls were made and thought there would be an easier solution to this rather than going to each and every single place and split the function call in to two statements ( class var=funcRet(); funcByRef(var);
[snip]
Please let me know any thoughts on this....easier way to get around this problem as I have 1000's of such function calls in a product which I am porting from windows to Linux. I wouldn't want to go to each and every place and split it up into two statements.
The Microsoft Visual C++ compiler gets this wrong. No doubt they thought
they were being helpful.
john
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2f******** ****@uni-berlin.de... The Microsoft Visual C++ compiler gets this wrong. No doubt they thought they were being helpful.
Well it cribs if you disable the language extensions (/Za compiler option).
It's a language extension by MS. ma*****@ca.com wrote:
[reformatted annoying line numbering & indentation] class ref { public: void getNumber(); ref getObj(); void neg(ref& Obj); int i; };
void ref::getNumber( ) { cout<<"Please enter a number "<<endl; cin>>this->i; cout<<"Assigned value is "<<this->i<<endl; }
ref ref::getObj() { return (*this); }
void ref::neg(ref& Obj) { Obj.i=-Obj.i; cout<<"In the neg func by reference "<<Obj.i<<e ndl; }
This is rather bad style: the function does not make any use of
the object. Consider either making it static, or having a free
function:
void neg(ref& Obj)
int main() { ref refObj;
refObj.getNumbe r(); refObj.neg(refO bj.getObj());
ref tmpObj=refObj.g etObj(); refObj.neg(tmpO bj);
cout<<"neg of the number in tmpObj is "<<tmpObj.i<<en dl; cout<<"neg of the number in refObj is "<<refObj.i<<en dl;
return 0; }
You can see that if I call the neg() function with another function call as as argument, copiler is giving mismatch, but If I split it up (line 50 and 51), it works fine. Please let me know any thoughts on this....easier way to get around this problem as I have 1000's of such function calls in a product which I am porting from windows to Linux. I wouldn't want to go to each and every place and split it up into two statements.
Your code is a great example of why you can't bind a temporary to a
non-const reference! You should have seen in the output that tmpObj.i
was negative, whereas refObj.i was positive. The code:
refObj.neg(refO bj.getObj());
has no effect on refObj (a copy of refObj is passed to neg(), not
the actual refObj, and then the copy is destroyed as soon as it has
been negated). The reason that this rule was added to the language
was precisely to catch this sort of error. If you do want to forge
ahead with this useless code, then you have to explicitly name the
temporary object.
"John Harrison" wrote*: You cannot bind a temporary to a non-const reference. It's a rule of C++.
What need is there for such a rule*? I understand why a warning should
be issued, since this usually shows that the programmer is not aware of
what is happening. But it may still be convenient in some cases, and I
cannot see what accepting this type of code would break.
Anyway, Sun's jvm developpers should stop using it... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ASallade |
last post by:
Hello,
I've scoured my books and the web, but am still daunted, hopefully some of
the users in this newsgroup will have advice for my problem.
I am not an experienced javascript programmer, but have gotten my code to
work well, without errors on IE, Opera and netscapes recent builds. While
testing, I found that it doesnt execute on Netscape 4.7
In the head I have a script that creates a custom object/class for products,
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object must be an object
instead of
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
|
by: Peter Proost |
last post by:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:
System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo
culture, Boolean...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
| |
by: Robert Dailey |
last post by:
Hi,
I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the function is
also modified.
Sometimes I wish to work with "copies", in that when I pass in an
integer variable into a function, I want the function to be modifying
a COPY, not the reference. Is this possible?
|
by: =?Utf-8?B?dmlzaHJ1dGg=?= |
last post by:
Hi,
I have 2 applications running, one Windows application project and the other
windows services project.
I want to call my Windows application in my windows services.
I want to run them as seperate process.
If my windows application starts running,only if it completes fully,
then my windows services should continue its execution.
My main process is Windows service.
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables.
Only very few global functions are allowed to be reusability for the
programmers to use. Few...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I'm sure, but just didn't think this was my
problem; LOL, I am needling him)
If...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |