I gather from this group's posts that there is no need of function
prototypes if the function is "seen" before it is called. (i.e)the
function should be defined before the main().
But in the following code i defined the function after the main() and
the code still works. Any thoughts. I am using Bloodshed C++ compiler.
#include<iostream>
using namespace std;
int main()
{
int x=10, y=20;
swap(x,y);
cout<<"x = "<<x<<" y = "<<y;
cin>>x;
return 0;
}
void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
}
Cheers
Shan 8 2516 sh*******@yahoo.com wrote in news:1121922738.228571.93040
@z14g2000cwz.googlegroups.com: I gather from this group's posts that there is no need of function prototypes if the function is "seen" before it is called. (i.e)the function should be defined before the main(). But in the following code i defined the function after the main() and the code still works. Any thoughts. I am using Bloodshed C++ compiler.
On 21 ÆßÔÂ 2005, you wrote in comp.lang.c++:
I gather from this group's posts that there is no need of function prototypes if the function is "seen" before it is called. (i.e)the function should be defined before the main(). But in the following code i defined the function after the main() and the code still works. Any thoughts. I am using Bloodshed C++ compiler.
//-----------------------------------------------------------------------
void swap() has been defined in file "stl_algobase.h".
/**
* @brief Swaps two values.
* @param a A thing of arbitrary type.
* @param b Another thing of arbitrary type.
* @return Nothing.
*
* This is the simple classic generic implementation. It will work on
* any type which has a copy constructor and an assignment operator.
*/
template<typename _Tp>
inline void
swap(_Tp& __a, _Tp& __b)
{
// concept requirements
__glibcpp_function_requires(_SGIAssignableConcept< _Tp>)
_Tp __tmp = __a;
__a = __b;
__b = __tmp;
} sh*******@yahoo.com wrote in news:1121922738.228571.93040
@z14g2000cwz.googlegroups.com: I gather from this group's posts that there is no need of function prototypes if the function is "seen" before it is called. (i.e)the function should be defined before the main(). But in the following code i defined the function after the main() and the code still works. Any thoughts. I am using Bloodshed C++ compiler.
#include<iostream> using namespace std;
int main() { int x=10, y=20; swap(x,y); cout<<"x = "<<x<<" y = "<<y; cin>>x; return 0; }
void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; }
Bad choice of name for the function to test with... there exists a
std::swap, and in your compiler, including iostream apparently brings it
in. And also you have just experienced the main drawback of "using
namespace std". Since you brought in all of the std namespace into the
global namespace, the compiler had no problem in finding std::swap<int,
int>() for you. It's not even ambiguous since it hasn't seen your swap
(int&, int&) function yet. If instead you had done:
using std::cout;
using std::cin;
the compiler would have told you that swap() doesn't exist. (Assuming
your compiler respects namespaces properly. Not all do... IIRC older
g++'s didn't.)
Most compiler allow this to keep comaptibllity with old C style
program. sh*******@yahoo.com wrote: I gather from this group's posts that there is no need of function prototypes if the function is "seen" before it is called. (i.e)the function should be defined before the main(). But in the following code i defined the function after the main() and the code still works. Any thoughts. I am using Bloodshed C++ compiler.
#include<iostream> using namespace std;
^^
remove this and see what happens.
Ian int main() { int x=10, y=20; swap(x,y); cout<<"x = "<<x<<" y = "<<y; cin>>x; return 0; }
void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; }
Cheers Shan sh*******@yahoo.com wrote: I gather from this group's posts that there is no need of function prototypes if the function is "seen" before it is called. (i.e the function should be defined before the main()). But in the following code, I defined the function after the main() and the code still works. Any thoughts. I am using Bloodshed C++ compiler.
cat main.cc
#include <iostream>
int main(int argc, char* argv[]) {
int x = 10, y = 20;
swap(x, y);
std::cout << "x = " << x << " y = " << y;
std::cin >> x;
return 0;
}
void swap(int& i, int& j) {
int tmp = i;
i = j;
j = tmp;
}
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:5: error: `swap' undeclared (first use this function)
main.cc:5: error: (Each undeclared identifier \
is reported only once for each function it appears in.)
main.cc: In function `void swap(int&, int&)':
main.cc:11: error: `void swap(int&, int&)' \
used prior to declaration
Because you are using Bloodshed C++ compiler, and the file
"stl_algobase.h" is in the folder "\include\c++\3.3.1\bits\".
U can delete void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; }
and try again.
that is a translation of originl in Portuguese
----------------------------------------------------------
This happened because you it used namespace STD mattering its symbols
for the global space of names. As the function "swap" is defined in
std, you simply it overloaded the function. It removes the line where
you use namespace STD or create a funcão that is not defined in STD
and tries to compile to see what it happens. :-)
#include<iostream>
using namespace std;
int main()
{
int x=10, y=20;
test(x,y);
cout<<"x = "<<x<<" y = "<<y;
cin>>x;
return 0;
}
void test(int& i, int& j){
i = j + i;
j = i - j;
i = i - j;
}
It notices that "test" will occur an error therefore is not defined in
SDT or possesss an prototypes.
----------------------------------------------------------------
Isto aconteceu porque você utilizou o namespace STD importando os seus
símbolos para o espaço de nomes global.
Como a função "swap" é definida em std, você simplesmente
sobrecarregou a função.
Retire a linha em que você utiliza o namespace STD ou crie uma funcão
que não esteja definida em STD e tente compilar para ver o que
acontece. :-)
Note que ocorrerá um erro pois "test" não está definido em SDT ou
possui um protótipo.
-----------
In message <11**********************@g49g2000cwa.googlegroups .com>,
upashu2 <up*****@rediffmail.com> writes Most compiler allow this
What is "this"?
to keep comaptibllity with old C style program.
Name five.
As other posters have explained, the combination of including a standard
header and a using-directive for namespace std:: has exposed std::swap.
It's nothing to do with compatibility with obsolete programs.
--
Richard Herring This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rob Somers |
last post by:
Hey people,
I read a good thread on here regarding the reason why we use function
prototypes, and it answered most of my questions, but I wanted to
double check on a couple of things, as I am...
|
by: Daniel Nichols |
last post by:
I've noticed that in a C module (.c, .h file combination) that if you
create a function's definition before it is used in other functions
than a declaration is not necessary. I believe if the...
|
by: junky_fellow |
last post by:
Can a function have two different prototypes ? If not , then how can
main() have
two different prototypes ?
int main(void)
and
int main argc(int argc, char *argv)
I mean to say, if I declare...
|
by: Robbie Hatley |
last post by:
I'm maintaining a software project with 134 C++ files, some of
them huge (as much as 10,000 lines each), and very few prototypes.
The author's attitude towards prototypes was like this:
...
|
by: wizwx |
last post by:
what does the following mean?
int func2();
According to C++, it surely means func2 is a function that takes no
argument and returns an integer. But what about in C? Does it have the
same...
|
by: Noah Roberts |
last post by:
Trying to use boost::function in a C++/CLI program. Here is code:
pragma once
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace System;
|
by: arne |
last post by:
Hi all,
imagine I call a function, but omit one of the parameters, like:
foo.c:
void foo( int a, int b ) {
/* do something with a and b */
return;
}
|
by: Steph Barklay |
last post by:
Hi, I'm currently taking a data structures course in C, and my teacher
said that function prototypes are not allowed in any of our code. He
also said that no professional programmers use function...
|
by: Ravishankar S |
last post by:
Dear C Experts,
While prepating a content for a C course,I made section on function
prototypes.
Could you kindly provide me your comments on its correctness. Thank you !
Q12: What is the...
|
by: Xiaoxiao |
last post by:
Hi,
I got a C library, is there a way to view the public function names in
this library so that I can use in my C program?
Thanks.
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |