Visual Studio doesn't recognize std library - c++

I'm writing an MFC application on Visual Studio 2015 in C++. I added some code which uses members of std library and suppose to take an int and create from it a hex char* with the prefix "0x". I tried to build the project on VS 2015 and VS 2017 from two different computers and I get the same errors - VS doesn't recognize the std library. I've tied running the code on other programs (Clion) and it worked well.
When I include #include <stdlib> I get the following error:
cannot open source file "stdlib"
I've tried re-installing VS, and checked I have all the necessary extensions to support C++, but I guess there's still something missing. How can I fix it?
The code:
std::ostringstream ss;
int i = 7;
ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();
std::cout << output << std::endl;
and included the following headers:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
I get the following errors:
'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier
Thank you.

I've included the headers in the wrong order. In every C++ project in Visual Studio it includes "stdafx.h" library automatically. This library contains many of the commonly used libraries such as <string> and etc. The solution was to write the includes in the following way:
#include "stdafx.h"
// other headers of the form "header.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
instead of:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
#include "stdafx.h"
// other headers of the form "header.h"
a bit more about this in this question
Thanks for everyone who tried to help, I appreciate your time and attention.

Related

"name followed by '::' must be a class or namespace name" error in release mode but not debug mode

C++, Visual Studio 2019
I keep getting this error in release mode but not debug mode for lines that use the "filesystem" and "chrono" libraries. I'm thinking that the issue probably relates to how the header files are linked in release mode.
Below are the relevant snippets of my code (ellipses indicate omitted code). The red underline of the error is under "chrono" and "filesystem", respectively.
#include <iostream>
#include <vector>
#include <fstream>
#include <numeric>
#include <string>
#include <algorithm>
#include <ctime>
#include <iterator>
#include <filesystem>
#include <cmath>
using namespace std;
...
int main() {
auto startTime = chrono::steady_clock::now(); // ERROR HERE
...
for (auto& entry : filesystem::directory_iterator(AbsPath + PTS_ByIndustry_Path)) { // ERROR HERE
if (find(TS_ToIncludeNums.begin(), TS_ToIncludeNums.end(), i) != TS_ToIncludeNums.end()) {
string p = entry.path().u8string();
PTS_ToInclude.push_back(p);
}
i++;
}
...
}
Fixed already! In project properties, I was changing the language standard to C++17 for Debug Mode but not Release Mode.
Screenshot:
.
It seems you didn't import the chrono header.
#include <chrono>

std::queue included in precompiled header is not part of namespace std

I have a problem with the inclusion of header "queue" via a precompiled header file in c++ (VS 2019).
My Visual Studio solution consists of two projects, one engine project (static library) and one sandbox project (links the engine project) to test the engine's functionality. The engine project uses a precompiled header file, in which i just included "queue" since i am implementing a message system. Both projects are using C++17.
Visual Studio compiles the engine project without a problem, the sandbox project then throws the following error while compiling:
Error C2039 'queue': is not a member of 'std' (path\to\engine\message_handler.h)
As soon as I include "queue" directly in message_handler.h (see code excerpt), the error vanishes and the project starts up just fine.
I have been using the precompiled header for almost a year now and never had any problems.
Can anyone help me with this?
Following are the relevant excerpts from my code.
Precompiled header:
#pragma once
//memory
#include <memory>
//timing
#include <chrono>
#include <ctime>
//container
#include <vector>
#include <queue>
#include <unordered_map>
#include <array>
//string and streams
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
//misc
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <random>
message.h
struct Message
{
Message(){}
uint32_t id_;
};
message_handler.h
#pragma once
//#include <queue> //fixes the issue
#include "message.h"
using UnqPtr = std::unique_ptr<Message>;
class MessageHandler
{
public:
MessageHandler();
private:
static constexpr uint32_t maxMessages_ = 10000;
std::queue<UnqPtr<Message>, std::vector<UnqPtr<Message>>> msqQueue_;
};
message_handler.cpp
#include "trpch.h" //precompiled header
#include "message_handler.h"
MessageHandler::MessageHandler()
{
//reserve space in the underlying vector
std::vector<UnqPtr<Message>> queue;
queue.reserve(maxMessages_);
msqQueue_ = std::queue{queue};
}

