Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing by Reference

82 views
Skip to first unread message

Neb

unread,
Apr 16, 2012, 10:04:53 AM4/16/12
to
Hi all,

Im new to c/c++ programming and am having difficulty in passing more
than one parameter into and out of a function.

//PASS BY REFERENCE
#include <stdio.h>
void swapnum(int &i,int &j)
{
int temp = i;
i = j;
j = temp;
return;
}

int main()
{
int a = 10;
int b = 20;

swapnum(a, b);
printf("A is %d and B is %d\n", a, b);
return (0);
}

In compiling I am recieving the error:

Error E2293 test.ccp 4: ) expected


Thanks in advance, any help is greatly appreciated.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James K. Lowden

unread,
Apr 16, 2012, 11:09:51 AM4/16/12
to
On Mon, 16 Apr 2012 07:04:53 -0700 (PDT)
Neb <nu...@nowhere.com> wrote:

> In compiling I am recieving the error:
>
> Error E2293 test.ccp 4: ) expected

You're invoking the C compiler, not the C++ compiler, perhaps because
the extension you're using is ".ccp", not the conventional ".cpp".

As a beginner, I suggest you not call any program "test", only to save
yourself some confusion and frustration. On systems other than
Windows, there is usually a program called "test", often /bin/test.
Unless you're careful, you'll invoke that test instead of yours.

Besides, you'll have a lot of tests. Make a test directory, call it
"t", and put your program in it. This one might be called
"t/swap.cpp".

And use <iostream>, not <stdio.h>. It's typesafe, and it's fun. :-)

HTH.

--jkl

Ulrich Eckhardt

unread,
Apr 16, 2012, 6:07:21 PM4/16/12
to
Am 16.04.2012 16:04, schrieb Neb:
> Im new to c/c++ programming and am having difficulty in passing more
> than one parameter into and out of a function.

Note up front: Since this is a moderated group, you have a rather high
latency until your posting shows up. For complex questions, this doesn't
matter, but for short questions I'd either suggest the dedicated
beginners' group (comp.lang.learn.c-c++) or one of the C++ channels on
IRC, where you might get help in real time.


> #include <stdio.h>

If any book starts with teaching printf(), it is probably a bad book for
learning C++, provided you want to learn C++ and not C. Note that it
isn't clear which of those two _distinct_ languages you want to learn.


> void swapnum(int &i,int &j)
> {
> int temp = i;
> i = j;
> j = temp;
> return;
> }

Note that in <algorithm> (or was it <utility>?) is a function called
std::swap() which does exactly the above.


> Error E2293 test.ccp 4: ) expected

I haven't tried it but the code is C++ and seems actually okay. However,
I think you are compiling it with a C compiler, is that possible? The C
compiler then chokes on the references, which only C++ supports. Also
note that typically, C++ source files are called "cpp" and not "ccp".

Good luck!

Uli

Neb

unread,
Apr 17, 2012, 2:52:49 AM4/17/12
to
{ Please restrict your quoting to the minimum needed to
establish context -mod/we }

On 16/04/2012 10:04 PM, Neb wrote:
> Hi all,
>
> Im new to c/c++ programming and am having difficulty in passing more
> than one parameter into and out of a function.
>
> //PASS BY REFERENCE
> #include <stdio.h>
> void swapnum(int &i,int &j)
> {
> int temp = i;
> i = j;
> j = temp;
> return;
> }
>
> int main()
> {
> int a = 10;
> int b = 20;
>
> swapnum(a, b);
> printf("A is %d and B is %d\n", a, b);
> return (0);
> }
>
> In compiling I am recieving the error:
>
> Error E2293 test.ccp 4: ) expected
>
>
> Thanks in advance, any help is greatly appreciated.
>
>


Thanks for the advice fellas.

The issue was with the filetype, should have been .cpp vs .ccp.
0 new messages