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.
Related
I updated my g++ because my older version didn't fully support std::filesystem, but now I cannot use mutexes at all. Example code:
#include <mutex>
int main(const int argc, const char** argv)
{
std::mutex test;
return 0;
}
Compiled with:
g++ -Wall -pedantic main.cpp -std=c++17 -o main.exe
The error, note how it suggests adding include for <mutex> right under the include that is already there:
main.cpp: In function 'int main(int, const char**)':
main.cpp:5:8: error: 'mutex' is not a member of 'std'
5 | std::mutex test;
| ^~~~~
main.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <mutex>
+++ |+#include <mutex>
2 |
The full version of g++ is: g++ (MinGW.org GCC Build-2) 9.2.0. I installed MinGW using the MinGW installation manager. Is it possible that I am missing the correct version of stdlib?
I opened the mutex file in the MinGW directory and I see nothing that would be obviously wrong.
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.
I have installed boost by downloading and unzipping the boost_1_67_0.tar.bz2 file in the following location:
/cygdrive/c/no_admin_programs/boost_1_67_0
I have entered the following into a file test.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " ");
}
And entered the following in the terminal:
c++ -I /cygdrive/c/no_admin_programs/boost_1_67_0 test.cpp
As a result, the following error is shown:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/include/stdlib.h:91:7: error: expected initializer before ‘__alloc_size2’
__alloc_size2(1, 2) _NOTHROW;
^~~~~~~~~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/include/stdlib.h:144:63: error: expected initializer before ‘__alloc_size2’
void *reallocarray(void *, size_t, size_t) __result_use_check __alloc_size2(2, 3);
^~~~~~~~~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:138:11: error: ‘::calloc’ has not been declared
using ::calloc;
^~~~~~
I've tried including <cstdlib> to no avail. Any assistance would be much appreciated.
if you also install the boost package alread provided on cygwin
there is no need to try to use boost_1_67_0.tar.bz2
$ cygcheck -c libboost-devel
Cygwin Package Information
Package Version Status
libboost-devel 1.66.0-1 OK
your program works fine with just
$ c++ test.cpp
$ ./test.exe
4
12 5
15 7
21 9
27
I'm trying compile this code in cmd:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 24;
string s = to_string(n);
cout<<s;
}
I found in another qestions, that I should use this command:
g++ -std=c++11 main.cpp
but I get error:
main.cpp: In function 'int main()':
main.cpp:7:24: error: 'to_string' was not declared in this scope
string s = to_string(n);
^
I have latest g++ compiler for Windows v. 5.3.0. I used this installer for MinGW.
I tried solve in to_string is not a member of std, says g++, but this patch doesn't work.
How can I compile this code in command line in Windows?
I am using geany (code::blocks wouldnt run my programs) as a compiler to compile a simple c++ program with one class. I am on Linux Mint 17 on a Dell Vostro 1500. Compiling works fine with both .cpp files, but the header file gives this error:
gcc -Wall "Morgan.h" (in directory: /home/luke/Documents/Coding/Intro#2)
Morgan.h:5:1: error: unknown type name ‘class’
class Morgan
^
Morgan.h:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
Compilation failed.
This is the main.cpp :
#include <iostream>
#include "Morgan.h"
using namespace std;
int main()
{
Morgan morgObject;
morgObject.sayStuff();
return 0;
}
This is the Header file (Morgan.h):
#ifndef MORGAN_H
#define MORGAN_H
class Morgan
{
public:
Morgan();
void sayStuff();
protected:
private:
};
#endif // MORGAN_H
And this is the class (Morgan.cpp):
#include <iostream>
#include "Morgan.h"
using namespace std;
Morgan::Morgan()
{
}
void Morgan::sayStuff(){
cout << "Blah Blah Blah" << endl;
}
I really do not know what is going wrong, so any help would be appreciated. I copy and pasted the same code into a windows compiler and it worked fine, so it might just be the linux.
also when I run the main.cpp this is what shows:
"./geany_run_script.sh: 5: ./geany_run_script.sh: ./main: not found"
You don't compile .h files. Try g++ -Wall main.cpp Morgan.cpp
Your issue is that you are compiling C++ code with a C compiler (GCC). The command you are looking for is g++. The complete command that would compile your code is:
g++ -Wall -o run.me main.cpp Morgan.cpp
If a file is included (In your case the Morgan.h file, you do not need to explicitly compile it. )