I didn't understand why i take this "strange" error. I read similar questions but it didn't answer my questions. If i define the array inside main function rather than global scope, there is no error. But assume that i have to define this array in global scope. Why do i take this error?
Here is the code :
#include <iostream>
#include <cstring>
using namespace std;
int right[1005];
int main()
{
memset(right,0,sizeof(right));
return 0;
}
Here is the error :
memset2.cpp: In function ‘int main()’:
memset2.cpp:9:9: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
memset2.cpp:9:24: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
Namespace std has already name right and you included names form std in the global namespace by means of directive
using namespace std;
So to avoid the ambiguity use a qualified name
memset( ::right, 0, sizeof( ::right ) );
Or remove the directive and in this case you may use unqualified name right because the compiler will seek the name only in the global namespace.
remove using namespace std ; from your code and precede any standard function or object with std::
Related
I wrote a simple hello world program:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Hello";
}
But it is reporting an error:
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:64,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\sstream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\complex:45,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ccomplex:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\mingw32\bits\stdc++.h:54,
from AAY.cpp:1:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:214:11: error: expected unqualified-id before numeric constant
214 | _T1 7
| ^
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h: In constructor 'constexpr std::pair<_T1, _T2>::pair()':
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:245:9: error: class 'std::pair<_T1, _T2>' does not have any field named 'first'
245 | : first(), second() { }
and so on.
Basically, it says that std::pair does not have a member named first although I haven't used any. Does anyone have a solution?
You should not use
#include<bits/stdc++.h>
This is not a C++ compliant header file.
Please use #include <iostream> instead and it should be sufficient.
Of course you may have also a problem with your compiler installation.
When i compile my c++ code using g++ 5.1.1 it says
"narc05b.cpp: In function ‘int main()’:
anarc05b.cpp:5:3: error: ‘cout’ was not declared in this scope
cout<<"hello\n"; ^ anarc05b.cpp:5:3: note: suggested alternative:
In file included from anarc05b.cpp:1:0:
/usr/include/c++/5.1.1/iostream:61:18: note: ‘std::cout’ extern
ostream cout; /// Linked to standard output "
...what does it mean ?
Use should use the namespace directive in the code
using namespace std;
or alternatively prefix cout with std::cout
I wrote the following code after using "gedit take_input.cpp":
#include <iostream>
int main()
{
cout<<"please enter your name (followed by 'enter')\n";
string file;
cin >> file;
cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}
However, when I used "g++" to convert my human-readable code into object code (writing g++ take_input.cpp -o take_input), the terminal returns with a result similar to this:
take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
cout<<"please enter your name (followed by 'enter')\n";
^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
string file;
^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
from /usr/include/c++/4.9/ios:38,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
cin >> file;
^ ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
^
Could you tell me what the reason is?
The errors that you are getting are because the cout is not in the global namespace rather it is in std namespace.
Well instead of writing
using namespace std;
after #include <iostream> try using:
using std::cout;
since using first option is a bad practice. You can refer to Why is using namespace std is a bad practice.
For benefits of using using std::cout refer Using std namespace
Also you can use std::cout everywhere if you don't want to use using std::cout.
Just read the error messages that you compiler gave you. The problem is that
‘cout’ was not declared in this scope
And the "suggested alternative" is std::cout. Same goes for string vs. std::string.
Note that generally, things belonging to the standard library need to be qualified with std:: to be found.
You also need to #include <string> to use std::string btw.
The compiler gives you the answer on line 7: Since you're not using the std namespace, you have to prepend std:: to your cout and cin calls.
Just add
using namespace std;
after #include <iostream>
Try this out.
Can someone please explain below output:
#include <iostream>
using namespace std;
namespace A{
int x=1;
int z=2;
}
namespace B{
int y=3;
int z=4;
}
void doSomethingWith(int i) throw()
{
cout << i ;
}
void sample() throw()
{
using namespace A;
using namespace B;
doSomethingWith(x);
doSomethingWith(y);
doSomethingWith(z);
}
int main ()
{
sample();
return 0;
}
Output:
$ g++ -Wall TestCPP.cpp -o TestCPP
TestCPP.cpp: In function `void sample()':
TestCPP.cpp:26: error: `z' undeclared (first use this function)
TestCPP.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
I have another error:
error: reference to 'z' is ambiguous
Which is pretty clear for me: z exists in both namespaces, and compiler don't know, which one should be used. Do you know? Resolve it by specifying namespace, for example:
doSomethingWith(A::z);
using keyword is used to
shortcut the names so you do not need to type things like std::cout
to typedef with templates(c++11), i.e. template<typename T> using VT = std::vector<T>;
In your situation, namespace is used to prevent name pollution, which means two functions/variables accidently shared the same name. If you use the two using together, this will led to ambiguous z. My g++ 4.8.1 reported the error:
abc.cpp: In function ‘void sample()’:
abc.cpp:26:21: error: reference to ‘z’ is ambiguous
doSomethingWith(z);
^
abc.cpp:12:5: note: candidates are: int B::z
int z=4;
^
abc.cpp:7:5: note: int A::z
int z=2;
^
which is expected. I am unsure which gnu compiler you are using, but this is an predictable error.
You get a suboptimal message. A better implementation would still flag error, but say 'z is ambiguous' as that is the problem rather than 'undeclared'.
At the point name z hits multiple things: A::z and B::z, and the rule is that the implementation must not just pick one of them. You must use qualification to resolve the issue.
i have following code
#include <iostream>
#include <set>
#include <string>
using namespace std;
template<class Container>
void print(const Container &c)
{
Container::const_iterator itr;
for (itr=c.begin();itr!=c.end();itr++){
cout<<*itr<< '\n';
}
}
int main(){
set<string,greater<string>>s;
s.insert("georgia");
s.insert("saqartvelo");
print(s);
return 0;
}
but errors are
reverse.cpp: In function ‘void print(const Container&)’:
reverse.cpp:9: error: expected ‘;’ before ‘itr’
reverse.cpp:10: error: ‘itr’ was not declared in this scope
reverse.cpp: In function ‘int main()’:
reverse.cpp:17: error: ‘s’ was not declared in this scope
reverse.cpp:17: error: ‘>>’ should be ‘> >’ within a nested template argument list
What might cause this and how do I solve it?
You need typename Container::const_iterator instead of Container::const_iterator.
At the point the compiler is reading your code, it doesn't know that Container has such a type (it is a so-called dependent name).
Alexandre is right about the first two errors. The last two are due to an annoying syntax limitation of C++: you need to have a space in between the two closing brackets in the template expression:
set<string,greater<string> > s;
Otherwise, C++ interprets it as the right shift >> operator.