Trouble with including external directories in C++ with ViconDataStreamSDK

#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <functional>
#include <limits>
#include <math.h>
#include "DataStreamClient.h" //[x]
#include "stdafx.h"
using namespace ViconDataStreamSDK::CPP;
{ ...
This is the start of my file that uses the header file ViconDatastreamSDK.h but in every instance that a class from the file is called Visual Studio gives the error:
'ViconDataStreamSDK': is not a class or namespace name
The solution explorer shows that the file is included in the external directories.
How do I get VisualStudio to recognize these classes?

Returning a string from static function

I have two files: DateTime.h and DateTime.cpp which are shown below:
DateTime.h
class DateTime
{
public:
static string getCurrentTimeStamp();
};
DateTime.cpp
#include "stdafx.h"
#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
My compiler (Visual Studio 2012) is spitting out errors the moment I have the function getCurrentTimeStamp() return an std::string object. The errors all point to syntactic problems but none are clear. Does anyone understand why this may be the case?
Update: Here are (some of) the errors.
Error 6 error C2064: term does not evaluate to a function taking 0
arguments c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 21 1 ConsoleApplication1
Error 1 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 7 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 5 error C2371: 'DateTime::getCurrentTimeStamp' : redefinition;
different basic
types c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 10 1 ConsoleApplication1
When trying to diagnose an issue with a header file, especially a simple one like this, step 1 is to try and see what the compiler sees.
#include is a pre-processor directive, so the compiler doesn't see it, instead the compiler sees the pre-processed output of the file that you're trying to include.
So your code would look something like this:
#include "stdafx.h"
//#include "DateTime.h"
class DateTime
{
public:
static string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
http://rextester.com/MODV66772
When I try and compile this on RexTester's online Visual Studio, I get very different errors, telling me that your stdafx.h isn't empty.
If I modify the code a little:
//#include "stdafx.h"
//#include "DateTime.h"
#include <string>
class DateTime
{
public:
static std::string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
This now compiles without the errors/warnings you are reporting: http://rextester.com/PXE62490
The changes:
include in the header file, since the header depends on it,
use std::string instead of string,
The C++ compiler is a single-pass compiler, so the header file can't know that you are intending to do using namespace std later, and even if it did, it's a terrible practice because the std namespace is densely populated.
If you absolutely can't do with typing std:: all over the place, try using the names you need, e.g.
using std::string; // string no-longer needs to be std::string

Compile errors when attempting to link <boost\property_tree\json_parser.hpp>

I have the following "includes" file in my project.
#pragma once
//glm
#include <glm\glm.hpp>
#include <glm\ext.hpp>
#include <glm\gtc\matrix_transform.hpp>
//glew
#include "GL\glew.h"
//glfw
#define GLFW_DLL
#include "GLFW\glfw3.h"
//libpng
#include <png.h>
//std
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <memory>
#include <iostream>
#include <fstream>
#include <assert.h>
//boost
#include <boost\filesystem.hpp>
#include <boost\property_tree\json_parser.hpp> /* problem */
//mandala
#include "types.h"
#include "type_traits.h"
#include "hash.h"
#include "macros.h"
When I include <boost\property_tree\json_parser.hpp>, I get many errors indicating that I'm redefining APIENTRY such as this one:
1>c:\program files (x86)\windows kits\8.0\include\shared\minwindef.h(130): warning C4005: 'APIENTRY' : macro redefinition
I'm perplexed as to why this is happening. I've tried to suppress the minwindef.h file from being processed by putting #define _MINWINDEF_ before the include statement but to no avail. Has anyone else encountered this or have any idea how I can properly include this boost library?
NOTE
Since youd did neither update your question to reflect the changes to the includes you made, nor provide the whole warning message, I can only guess:
You still have glfw.h included before the boost lib that includes the WinAPI header. Because when I just google for "APIENTRY redefinition", I get this SO question as first result, including the answer: Put the WinAPI header (or the boost header includign them) before the glfw.h include.
You may want to include also ptree.
#include <boost/property_tree/ptree.hpp>