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.
Related
Upgrading from boost 1.76 to 1.79 caused a regression in some code I have been working on. This regression comes in the following build error:
In file included from .../boost/json/value.hpp:20,
from <list of includes>
from .../a.cpp
.../boost/json/string.hpp: In member function ‘int boost::json::string::compare(boost::json::string_view) const’:
.../boost/json/string.hpp:1948: error: call of overloaded ‘basic_string_view(const boost::json::string&)’ is ambiguous
return string_view(*this).compare(sv);
In file included from .../boost/json/string_view.hpp:14,
from <lsit of includes>
from .../a.cpp
.../boost/core/detail/string_view.hpp:391: note: candidate: ‘boost::core::basic_string_view<Ch>::basic_string_view(const std::basic_string_view<Ch, std::char_traits<_CharT> >&) [with Ch = char]’
basic_string_view( std::basic_string_view<Ch, std::char_traits<Ch> > const& str ) BOOST_NOEXCEPT: p_( str.data() ), n_( str.size() )
.../boost/core/detail/string_view.hpp:335: note: candidate: ‘constexpr boost::core::basic_string_view<char>::basic_string_view(const boost::core::basic_string_view<char>&)’
template<class Ch> class basic_string_view
.../boost/core/detail/string_view.hpp:335: note: candidate: ‘constexpr boost::core::basic_string_view<char>::basic_string_view(boost::core::basic_string_view<char>&&)’
Here is some example code that I reproduced this with: (GCC 8.2.1, std=c++17)
#include <string>
#include <boost/json.hpp>
#include <boost/json/src.hpp>
int main(int argc, char** argv)
{
return 0;
}
Note it builds with std=c++11 or c++14, however c++17 is a requirment of this project. This error only occurs on linux with GCC. This code builds on Windows with MSVC.
Are there any possible workarounds?
I have tried a couple different tricks using "using" and different namespaces but with the templating it's hard for me to to figure out what is happeneing enough to fix it. I hope I can get this working without changing JSON libraries, or with some other workaround.
Note this build error does not exist with boost 1.76.
Here's my code:
main.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << pow(4, 3);
cin.get();
}
So basically I try to compile with command g++ main.cpp -std=c++98
Here's what I get:
C:\Users\NullPointerException\Desktop\cpp>g++ main.cpp -std=c++98
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from main.cpp:1:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\cwchar:166:11: error: '::vfwscanf' has not been declared
166 | using ::vfwscanf;
| ^~~~~~~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\cwchar:172:11: error: '::vswscanf' has not been declared
172 | using ::vswscanf;
| ^~~~~~~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\cwchar:176:11: error: '::vwscanf' has not been declared
176 | using ::vwscanf;
| ^~~~~~~
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\cwchar:193:11: error: '::wcstof' has not been declared
193 | using ::wcstof;
| ^~~~~~
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_facets.h:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_ios.h:37,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:44,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from main.cpp:1:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\cwctype:89:11: error: '::iswblank' has not been declared
89 | using ::iswblank;
| ^~~~~~~~
Basically I wanted to test if c++ can deduce type for pow. But I get these errors. What's the problem and how do I fix it?
g++ version: 9.2.0
I can compile this without problems using g++ 9.3 on ubuntu. Seems you have run into a bug specific to the mingw32 variant of g++.
You might try adding include <cstdio> and perhaps also include <cstdarg> before the other includes.
I cannot test this since I'm not using mingw32.
pow is a C function, not C++, so use the alternative C header for the math library.
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::
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 am new to c++ and am trying to understand namespaces and how they work
I thought i'd code up a simple "hello world" program using namespaces but as it turned
out, it seems to have backfired on me and i am getting a bunch of weird errors.
Here is my code:
#include <iostream>
namespace names
{
using namespace std;
void class hello() //line 7 <-- here is where the compiler is complaining
about the 'unqualified id'
{
cout <<"Hello World";
}
}
int main()
{
names::hello(); //line 16
}
And here is the output:
E:\CB_Workspace\Names\names_main.cpp|7| error: expected unqualified-id before ')' token|
E:\CB_Workspace\Names\names_main.cpp|| In function 'int main()':|
E:\CB_Workspace\Names\names_main.cpp|16| error: invalid use of incomplete type 'struct names::hello'|
E:\CB_Workspace\Names\names_main.cpp|7| error: forward declaration of 'struct names::hello'|
||=== Build finished: 3 errors, 0 warnings ===|
I am not sure what is going on and I have tried to search through other posts on this error.
The other post i found on this did not really address the context of namespaces.
g++ error - expected unqualified-id before ')' token
Any help would be much appreciated. Thank you
edit: ok thanks guys. I removed the "class" under my namespace and it works now. I'll flag it to be closed now. Thanks for the help
You are not trying to write a class there. A class is different than a function. Please try:
void hello()
This has nothing to do with namespace.
In C/C++ the rule for declaring a function is:
returnType functionName(functionArgument1,functionArgument2,...);
Your way of declaring the function does not follow the C/C++ rule. What you have is:
void class hello();
It should be:
void hello();
Probably you are confusing it with syntax to define the function outside the class body. In that case the rule is:
returnType className::functionName(functionArgument1, functionArgument2,...)
{
}
Namespace does not affect how function is declared. It defines where the function is available
void class hello()
Huh? How can a function also be a class? Just remove that:
void hello()