VS2015 wont compile my code, says namespace "std" has no member "clamp", although intellisense picks it up just fine and tells me the parameters and return value.
Yes, i have included the header.
#include <Math/Matrix3D.h>
#include <glm.hpp>
#include <gtx/transform.hpp>
#include <Utils/Clock.h>
#include <algorithm>
void somefunc()
{
viewPos.y = std::clamp(viewPos.y, -0.95f, 0.95f);
}
You would have to use the /std:c++latest switch to enable C++17 additions to the standard.
https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/
Related
I change my settings in Visual Studio C++ language standard to Preview - Features from the Latest C++ Working Draft (std:c++latest), but it still not letting me use std::string_view and shows me this message
namespace "std" has no member "string_view"
Does somebody know why?
#include <iostream>
#include <string_view>
int main() {
std::string_view str{ "abc" };
}
I am having an error while trying to convert a string vector to a double vector. The error keeps saying:
error: 'stod' was not declared in this scope
Even though I enabled C++11 for my compiler and I used #include <string> I also used using namespace std; And it still didn't work.
The code is is down below:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
using stod;
transform(userNums.begin(), userNums.end(), back_inserter(convUserNums), [](const string & astr){ return stod( astr) ; } ) ;
stod is defined in the namespace std. You should call it like std::stod.
Or put using namespace std; above.
Be sure to include the necessary header and using directive.
#include <string>
using std::stod;
int main( const int, const char** )
{
stod( "3.4" );
return EXIT_SUCCESS;
}
Please find the std::stod documentation here. Note that this function will throw both std::invalid_argument and std::out_of_range exceptions. A try-catch block is highly recommended.
The example was built with g++ -o main main.cpp -std=c++11 under gcc version 4.9.2 (Debian 4.9.2-10).
I'm current building an application in which I have a log function that is accessible in most of my classes which was declared as below:
FileHandler.h
#ifndef FILEHANDLER_H
#define FILEHANDLER_H
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
//Include to allow logging
#include "log.h"
class fileHandler
{
public:
fileHandler();
virtual ~fileHandler();
void WriteToFile(const std::string& filename, std::string textToWrite);
std::vector<std::string> ReadFromFile(const std::string& filename);
std::string& TrimString(std::string& stringToTrim);
protected:
private:
class log logHandler;
std::vector<std::string> blockOfText;
std::string currentLine;
};
#endif // FILEHANDLER_H
Log.h
#ifndef LOG_H
#define LOG_H
#pragma once
#include <SDL.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
class log
{
public:
log();
virtual ~log();
void static WriteToConsole(std::string textToWrite);
void WriteToLogFile(std::string textToWrite);
protected:
private:
};
#endif // LOG_H
This worked fine for a long time and then I wanted to include another function elsewhere in my application that was only compatible with C++11 so I told the compiler to compile to these standards. I was then receiving an error on "log logHandler" saying log is not a declared name.
I was able to resolve the problem by changing the line to
class log logHandler;
I was wondering if anybody could tell me what has changed between C++03 and C++11 that required me to do this?
EDIT: Included all relevant code to make question more complete.
You don't show your real code (missing ; at the end of the class declaration, no #endif), but chances are that your problem is somehow related to std::log, which has received a new overload in C++11, in combination with a using namespace std somewhere in your code.
Note that the new overload is probably irrelevant to the problem at hand; the real reason may very well be a change somewhere in your compiler's standard-library implementation causing an internal #include <cmath>. This means that even in C++03, your code was only working by sheer coincidence, and a conforming C++03 compiler was always allowed to reject it.
Here is an example program which may reproduce your problem:
#include <cmath>
using namespace std;
struct log
{
};
int main()
{
// log l; // does not compile
struct log l; // compiles
}
Nothing has changed about how the code you posted is treated.
What I suspect is, that you somewhere have an
#include <cmath>
And below that, somewhere else
using namespace std;
This causes your compiler to not be able to unambiguously resolve the name log, since there is std::log (a function) and your class log.
By explicitly stating class log, you tell the compiler that you are referring to the class.
I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0,
from src/LM.cpp:1:
src/common.h:30:13: error: ‘hash’ is already declared in this scope
using tr1::hash;
This is the command I used to compile the files below.
g++ -std=c++11 -Wall src/Foo.cpp
Foo.cpp
#include "Foo.h"
...
Foo.h
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD:
https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
There is already std::hash in C++11. You cannot redefine it. You can use another name for tr1::hash.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map instead of C++11 structures) is to write your own namespace in which using all structures, that you want without std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}
I have a problem that does no resurface (even no warnings) in XCode but does allow me to compile in Keil MDK.
void grammar::parse(std::string &_expr) {
std::transform(_expr.begin(), _expr.end(), _expr.begin(), std::tolower);
_expr.erase(std::remove_if(_expr.begin(), _expr.end(), std::isspace), _expr.end());
}
That is what I get
error: #304: no instance of overloaded function "std::transform" matches the argument list
error: #304: no instance of function template "std::remove_if" matches the argument list
Header included:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
Could you please advise me on where to look? I am surprised that XCode version works as expected...
You include ctype.h, that header declares a function tolower in the global namespace (this is part of the C library, so there are no other namespaces there). Maybe you meant to include cctype. For a given C standard library header X.h, there is a c++ version cX that provides some of the same functionality inside the ::std namespace.