Strip pointer from void* for use in unique_ptr using statement? - c++

I'm trying to compile a simple C++ program that uses some functions and datastructures from the Win32 API and Wincrypt:
#include <memory>
using std::unique_ptr;
#include <Windows.h>
#include <Wincrypt.h>
using CERTSTORE_ptr = unique_ptr<CERTSTORE, decltype(&:: CertCloseStore)>;
int main(int argc, char* argv[])
{
return 0;
}
When I attempt to compile it, I get errors at the using CERTSTORE_ptr ... line. The errors:
test.cpp(18): error C2873: 'CERTSTORE_ptr' : symbol cannot be used in a using-declaration
test\certstoreexporttest.cpp(18): error C2513: 'int' : no variable declared before '='
test\certstoreexporttest.cpp(18): error C2065: 'CERTSTORE' : undeclared identifier
...
The problem appears to be with CERTSTORE and HCERTSTORE, which is typedef'd as:
typedef void *HCERTSTORE;
How do I declare the using statement so I can enjoy the automatic cleanup? (I'm trying to avoid the __try\__finally).
Or is this simply the wrong approach and should be abandoned? (I'm ready to go back to __try\__finally so I can finish up this test program).

Related

C4430 and C2146 Visual Studio errors

I have searched through many posts on here and cannot seem to locate a solution to my problem. I am getting two errors when I try to compile my program, both of them are coming from one of my header files. Here are the errors:
Error 1 error C2146: syntax error : missing ';' before identifier 'datastore'
AND
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
In my cpp file I have scope resolution operators and I don't have any squiggly red lines under anything. Also the program compiled ONCE and then I saved it and reopened the program and it gave me these errors. So I think I originally "tricked" the compiler or something weird. So any help would be awesome!
#ifndef INTERNET_H
#define INTERNET_H
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sstream>
#include "Wininet.h"
#include "Internet.h"
#include "ForexPrices.h"
using namespace std;
class Internet
{
private:
ForexPrices datastore;
BOOL bResult;
char *chPtr0,
*chPtr1,
*chPtr2;
DWORD dw1,
dw2,
dwIndex;
HINTERNET hInet, hRequest;
HINTERNET h_Inet;
char ch_Buffer[4096],
ch_Line[256];
std::ofstream of_OutFile;
public:
Internet();
void openFile();
void internetCheckConnection();
HINTERNET internetopen();
HINTERNET internetconnect();
void internetclose();
void closeFile();
char* grabMargin();
double grabDailyAverageLine();
void setcurrency(char *currencyfiller1);
};
#endif
[error C2146: syntax error : missing ';' before identifier 'datastore'] is a hint that the class before 'datastore' is unknown, which leads to your next error.
[error C4430: missing type specifier - int assumed. Note: C++ does not support default-int] comes as a result of the first error. Because the compiler doesn't know what your ForexPrices class is, it is trying to use something else (I'm no expert on default-int). This is not supported and so you see this error instead.
For some reason your ForexPrices class is unknown. I see that you included the file above, ForexPrices.h. I would make sure that the name of your class is exactly the same in your header file as it is used here. Also make sure it isn't declared in a namespace that you haven't included. If so, you'll need another using statement or reference the class in the namespace (YourNamespace::ForexPrices). It's good practice not too always trust the "squigglies" I think. Visual studio can sometimes goof at least until your solution is fully parsed, but this is more of a problem on very large projects where parsing takes some time.

Why the compiler outputs the following error codes: C2143, C2238, C2501

C2143: syntax error : missing ';' before '<'
I might be quite rusty in C++ since I sincerely don't know the reason of such errors.
The code is actually quite simple. (VS2003)
#include <vector>
class store
{
public:
vector<int>storage;
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Because you need to add std:: in front of vector:
std::vector<int>storage;
The vector class is inside the std namespace.
Or just add
using namespace std;
which is highly NOT recommended, especially for header files.

Compiler error with list<string>

I'm trying to create a list of strings, following the example here. This below gives me syntax errors:
private: list<string> images;
The errors (all on the line where the above declaration is):
syntax error : missing ';' before '<'
missing type specifier - int assumed. Note: C++ does not support default-int
unexpected token(s) preceding ';'
It's in a class with only a single constructor besides it, and it compiles fine without it. What am I doing wrong?
Did you #include both <list> and <string>? Also, did you import the names list and string from namespace std by writing either
using namespace std;
or
using std::list; using std::string;
The error you're getting is consistent with the names not being accessible, so this is my best guess.
EDIT: Since this is in a header file, you should not be using either of the above constructs (thanks to wilhelmtell for pointing out that this is a header file!). Instead, you should fully-qualify the names as
private: std::list<std::string> images;
This way the compiler knows exactly where to look for list and string.
You need to qualify the list and string types with their namespace.
Either type std::list<std::string> or add using namespace std; after the #include <string> and #include <list> directives.
A simple working program:
#include <list>
#include <string>
using namespace std;
int main ( int, char ** )
{
list<string> strings;
strings.push_back("1st string");
}

compiling error with vector in c++

I'm stuck! I have this very simple test code and I can't get it to compile! I have used the same code many times before but now it won't work!
I have this simple program
#include <vector>
#include <iostream>
#include "Rswap.h"
using namespace std;
int main(){
Rswap test();
cin.get();
return 0;}
And then the rswap.cpp...
#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
V.push_back(9);
};
And then the rswap.h...
#ifndef Rswap_h
#define Rswap_h
class Rswap{
public:
vector<int>V;
Rswap();
};
#endif
I'm using Visual studio 2008. Is there something wrong that is obvious and I'm missing or what could it be! As I said I have used this snippet on several differnet occassions and I can't find any difference between this and those that work...
And i have tried both
vector < int > V; and vector <int> V; without any luck
I have stared at this blindly now for some while so I thought it's better to ask here!
rswap.h(7) : error C2143: syntax error : missing ';' before '<'
rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
rswap.h(7) : error C2238: unexpected token(s) preceding ';'
At the point you #include "Rswap.h", you haven't declared using namespace std; yet, so the reference to vector in Rswap.h must be qualified with a namespace. Simply declare the vector with a namespace prefix.
class Rswap{
public:
std::vector<int>V;
Rswap();
};
Also, I suggest you #include <vector> from Rswap.h rather than relying on whoever uses Rswap.h to get the #include order right.
It should be
std::vector<int>V;
Ah, your using namespace std comes too late - it should be inserted BEFORE the rswap.h include. In any case, it's MUCH better to write std::vector instead.
You need to #include <vector> in rswap.h.

C++ Use of undeclared identifier 'make_unique'

I am keep on getting the following error when I build,
use of undeclared identifier 'make_unique'
m_planet = make_unique();
My Header File which gives out the error,
#include "planet.h"
#include <memory>
using namespace std;
class PlanetBuilder
{
public:
PlanetBuilder();
virtual ~PlanetBuilder();
void createNewPlanetProduct() {
m_planet = make_unique<Planet>();
}
protected:
unique_ptr<Planet> m_planet;
};
#endif // PLANETBUILDER_H
I am running QtCreator 3.6.0 , tried on both Mac and Windows platforms and the error is consistent.. where am I going wrong?
make_unique requires #include <memory>
But since you have done that, I would think you are not using C++14 or above.
Try adding following flag on your make file.
-std=c++14
For current QT which is 5.7 I'm using a #include .
By following a link bellow you find out more. QScopedPointer link