I am trying to write a function which takes two numbers and prints out their sum.
#include <iostream>
using namespace std;
int plus(int, int);
int main () {
int a, b, result;
cout << "2 numbrs";
cin>>a>>b;
result = plus(a,b);
cout << result;
return 0;
}
int plus(int a,int b) {
int sum;
sum = a+b;
return sum;
}
and error I get:
use of `plus' is ambiguous
It´s my first C++ program and in fact I am getting blind finding an error.
Either do
result = ::plus(a,b);
Or rename the function. This is a good lesson on why using namespace std is not considered good practice.
There is already a function object in the std namespace called plus. Because of using namespace std; this std::plus is put in the global namespace, which is also where your plus() is named. When you attempt to call your plus() the compiler can't tell whether you are referring to std::plus or your plus() because they are both in the global namespace.
You have the following options:
Remove using namespace std; (you'll then need to qualify other functions in the std namespace -- e.g. std::cout).
Put your plus() in its own namespace (say, mine), and call it using mine::plus(a, b).
Call your function with ::plus() as suggested (assuming you don't put it in its own namespace).
Rename the function so that there is no name collision.
Related
#include<bits/stdc++.h>
using namespace std;
int count;
int main()
{
int k;
cin>>k;
count=k;
cout<<count;
return 0;
}
I am trying to change the value of 'count' (Global Variable) in main function but getting reference to 'count' is ambigous error in C++. But the same kind of code works well in C. Please help me.
Remove the using namespace std; line, add the std:: to cin and cout and it should be ok.
You have this compiler error because std::count exist: https://en.cppreference.com/w/cpp/algorithm/count
So it's ambigous for the compiler between std::count and your variable count because you use using namespace std.
What is the matter with this C++ code?
I get an error message saying: that there should be a ; before z, which is not correct. The only part that I don't understand is the purpose line 2 serves.
#include <iostream>
using namespace std;
int subtraction(int a, int b)
{
int r;
r=a-b;
return r;
}
int main()
{
int z;
z = subtraction (5,9);
cout z;
}
Thank you in advance.
using namespace std; means you can write cout later on rather than std::cout. It saves typing at the expense of gross namespace pollution.
Your compile error can be fixed by writing cout << z;
Also, do return a value from main.
To begin to explain what this does, it is important for you to understand what namespaces do. Namespaces are logical units of code divided up, you are able to create your own namespace or use other namespaces. The benefit of using namespaces is to have your program logically divide up your code with like functions. This is very similar to classes, but you do not need initiation of the namespaces like classes do. IN Java this would be similar to packages. To use a function within a namespace you need to use the namespace identifier followed by the function you are calling. This will call the correct function in the namespace scope you are wanting to use. An example of creating a namespace is the following:
namespace connection
{
int create_connection();
int close_connection();
//ect.......
}
Then later in the code when you want to call create_connection you need to do it the following way:
connection::create_connection();
As for your answer you are able to prevent from having to type the namespace identifier in this case connection, or in your case std. You are able to introduce an entire namespace into a section of code by using a using-directive. This will allow you to call the functions that are in that namespace without needing to use the namespace followed with the scope indicator" :: ".
The following syntax to do this is as follows:
using namespace connection:
or in your case
using namespace std;
So by doing this with std you will be granting that access to std namespace which includes C++ I/O objects cout and cin to use freely without having to use the namespace and scope operator first. Though a better practice is to limit the scope to the namespace members you want to actually use. On large programs this will be cleaner why of coding as well as avoid several problems. To introduce only specific members of a namespace, such as only introducing the std::cin and std::cout, you do the following:
using std::cin;
using std::cout;
What does using namespace std; do?
It tells the compiler which class/namespace to look in for an identifier. You either use using namespace std; in the beginning of the file or have to place it in front of each function that belongs to it.
What is the matter with this C++ code?
The syntax to use std::cout is:
std::cout << source;
variable source is inserted with the help of operator << in the std::cout stream which prints it to the standard output, i.e. computer monitor.
std "labels" a function member of the Standard Library. This is a technique used (among other reasons) to resolve (using the resolution operator ::) members that belong to the Standard Library from (possible) name conflicts with functions with similar(same) names and to reduce a scope of a search. std is called a namespace, so using namespace std; is a bit self explanatory.
I am developing a project which works with multiple arithmetic types. So I made a header, where the minimal requirements for a user defined arithmetic type are defined:
user_defined_arithmetic.h :
typedef double ArithmeticF; // The user chooses what type he
// wants to use to represent a real number
namespace arithmetic // and defines the functions related to that type
{
const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
What is troubling me is that when I use code like this:
#include "user_defined_arithmetic.h"
void some_function()
{
using namespace arithmetic;
ArithmeticF lala(3);
sin(lala);
}
I get a compiler error:
error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
I have never used the <math.h> header, only the <cmath>. I have never used the using namespace std in a header file.
I am using gcc 4.6.*. I checked what is the header containing the ambiguous declaration and it turns out to be:
mathcalls.h :
Prototype declarations for math functions; helper file for <math.h>.
...
I know, that <cmath> includes <math.h>, but it should shield the declarations by the std namespace. I dig into the <cmath> header and find:
cmath.h :
...
#include <math.h>
...
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
#undef acos
...
namespace std _GLIBCXX_VISIBILITY(default)
{
...
So the namespace std begins after the #include <math.h>. Is there something wrong here, or did I misunderstand something?
Implementations of the C++ standard library are permitted to declare C library functions in the global namespace as well as in std. Some would call this a mistake, since (as you've found) the namespace pollution can cause conflicts with your own names. However, that's the way it is, so we must live with it. You'll just have to qualify your name as arithmetic::sin.
In the words of the standard (C++11 17.6.1.2/4):
In the C++ standard library, however, the declarations (except for
names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is
unspecified whether these names are first declared within the global namespace scope and are then injected
into namespace std by explicit using-declarations (7.3.3).
If you really wanted to, you could always write a little wrapper around cmath, along the lines of:
//stdmath.cpp
#include <cmath>
namespace stdmath
{
double sin(double x)
{
return std::sin(x);
}
}
//stdmath.hpp
#ifndef STDMATH_HPP
#define STDMATH_HPP
namespace stdmath {
double sin(double);
}
#endif
//uses_stdmath.cpp
#include <iostream>
#include "stdmath.hpp"
double sin(double x)
{
return 1.0;
}
int main()
{
std::cout << stdmath::sin(1) << std::endl;
std::cout << sin(1) << std::endl;
}
I suppose there could be some overhead from the additional function call, depending on how clever the compiler is.
This is just a humble attempt to start solving this problem. (Suggestions are welcomed.)
I have been dealing with this problem a long time. A case were the problem is very obvious is this case:
#include<cmath>
#include<iostream>
namespace mylib{
std::string exp(double x){return "mylib::exp";}
}
int main(){
std::cout << std::exp(1.) << std::endl; // works
std::cout << mylib::exp(1.) << std::endl; // works
using namespace mylib;
std::cout << exp(1.) << std::endl; //doesn't works!, "ambiguous" call
return 0;
}
This is in my opinion is an annoying bug or at the least an very unfortunate situation. (At least in GCC, and clang --using GCC library-- in Linux.)
Lately I gave it another shot to the problem. By looking at the cmath (of GCC) it seems that the header is there simply to overload the C-functions and screws up the namespace in the process.
namespace std{
#include<math.h>
}
//instead of #include<cmath>
With it this works
using namespace mylib;
std::cout << exp(1.) << std::endl; //now works.
I am almost sure that this is not exactly equivalent to #include<cmath> but most functions seem to work.
Worst of all is that eventually some dependence library will eventually will #inclulde<cmath>. For that I couldn't find a solution yet.
NOTE: Needless to say this doesn't work at all
namespace std{
#include<cmath> // compile errors
}
I have a simple question for namespace in C++. There are errors when I compiling the following little piece of code. I don't understand why. Thanks for any help in advance!
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
using namespace first;
cout<<x<<endl;
}
{
using namespace second;
cout<<x<<endl;
}
cout<<x<<endl;
}
If I comment out the x declared in the global scope and the last statement. It works fine. But in my mind, the first x is declared in the std namespace and using namespace first and second in the main will be invalid after the the code block they are declared(so the namespace will be std again). So the above code should work. Where am I wrong?
But in my mind, the first x is declared in the std namespace
Your mind is wrong. The using namespace std makes names from std available in the global namespace, it doesn't mean names declared in the global namespace are in std.
x is declared in the global namespace.
so the namespace will be std again
No, nothing in your file is in namespace std, only the contents of <iostream> are in namespace std.
The error is that when you try to use x there are two different variables in scope, ::x and first::x that you could be referring to, so it is ambiguous. You can disambiguate with a using declaration instead of a using directive:
{
using first::x;
cout<<x<<endl;
}
This says that in that scope x refers to first::x
in this case x variable is ambiguous. The compiler can't find which x you are going to use. You can write like this.
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
cout<<first::x<<endl;
}
{
cout<<second::x<<endl;
}
cout<<x<<endl;
}
now your code will compile.
The first x is not in the std namespace. It would only be in the std namespace if defined like this:
namespace std
{
int x;
}
Because it's not in that namespace, it's matched by references to x later in the program, and such references become ambiguous when another namespace declaring x is also being used (as in using namespace first / second).
I am developing a project which works with multiple arithmetic types. So I made a header, where the minimal requirements for a user defined arithmetic type are defined:
user_defined_arithmetic.h :
typedef double ArithmeticF; // The user chooses what type he
// wants to use to represent a real number
namespace arithmetic // and defines the functions related to that type
{
const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
What is troubling me is that when I use code like this:
#include "user_defined_arithmetic.h"
void some_function()
{
using namespace arithmetic;
ArithmeticF lala(3);
sin(lala);
}
I get a compiler error:
error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
I have never used the <math.h> header, only the <cmath>. I have never used the using namespace std in a header file.
I am using gcc 4.6.*. I checked what is the header containing the ambiguous declaration and it turns out to be:
mathcalls.h :
Prototype declarations for math functions; helper file for <math.h>.
...
I know, that <cmath> includes <math.h>, but it should shield the declarations by the std namespace. I dig into the <cmath> header and find:
cmath.h :
...
#include <math.h>
...
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
#undef acos
...
namespace std _GLIBCXX_VISIBILITY(default)
{
...
So the namespace std begins after the #include <math.h>. Is there something wrong here, or did I misunderstand something?
Implementations of the C++ standard library are permitted to declare C library functions in the global namespace as well as in std. Some would call this a mistake, since (as you've found) the namespace pollution can cause conflicts with your own names. However, that's the way it is, so we must live with it. You'll just have to qualify your name as arithmetic::sin.
In the words of the standard (C++11 17.6.1.2/4):
In the C++ standard library, however, the declarations (except for
names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is
unspecified whether these names are first declared within the global namespace scope and are then injected
into namespace std by explicit using-declarations (7.3.3).
If you really wanted to, you could always write a little wrapper around cmath, along the lines of:
//stdmath.cpp
#include <cmath>
namespace stdmath
{
double sin(double x)
{
return std::sin(x);
}
}
//stdmath.hpp
#ifndef STDMATH_HPP
#define STDMATH_HPP
namespace stdmath {
double sin(double);
}
#endif
//uses_stdmath.cpp
#include <iostream>
#include "stdmath.hpp"
double sin(double x)
{
return 1.0;
}
int main()
{
std::cout << stdmath::sin(1) << std::endl;
std::cout << sin(1) << std::endl;
}
I suppose there could be some overhead from the additional function call, depending on how clever the compiler is.
This is just a humble attempt to start solving this problem. (Suggestions are welcomed.)
I have been dealing with this problem a long time. A case were the problem is very obvious is this case:
#include<cmath>
#include<iostream>
namespace mylib{
std::string exp(double x){return "mylib::exp";}
}
int main(){
std::cout << std::exp(1.) << std::endl; // works
std::cout << mylib::exp(1.) << std::endl; // works
using namespace mylib;
std::cout << exp(1.) << std::endl; //doesn't works!, "ambiguous" call
return 0;
}
This is in my opinion is an annoying bug or at the least an very unfortunate situation. (At least in GCC, and clang --using GCC library-- in Linux.)
Lately I gave it another shot to the problem. By looking at the cmath (of GCC) it seems that the header is there simply to overload the C-functions and screws up the namespace in the process.
namespace std{
#include<math.h>
}
//instead of #include<cmath>
With it this works
using namespace mylib;
std::cout << exp(1.) << std::endl; //now works.
I am almost sure that this is not exactly equivalent to #include<cmath> but most functions seem to work.
Worst of all is that eventually some dependence library will eventually will #inclulde<cmath>. For that I couldn't find a solution yet.
NOTE: Needless to say this doesn't work at all
namespace std{
#include<cmath> // compile errors
}