queue is not a template - c++

I've been creating codes using C++ STL. And I want to use "queue".
So, I wrote the codes like below.
But, I encountered "queue is not a template" error.
As you can see, I wrote headers related with queue (iostream, queue) in "Common.h" file and wrote include "Common.h" in "DataQueue.h" file. But, VS2013 IDE tool said that 'queue m_deQueue' is error because queue is not a template. I don't know why.. this error occured. Any help is appreciated!
//[Common.h]
#ifndef _COMMON_
#define _COMMON_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
//thread related headers
#include <Windows.h>
#include <process.h>
//socket related headers
#include <winsock.h>
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#endif
//[DataQueue.h]
#ifndef _QUEUE_
#define _QUEUE_
#include "SocketStruct.h"
#include "Common.h"
class CDataQueue{
private:
static CDataQueue* m_cQueue;
// deque <ST_MONITORING_RESULT> m_deQueue;
queue <ST_MONITORING_RESULT> m_deQueue;
CRITICAL_SECTION m_stCriticalSection;
CDataQueue();
~CDataQueue();
public:
static CDataQueue* getDataQueue(){
if (m_cQueue == NULL){
m_cQueue = new CDataQueue();
}
return m_cQueue;
}
deque <ST_MONITORING_RESULT> getQueue();
void pushDataToQueue(ST_MONITORING_RESULT data);
ST_MONITORING_RESULT popDataFromQueue();
};
#endif
//[DataQueue.cpp]
#include "DataQueue.h"
CDataQueue* CDataQueue::m_cQueue = NULL;
CDataQueue::CDataQueue(){
::InitializeCriticalSection(&m_stCriticalSection);
// m_mutex = PTHREAD_MUTEX_INITIALIZER;
}
CDataQueue::~CDataQueue(){
::DeleteCriticalSection(&m_stCriticalSection);
}
::deque <ST_MONITORING_RESULT> CDataQueue::getQueue(){
return m_deQueue;
}
void CDataQueue::pushDataToQueue(ST_MONITORING_RESULT data){
::EnterCriticalSection(&m_stCriticalSection);
m_deQueue.push_back(data);
::LeaveCriticalSection(&m_stCriticalSection);
}
ST_MONITORING_RESULT CDataQueue::popDataFromQueue(){
::EnterCriticalSection(&m_stCriticalSection);
ST_MONITORING_RESULT data = m_deQueue.front();
m_deQueue.pop_front();
::LeaveCriticalSection(&m_stCriticalSection);
return data;
}

Sitting at the top of the <queue> header from the MS implementaiton of the standard library, we find...
// queue standard header
#pragma once
#ifndef _QUEUE_
#define _QUEUE_
Which means your usage of that identifier for your own header fencepost is precluding the MS header body from being pulled in. Thus, no std::queue for you. Use a different id, preferably something that doesn't violate usage rules for macro constants reserved for the implementation (like this one).
And that, kids, is why we don't use identifiers reserved for implementation usage. For more information, read this question: "What are the rules about using an underscore in a C++ identifier?"

Related

can someone help me with the #include being nested to deeply error before i go insane? thx [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed last year.
//main.cpp
#include <iostream>
#include <array>
#include <iomanip>
#include "Board.h"
#include "Game.h"
//Player.h
#include <array>
#include <string.h>
#include <random>
#include "Property.h"
#include "Game.h"
#ifndef Player_h
#define Player_h
//Property.h
#include "Space.h" //#include nested too deeply error
#ifndef Property_h
#define Property_h
//Space.h
#include <sstream>
#include <string>
#ifndef Space_h
#define Space_h
//FreeParking.h + a couple others inheriting from space; there's no error in any of these either
#include "Space.h"
#ifndef FreeParking_h
#define FreeParking_h
//Board.h
#include "Player.h"
#include <array>
#include <random>
#ifndef Board_h
#define Board_h
//Game.h
#include <array>
#include "Property.h"
#include "CommunityChest.h"
#include "Tax.h"
#include "FreeParking.h"
#include "Jail.h"
#include "GoToJail.h"
#include "Go.h"
#include "Player.h"
#ifndef Game_h
#define Game_h
I don't think I made any changes to the #includes today but just got this error even though the program was running fine 20 minutes ago. I only posted the includes because I'm not sure if the actual code matters for this error or not. If it does I'll try to go over the stuff I wrote today, but most of it was just changing things that already worked previously.
Player.h includes Game.h and Game.h includes Player.h. This is an infinite loop. There might be more, but that's just the first one I saw.
You should remove at least one of those includes to break the infinite loop. If you get errors when you do that, you might be able to fix them using a forward declaration that looks something like this:
class Player;
A forward declaration like that would allow you to compile some code that uses the Player class, even though a complete definition of the Player class is not available at that point in the program.
Two more tips to make things more sane:
Put your include guards at the very top of your file before you include anything.
Use #pragma once as the include guard instead of your more complicated thing.

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

Class differences between C++03 and C++11

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.

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

'SHGFP_TYPE_CURRENT' was not declared in this scope

I am migrating a huge project from Qt 4.x to 5, (in fact I have asked for help several times here, I couldnt be more grateful for your help).
I am getting the next error:
..\marssies\userlayerswidget.cpp: In member function 'void
LayersModel::importFromOld()':
..\marssies\userlayerswidget.cpp:1736:60: error: 'SHGFP_TYPE_CURRENT'
was not declared in this scope
It doesnt make too much sense, as I have the correct header included, here are all the includes:
#include "userlayerswidget.h"
#include "appcommon.h"
#include "messagebox.h"
#include "polyline.h"
#include "painterbar.h"
#include "rectangle.h"
#include "polygon.h"
#include "label.h"
#include "line.h"
#include "point.h"
#include "encsymbol.h"
#include "touchswibz.h"
#include "mapmodulelist.h"
#include "offlinelayersaver.h"
#include "circle.h"
#include <QMenu>
#include <QDir>
#include <QDesktopServices>
#include <QtDebug>
#ifdef _WIN32
#include <Shlobj.h>
#endif
And here is the piece of code that makes use of SHGFP_TYPE_CURRENT:
void LayersModel::importFromOld() {
TCHAR appPath[MAX_PATH];
if (!(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appPath))) {
//code
}
I have researched and everything is correct according to http://msdn.microsoft.com/en-us/library/bb762181%28VS.85%29.aspx
I tried to find other people with the same problem but either the context was different or the question wasnt answered.
Thankyou.
In ShlObj.h is SHFGP_TYPE_CURRENT defined like this:
#if (_WIN32_IE >= 0x0500)
typedef enum {
SHGFP_TYPE_CURRENT = 0,
SHGFP_TYPE_DEFAULT = 1,
} SHGFP_TYPE;
#endif
So one can do this:
#define _WIN32_IE 0x0500
#include <ShlObj.h>
Or another way is to directly use value 0 or 1 as parameter to SHGetFolderPath.
Doing more research I found an answer that was to replace SHGFP_TYPE_CURRENT with a literal 0, I did it and it compiled, but I'm not sure if it will be the same (I haven't written this program, I'm just migrating it). So if anybody could give some insight it would be nice.
Source: http://webcache.googleusercontent.com/search?q=cache:http://www.dreamincode.net/forums/topic/200660-undeclared-variable/