The following code gives me a compile time error:
#include <chrono>
int main() {
auto day = 24h;
return 0;
}
Error C3688: invalid literal suffix 'h'; literal operator or literal operator template 'operator ""h' not found.
I'm trying this on Visual Studio 2015 Update 1, which according to this should work, so what's going on?
The literals aren't in the global namespace. Add this:
using namespace std::chrono_literals;
Depending on the situation, you might also consider using:
using std::chrono::operator""h;
instead of importing every name from that namespace if you need more fine grained control.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
In Visual Studio 2017 when creating Linux project and inserting using namespace std; in source code like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t i = 1;
string s = to_string(i);
cout << i << s << endl;
return 0;
}
VS underlines size_t and says that it is an ambiguous symbol.
If I press F12 (Go to definition) it offers me two definition places:
From stddef.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\5\include\stddef.h):
// ...
namespace std
{
typedef __SIZE_TYPE__ size_t;
// ...
And c++config.h
(C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\c++\5\bits\c++config.h):
// ...
#if !(defined (__GNUG__) && defined (size_t))
typedef __SIZE_TYPE__ size_t;
// ...
It happens only with Linux projects in VS, not with Windows projects.
Is there any known solution (except for "do not use using namespace std; :) )?
Upd: Reported this problem to Microsoft: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
Upd2: Microsoft says that they fixed it, and solution will be in next update: https://developercommunity.visualstudio.com/content/problem/67405/ambiguous-symbol-size-t-in-linux-projects-when-usi.html
It looks like there is a difference between Microsoft's and other compilers related to typedef in and out of a namespace.
This source file
namespace foo { typedef int moo; }
typedef int moo;
using namespace foo;
extern moo a;
compiles in g++ and clang++ (no warnings with -Weverything). MSVC rejects it because of an ambiguous symbol.
This is exactly the situation with size_t in gcc headers. It is typedefed both in and out of namespace std. This doesn't seem to cause any problems with
g++.
Why does this compile in g++ and not in msvc? I guess this is because of different interpretation of 7.1.3/3
In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.
Admittedly the interpretation by g++ is rather loose. The first moo is not declared within namespace :: so the rule seemingly doesn't apply. I cannot find anything else that would permit such a thing.
To solve the problem, I would patch the header where size_t is defined in the global namespace, and bring the declaration inside namespace std (conditionally, if __cplusplus is defined). But I have not tested it (no VC2017 here) and cannot guarantee it will work.
I also have no idea why your code is accepted by the actual compiler and only rejected by IntelliSense. I have tested this construction with actual compilers. (update to the last sentence: I have had the code tested with, and rejected by, MSVC. I have conducted the tests before realising that "the actual compiler" above is in fact gcc and not MSVC).
class Space2D {
public:
vector<Agent> v;
bool star;
Space2D() {
bool star = false;
}
};
In visual studio this give me a error: missing type specifier - int assumed. I also get errors like syntax error: missing ';' before '<'.
std::vector<Agent> v; Solved this
Three possible problems with this code.
Possibly vector header is not included
Fix: #include <vector>
Possibly Agent class is not defined
Fix: include header, where Agent class is defined
Possibly you forgot to write using namespace std; as wrongly recommended by beginner books
Fix: instead of 'vector' on line 7 use std::vector, or do it wrong and write using namespace std;
Assuming from two error messages you posted and by guessing to which line they correspond to you forget to #include <vector> or you do not have imported std::vector to your namespace (using std::vector; or using namespace std).
I personally would not recommend using either of those usings for reasons and instead wrote std::vector.
This appears to be a visual-studio problem. This code runs fine in gcc but fails to compile in Visual Studio:
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;
true_type foo();
template <typename T>
struct bar{
using def = conditional_t<decltype(foo())::value, char, void>;
};
int main() {
cout << typeid(bar<int>::def).name() << endl;
cout << decltype(foo())::value << endl;
}
The error given is:
error C2146: syntax error: missing > before identifier value
Live Example
Is there a bug fix for this or a workaround?
In your question you're using decltype(foo()):
using def = conditional_t<decltype(foo())::value, char, void>;
^^^^^
while on Ideone, decltype(foo):
using def = conditional_t<decltype(foo)::value, char, void>;
^^^^^
They're different things. In the first case, you're getting the type of the result of calling foo. In the second one you're getting the type of a function itself.
Okay, since that time things changed dramatically.
The code was edited and was supposed to compile well, but compilations with Visual Studio were failing, while clang was very happy with this code and wasn't showing any errors and even warnings.
So, given that clang (the latest version, using --std=c++14 -Wall -Wextra) finds this code correct, I believe this should be a bug in VS.
I'm not sure if it can help you, but I am able to work around the problem like this:
First define:
template <typename I> using id = I;
Then replace every instance of decltype(foo())::value with
id<decltype(foo())>::value
Alternately, you could use std::common_type_t the same way:
std::common_type_t<foo()>::value
Or, my psychic powers predict you might just want to define a separate type for decltype<foo()>, for convenience:
using id = decltype(foo());
then replace all instances of decltype(foo())::value with id::value.
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.