This code does not compile with the command g++ -std=c++17 main.cpp
#include <iostream>
#include <experimental/optional>
int main()
{
std::optional<int> x;
std::cout << "Hello World";
return 0;
}
The Errors are the following:
error: ‘optional’ is not a member of ‘std’
error: expected primary-expression before ‘int’
Is there a way to get this code to compile?
The <experimental/optional> header doesn't define std::optional but rather std::experimental::optional. To get std::optional, which is a (non-experimental) part of the C++17 standard, you should just #include <optional>.
Try on godbolt.
Related
I'm using the gcc compiler on Windows with MinGW. Version is 4.9.3.
The following code gives errors when -std=c++98, -std=c++03 or -std=c++11 is used as an argument.
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
The code compiles with no errors when -std=gnu++98, -std=gnu++03 or std=gnu++11 is used as an argument. Also, the code compiles with no errors when using no c++ version arguments (g++ test.cpp -c)
On further investigation I found it was the #include causing issues.
This code produces no errors when using any of the std=c++ arguments:
int main()
{
return 0;
}
However, when looking for other things to include to test my code, the following works:
#include <cmath>
int main()
{
return 0;
}
but this doesn't:
#include <string>
int main()
{
return 0;
}
What's going on? From a brief search on gnu++, it says that it provides additional extensions but code as simple as the ones above surely shouldn't be reliant on any extensions?
I've pasted the large error which occurs when compiling the first piece of code with g++ test.cpp -c -std=c++11. http://pastebin.com/k0RLtWQz
The first messages are:
$ g++ test.cpp -c -std=c++11
In file included from c:\mingw\include\wchar.h:208:0,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\cwchar:44,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iostream:39,
from test.cpp:1:
c:\mingw\include\sys/stat.h:173:14: error: '_dev_t' does not name a type
struct _stat __struct_stat_defined( _off_t, time_t );
^
c:\mingw\include\sys/stat.h:173:14: error: '_ino_t' does not name a type
struct _stat __struct_stat_defined( _off_t, time_t );
^
…
Solved by changing to mingw64 (which also uses a newer version of gcc). Seems like the problem was with either my mingw32 installation or with the distribution (as pointed out by Jonathan Leffler).
All the -std=c++xx parameters now work.
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. )
I'm trying to compile a simple program utilizing literals from the std::literals namespace, but Clang is generating errors when I try to compile it.
The code I'm trying to compile:
#include <string>
#include <iostream>
using namespace std::literals;
int main()
{
std::cout << "Hello World!"s << std::endl;
return 0;
}
and the compilation command:
clang++ -stdlib=libstdc++ -std=c++1y a.cpp
which leads to this output:
a.cpp:4:22: error: expected namespace name
using namespace std::literals;
~~~~~^
a.cpp:8:29: error: no matching literal operator for call to 'operator "" s' with arguments of
types 'const char *' and 'unsigned long', and no matching literal operator template
std::cout << "Hello World!"s << std::endl;
^
2 errors generated.
Using g++ or libc++ are out of the question for various reasons, and I've confirmed that other C++14 features (ie. return type deduction and binary literals) work, so it's not an issue with the compiler, making me believe it involves libstdc++.
What can I do to fix this? I'm on Linux Mint 17.1 if it makes any difference.
Remember to ensure that you're compiling the source according to C++14 (the chrono literals are not provided in C++11).
clang++ -stdlib=libstdc++ -std=c++14 a.cpp
The following code fails to compile:
#include <cstdio>
#include <sstream>
int main()
{
std::ostrstream strm;
strm.rdbuf()->freeze(0);
}
I get the following errors on compilation:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:5: error: 'ostrstream' is not a member of 'std'
sample3.cpp:5: error: expected `;' before 'strm'
sample3.cpp:6: error: 'strm' was not declared in this scope
After searching in google, I suspect that I should use ostringstream in place of ostrstream, so I have modified the program as below:
#include <cstdio>
#include <sstream>
int main()
{
std::ostringstream strm;
strm.rdbuf()->freeze(0);
}
But now I get the following errors:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:6: error: 'struct std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >' has no member named 'freeze'
Just scrap the freeze() call -- the current generation std::ostringstream doesn't expose its memory management guts to you like the old ostrstream did. You'll need to rework your original code to let the stringstream manage memory in the way it wants (it'll be much simpler/less error-prone that way!).
I should have changed #include "sstream" to #include "strstream"
then it will not report the error "'ostrstream' is not a member of 'std'".
Why this doesn't compile on gcc?
#include <iostream>
#include <string>
int main() {
double f = 23.43;
std::wstring f_str = std::to_wstring(f);
std::wcout << f_str << std::endl;
return 0;
}
The error:
prog.cpp: In function ‘int main()’:
prog.cpp:6:26: error: ‘to_wstring’ is not a member of ‘std’
example
std::to_wstring() was added in C++11. The link in your answer, on ideone, is not using the C++11 compiler:
language: C++ (gcc-4.7.2)
It compiles fine: http://ideone.com/UO2FQM :
language: C++0x (gcc-4.7.2)
Your program compiles IF you choose C++0x in IDEONE instead of C++ because its a C++11 feature.