Passing unique pointer child as a reference - c++

VulkanSurface.h:
#include "VulkanInstance.h"
class VulkanSurface {
public:
//Only constructor
VulkanSurface(VulkanInstance &instance); //line 14
};
VulkanSurface.cpp:
VulkanSurface::VulkanSurface(VulkanInstance &instance) {
//...
}
VulkanInstance.cpp:
VulkanInstance::VulkanInstance(const std::vector<const char*> validationLayers) :
validationLayers(validationLayers) {
...
}
main.cpp:
instance = std::make_unique<VulkanInstance>(validationLayers);
surface = std::make_unique<VulkanSurface>(*instance); //line 104
This shows no errors in VisualStudio in form of red squiggly line, but the compilation fails with the following:
1>main.cpp
1>c:\users\karlovsky120\source\repos\vulkanproject\vulkanproject\vulkansurface.h(14): error C2061: syntax error: identifier 'VulkanInstance'
1>c:\users\karlovsky120\source\repos\vulkanproject\vulkanproject\main.cpp(104): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'VulkanSurface'
1>c:\users\karlovsky120\source\repos\vulkanproject\vulkanproject\main.cpp(104): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\karlovsky120\source\repos\vulkanproject\vulkanproject\main.cpp(104): error C2672: 'std::make_unique': no matching overloaded function found
1>VulkanInstance.cpp
1>c:\users\karlovsky120\source\repos\vulkanproject\vulkanproject\vulkansurface.h(14): error C2061: syntax error: identifier 'VulkanInstance'

Related

Qt Throwing Illegal Error at QVariant

I have some code that is meant to get a QList of QMaps from a set of data. Then it should go through all the QMaps in that list. For some reason upon attempting this I am getting some errors regarding the QVector used to store data. Here is my code:
#include <QCoreApplication>
#include <QMap>
#include <QVariant>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QMap<QString, QVariant>> maps;
QMap<QString, QVariant> item1;
item1.insert("Item 1", QVariant::fromValue(1));
maps.append(item1);
foreach (const QMap<QString, QVariant> & map, maps)
{
qDebug() << map;
}
return a.exec();
}
Error:
..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
..\ErrorTest\main.cpp(17): error C2275: 'QVariant': illegal use of this type as an expression
C:\Qt\5.8\msvc2015_64\include\QtCore/qsharedpointer_impl.h(94): note: see declaration of 'QVariant'
..\ErrorTest\main.cpp(17): error C2955: 'std::remove_reference': use of class template requires template argument list
D:\Microsoft Visual Studio 14.0\VC\INCLUDE\xtr1common(301): note: see declaration of 'std::remove_reference'
..\ErrorTest\main.cpp(17): error C2065: 'map': undeclared identifier
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ')' before '>'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '>'
..\ErrorTest\main.cpp(17): error C2065: '_container_': undeclared identifier
..\ErrorTest\main.cpp(17): error C2228: left of '.control' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.i' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.e' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2059: syntax error: ')'
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before 'for'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '='
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before '}'
..\ErrorTest\main.cpp(17): fatal error C1004: unexpected end-of-file found
Thank you, and yes the tagData->toMappedList() returns the correct set of QMaps/data.
Imho the problem is the , inside the template. Sadly foreach is nothing but a call for Q_FOREACH which is a macro. It requires to arguments separated by comma. But because of the template you have 2 commas. I had this problem a while ago, though Qt Creator at least provided me with error: macro "Q_FOREACH" passed 3 arguments, but takes just 2 instead of the ton of errors you got. In your case this would be:
..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
I would suggest a for loop with an iterator to traverse the list of maps. Unless you want to dump the const in which case you can do:
QMap<QString, QVariant> map;
foreach (map, maps) {
...
}
You can also use auto but again - no constantness.
If constantness is a critical aspect in your case, go for the for loop with a constant iterator.

Declaration of templated class member function returning a map

In VC++ 2015 I have an untemplated class which should have a templated memberfunction which returns a map.
Here is a bit of the code:
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
But the compiler throws error messages:
error C2988: Unrecognized template declaration/definition
error C2143: Syntax error: missing ";" before "<"
error C2238: Unexpected Token before ";"
error C2059: Syntax error: "<"
I assume my problem is in having to use a map where the wstring has to be fixed/untemplated but the second argument is my template class.
Of course i followed the compilers suggestions but that didn't get me any further.
You need to #include <map> and add a semi-colon to the end of your class declaration.
As #Kevin and #juanchopanza have pointed out in the comments, you are simply missing a semi-colon and potentially an include. With the following program in VS2013:
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
int main()
{
}
I get an assortment of the errors you listed:
error C2143 : syntax error : missing ';' before '<'
error C2238 : unexpected token(s) preceding ';'
error C2988 : unrecognizable template declaration / definition
error C2059 : syntax error : '<'
And also:
error C2039 : 'map' : is not a member of 'std'
Once I add an include for std::map, the errors are reduced:
#include <map>
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
error C2628 : 'Registry' followed by 'int' is illegal(did you forget a ';' ? )
Which suggests you are missing a semi-colon at the end of the class declaration.

