Error using alias declaration - c++

I'm trying to compile an easy program that use the alias declaration.
This is the code:
#include <iostream>
using namespace std;
using in = int;
in main ()
{
in a = 1;
cout << a << '\n';
return 0;
}
The command I use to compile is g++ -std=c++0x program_name.cxx, using the built-in terminal in Kate on Ubuntu OS.
But it doesn't work! Any suggestion?
(instead using typedef int in; it works).

Compile in C++11 mode. Type aliasing is supported only in C++11. I suspect the g++ version that use is older and doesn't fully support c++11, hence fails with c++0x.
Compile with: g++ -std=c++11 file.cpp
and it works.
By the way, it seems to be a terrible idea to alias int in such a way.

Related

How to compile generator and coroutine using c++2a on Mac

I am setting up my MacBook for C++20 and having problem to compile the code. I have installed latest Xcode, llvm and gcc. Here is the code that I am trying to compile
#include <chrono>
#include <experimental/coroutine>
#include <future>
#include <iostream>
using namespace std;
generator<int> getInts(int first, int last) {
for (auto i = first; i <= last; ++i) {
co_yield i;
}
}
int main() {
for (auto i : getInts(5, 10)) {
std::cout << i << " ";
}
}
however I am getting following error:
$ g++ gen.cpp -std=c++2a
In file included from gen.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental/coroutine:66:5: warning:
<experimental/coroutine> cannot be used with this compiler [-W#warnings]
# warning <experimental/coroutine> cannot be used with this compiler
^
appreciate any insight how to resolve this compile issue.
The Coroutines TS has not been voted into C++20 (indeed, there have been three separate votes, with approval not achieving consensus in all of them), so even if GCC implemented it (which it doesn't), it wouldn't be activated by the -std=c++2a switch.
Now, Clang does implement the Coroutines TS, which you have to turn on with -fcoroutines-ts. And apparently, you have to be using libc++.

Mac gcc doesn't allow calling std::string::~string explicitly

strdata->std::string::~string();
Here is the error I get:
error: '~' in destructor name should be after nested name specifier
strdata->std::string::~string();
^
I am using a cmake project... My gcc version installed via brew is following:
gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
I couldn't find the ~string() defined anywhere in the header files. I ended up changing it as follows and that works. It is ok for my use case for now.
strdata->std::string::~basic_string();
This original seems correct and works perfectly in GCC on Linux and CYGWIN. What is the issue which is preventing it from working on mac? Templates? Something else?
This isn't a complete answer. For some reason, using namespace std; works, but without that clang fails. Consider this example:
#include <new>
#include <type_traits>
namespace foo {
struct A {};
typedef A A_t;
}
int main() {
std::aligned_storage<sizeof(foo::A)>::type storage;
foo::A_t* obj = new(&storage) foo::A;
using namespace foo; // Without this line, clang fails.
obj->foo::A_t::~A_t();
}
Without the using namespace foo; line, clang will give an error expected the class name after '~' to name a destructor. But with that line, it works. Extending this to std::string:
#include <new>
#include <type_traits>
#include <string>
int main() {
std::aligned_storage<sizeof(std::string)>::type storage;
std::string* obj = new(&storage) std::string;
using namespace std; // Without this line, clang fails.
obj->std::string::~string();
}
it works. It also works with the narrower using std::string;.
This doesn't answer the question of why clang fails. I don't know if it's a bug in clang or in gcc. But at least a workaround exists.
It may be worth reporting this as a bug in clang, and then letting them decide whether or not it really is a bug.

C++11 typedef alias compile error

When I try to compile this, I get the following error:
error: expected unqualified-id before ‘using’
I know, this was asked several times before, but I didn't find the answer. Usually they say that a semicolon is missing in one of the header files. But it's not the case now. And of course I use the -std=c++0x flag
#include <iostream>
#include <string>
#include <vector>
template <typename T>
using stringpair = std::pair<std::string, T>;
int main (int argc, char* argv[]) {
return 0;
}
Your error is caused by the fact that template aliases with using is a C++11 feature and your compiler does not support it. You should add the corresponding flags at compilation. Those most likely are:
-std=c++11
(at least for g++ and clang++).
Live demo
Otherwise your compiler does not support them yet. GCC supports them from 4.7.

c++11 to_string to working with code::blocks -std=c++11 flag already selected

this is the code i am trying to compile, got it from another forum somewhere.
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return 0;
}
I am getting the error:
'to_string' is not a member of 'std'
I have read in other forums to select the flags "Have g++ follow the c++11 ISO language standard [-std=c++11]" and i have and it still doesn't work.
Any help would be greatly appreciated
I am using the GNU GCC Compiler
and code::Blocks 12.11
MinGW-w64 added support for the necessary functionality since GCC 4.8, so make sure you are using at least version 4.8 GCC from MinGW-w64.
You can get one from here, although Code::Blocks should come with a TDM GCC toolchain which should work if it's the latest (because it's GCC 4.8.1 at the time of writing).
i also encounter this error in codeblocks-13.12mingw-setup-TDM-GCC-481.exe(built on 27 Dec 2013).
it seems a bug with tdmgcc4.8.1. maybe the newest tdmgcc whill fixed it.
https://stackoverflow.com/questions/21626866/c-elipse-cdt-getting-to-string-was-not-declared-in-this-scope-with-tdm-gcc-6
the reason of above list should be the same as ours.
==============================
The std::to_string functions are guarded by !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF). MinGW defines this symbol as 1 in its os_defines.h, so you know, we cannot use it in mingw.
Easiest way to deal with this error is:
double piValue = 3.1415;
char piBuffer[8];
sprintf(piBuffer, "%f", piValue);
string pi(piBuffer);

unordered_map error in GCC

When was the unordered_map concept built into g++?
Because the following code throws an error.
#include<iostream>
#include<unordered_map>
#include<stdio.h>
using namespace std;
std::unordered_map<std::int,int> mirror;
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
int main(void)
{
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
return 0;
}
I am compiling the code as follows:
g++ -c hashexample.cpp
g++ -o result hashExample.o
./result
The error I got is this:
inavalid types int[char[ for aaray subscript
What is the fix for this?
The problem is your assignment. You cannot assign values to your map in this place. C++ is not a script language.
This program works fine on my machine with gcc4.6:
#include<iostream>
#include<unordered_map>
std::unordered_map<int,int> mirror;
int main() {
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
}
First, as mkaes points out, you cannot put assignments outside functions, so you have to put it in any, for example main.
As for unordered_map, for recent versions of gcc, if you don't want to go into C++11, you can use the TR1 version of unordered_map:
#include <tr1/unordered_map>
and the type std::tr1::unordered_map. You know, C++11 supersedes all this, but you will (at least in GCC) get this working.