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

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};
}

Related

Visual Studio doesn't recognize std library

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.

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?

error: ‘GlobalRNG’ was not declared in this scope

I'm using Crypto++ to encrypt files in C++. And I'm using the code below.
It doesn't contain the headers files so I added my own :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/sha.h>
#include <cryptopp/secblock.h>
#include <cryptopp/files.h>
#include <cryptopp/queue.h>
#include <cryptopp/hex.h>
#include <cryptopp/base64.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/integer.h>
#include <cryptopp/dh.h>
#include <cryptopp/sha.h>
#include <cryptopp/modes.h>
#include <cryptopp/eax.h>
#include <cryptopp/tea.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/pssr.h>
#include <cryptopp/rsa.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/eccrypto.h>
#include <cryptopp/oids.h>
#include <cryptopp/modes.h>
#include <cryptopp/gzip.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/rsa.h>
#include <cryptopp/rng.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/filters.h>
#include <cryptopp/rdrand.h>
using namespace std;
using namespace CryptoPP;
But unfortunately the code doesn't work
Saying that the GlobalRNG is not declared !
error: ‘GlobalRNG’ was not declared in this scope
I googled and kept looking for a solution for 2 days i found that it's a bug and fixed but i'm having the latest version : 5.6.3 !
So i really don't know why this error is showing !
In the version 5.6.3 GlobalRNG is defined in the file validate.h, as:
// Functions that need a RNG; uses AES inf CFB mode with Seed.
CryptoPP::RandomNumberGenerator & GlobalRNG();
Just add this inclusion:
#include <cryptopp/validate.h>
to solve definition problem.
GloablaRNG is part of testing and bench-marking. It should not be part of the library proper (i.e., libcryptopp.a or libcryptopp.so). If your programs are complaining about a missing GloablaRNG, then the library was cross-contaminated with some of the testing and bench-marking gear.
These are the files used for testing and bench-marking. They should not be included in your build of the library or your project:
validate.h
bench.h
test.cpp
bench1.cpp, bench2.cpp
validat0.cpp, validat1.cpp, validat2.cpp, validat3.cpp
datatest.cpp, regtest.cpp, fipsalgt.cpp, dlltest.cpp
You are free to use a function called GlobalRNG(). Here's how its used in the library's test and bench-marking gear. But you might consider using an AutoSeededRandomPool instead. The AutoSeededRandomPool is a PGP-style generator, and its seeded from /dev/urandom, /dev/srandom, /dev/random or the Windows entropy pool.
Declaration in validate.h
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
CryptoPP::RandomNumberGenerator & GlobalRNG();
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Definition in test.cpp
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
ANONYMOUS_NAMESPACE_BEGIN
OFB_Mode<AES>::Encryption s_globalRNG;
NAMESPACE_END
RandomNumberGenerator & GlobalRNG()
{
return dynamic_cast<RandomNumberGenerator&>(s_globalRNG);
}
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Seeding in test.cpp
// Don't do this in production because it creates a deterministic generator
OFB_Mode<AES>::Encryption& aesg = dynamic_cast<OFB_Mode<AES>::Encryption&>(Test::GlobalRNG());
aesg.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data());
A lot of folks have had this problem over the years. At Crypto++ 6.0, we moved GlobalRNG() into the Test namespace. Test is a new namespace, and we hope Test::GlobalRNG() will provide the signals that something is amiss in your library build or project configuration.
Also see Issue 379, Add Test namespace within CryptoPP namespace and Commit 73836e58a5f5c11c.

‘hash’ is already declared in this scope using tr1::hash;

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
}

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>