Initializing object of a class inside another class

I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock.
Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock
{
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
{
TI1 = TI1_In; //in us
TI2 = TI2_In; //in us
pSBBList2 = pSBBList0;
//SeqBuildBlockIRns myIRns_3(pSBBList2); //Defined in libSBB.h
}
//~MySeqBuildBlockModule(){}
virtual bool prep (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo);
virtual bool run (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo, sSLICE_POS* pSLC);
protected:
private:
long TI1; //in us
long TI2; //in us
SBBList* pSBBList2;
SeqBuildBlockIRns myIRns_3(pSBBList2); // Line 106
};
bool MySeqBuildBlockModule::prep(MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo)
{
NLS_STATUS lStatus;
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest(); //Line 113
// Now we prepare:
lStatus = pSBBList->prepSBBAll(pMrProt, pSeqLim, pSeqExpo, &dEnergyAllSBBs_DK); // Line 116
if (lStatus ) {
cout << "DK: An error has occurred while preparing SBBs"<< endl;
}
return lStatus;
}
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
PS: I have marked the line no in the code that I am posting.
Any help would be appreciated.
Regards,
DK
Your problem is that the below is declaring a function:
SeqBuildBlockIRns myIRns_3(pSBBList2);
What you want to do is declare a member:
SeqBuildBlockIRns myIRns_3;
And then in your constructor construct it with the argument pSBBList2:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
myIRns_3(pSBBList2) // <- construct
{ }

Is this the correct way to write a function template which accepts a function pointer with a variable number of arguments?

I have the following code,
class ODBCFuncCall // Line 67
{
public:
ODBCFuncCall(SQLHANDLE stmt, SQLHANDLE conn) : m_stmt(stmt), m_conn(conn) {}
virtual ~ODBCFuncCall() {}
template <typename... A>
void RunStatementFunction(SQLRETURN (*in_func)(SQLHSTMT, A...), A... args)
{
in_func(m_stmt, args...);
}
virtual const char* GetFunctionName() = 0;
virtual void Setup() = 0;
virtual void Run() = 0;
virtual void Cleanup() = 0;
protected:
const SQLHANDLE m_stmt;
const SQLHANDLE m_conn;
};
which I'm trying to compile with VS2012 SP4. It's barfing on it completely, here's a sample of the errors I'm getting:
Error 6 error C2059: syntax error : ')' \ODBCTask.h 75
Error 4 error C2065: 'A' : undeclared identifier \ODBCTask.h 75
Error 2 error C2065: 'in_func' : undeclared identifier \ODBCTask.h 75
Error 5 error C2143: syntax error : missing ')' before '...' \ODBCTask.h 75
Error 1 error C2143: syntax error : missing ',' before '...' \ODBCTask.h 74
Error 7 error C2238: unexpected token(s) preceding ';' \ODBCTask.h 80
Error 3 error C2275: 'SQLHSTMT' : illegal use of this type as an expression \ODBCTask.h 75
It compiles fine when I comment out 'RunStatementFunction'.
The syntax seems to be correct to me, from what I can find by googling similar things...
Yes, this is correct: see it in action on ideone.
Variadic templates are only available in VS 2012 when using the November 2012 CTP. That introduced a separate toolset, which must be selected in the project properties. It should compile with that.
There are two issues I see. First vs2012 does not support variadic templates, outside of the experimental CTP which should not be used in production code
Second you should perfect forward the arguments to your function pointer.
template <typename... As,typename...Ts>
void RunStatementFunction(SQLRETURN (*in_func)(SQLHSTMT, As...), Ts&&... args)
{
in_func(m_stmt, std::forward<Ts>(args)...);
}
which should eliminate all overhead from the helper function in optimized builds.
vs2013 supports variadic templates.

new to CPP: Compiler error

Compilation fails with the following output.
Any thoughts please..
PStore.cpp
PStore.cpp(169) : error C2220: warning treated as error - no 'object' file generated
PStore.cpp(169) : warning C4091: '' : ignored on left of 'bool' when no variable is declared
PStore.cpp(169) : error C2143: syntax error : missing ';' before 'inline function header'
PStore.cpp(170) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
PStore.cpp(170) : error C2556: 'int PStore::getVersion(std::string &)' : overloaded function differs only by return type from 'bool PStore::getVersion(std::string &)'
../include\PStore.h(48) : see declaration of 'PStore::getVersion'
PStore.cpp(170) : error C2371: 'PStore::getVersion' : redefinition; different basic types
../include\PStore.h(48) : see declaration of 'PStore::getVersion'
Here is the code snippet:
bool PStore::getVersion(std::string& version)
{
AMPI_INFO("[API]");
return getVersionNoLogging(version);
}
bool PStore::getVersionNoLogging(std::string& version)
{
version = AMPI_PStore_VERSION " " __DATE__ " " __TIME__;
return true;
}
Please post your code so that all errors can be explained.
One of the errors is obvious however: you can't have two functions with the same parameters and the same name.
In your case you have int PStore::getVersion(std::string &) and bool PStore::getVersion(std::string &), which is not legal.
Either change the name of one of the functions, or change the type or number of parameters of one of the functions.