Does anyone know why it happens? C++ 'initializing': cannot convert from - c++

So this is my header to a class:
#include <iostream>
class Board
{
public:
Board();
AllPieces* GetBoard();
void createBoard();
protected:
AllPieces** everything= new (AllPieces**)[32];
};
But when I try to initialize the 'everything' array, it says this error:
Error C2440 'initializing': cannot convert from 'AllPieces ***' to 'AllPieces **'
I don't know why it says that, what I want to do is to make an array of pointers from the class AllPieces.
Can anybody help me please? thank you

The code new (AllPieces**)[32] creates a pointer to an object of type AllPieces**, which thus has a type of AllPieces***. If you drop one of the *, you will get the correct return type.
To eliminate the error, change the line in question to:
AllPieces** everything = new AllPieces*[32];

Related

Unit testing a vector string

I have this really simple line of code in my production-code(A.cpp) as follows:
std::string A::getString(int i) {
return sVect_[i];
}
with the header as follows:
class A{
public:
std::string getString(int i);
...
private:
vector<std::string> sVect_;
...
};
I've been trying to test the getString() function using googletest but an error keeps popping out:
error: invalid conversion from 'char* (*)(const char*, int)throw ()' to 'int'
error: initializing argument 1 of 'std::string A::getString(i)'
This was my test program:
TEST(ATest, getString){
A a;
EXPECT_EQ("c", a.getString(i));
}
I couldn't quite grasp the workaround of the vector string and how to call it in my test program without ever changing the production code. I even use the hack, adding #define statements, to access the private member but still couldn't do it.
How do my test actually looks like to successfully call that function?
Note: I'm on Linux and using gcc. Thank you in advance guys.
Perhaps the error message is misleading. Have you defined i globally somewhere else? To me it looks like in the local scope because it does not know what the value of the variable i is, it is misbehaving in an unexpected way
TEST(ATest, getString){
A a;
EXPECT_EQ("c", a.getString(i)); //here what is the 'i' and where is it defined
}

gluTessCallback error C2440

I am trying to use the function gluTessCallback but I get C2440 error. I have no idea why.
Here is the code:
#define callback void(CALLBACK*)()
template<typename T>
class Tessellation
{
private:
GLUtesselator *pTess;
void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
public:
void Triangulation3D(T* & point, short numOfPoints)
{
pTess = gluNewTess();
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)tessError);
}
};
The error is on gluTessCallback function:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'void (__stdcall *)(void)'
Why do I get this compile error?
The error id C2440 on Visual Studio is a type conversion error.
The problem in your code is that you are trying to pass a class method Tessellation::tessError() as function pointer to gluTessCallback(), which expects a pointer to a global C-style function.
A class method is very different from a free/global function and you cannot pass it as a simple function pointer because it needs an object to go along with it every time, the this pointer.
A solution to your problem would be to declare tessError() as a static method, making it effectively the same as a free/global function scoped inside the class, like so:
template<typename T>
class Tessellation
{
private:
static void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
...
And pass it to gluTessCallback():
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)&Tessellation<T>::tessError);
The only downside to this approach is that a static tessError() can no longer access class variables. Which doesn't seem like a problem to you, since it is not doing so right now, it appears.

Converting Cocos2d code to ARC issues

I am trying to ARC enable a project and I am having a few issues when selecting files for ARC.
In the Ball class, the following line,
ballBody->SetUserData(self);
gives the error,
Cannot initialize a parameter of type 'void *' with an Ivalue of type 'Ball *const__strong'
In the Enemy.mm class, the following line,
enemyBody->SetUserData(enemySprite);
gives the error,
Cannot initialize a parameter of type 'void *' with an Ivalue of type 'CCPhysicsSprite*__strong'
In Enemy.h I have defined the above as:
b2Body* enemyBody;
CCPhysicsSprite* enemySprite; (in Enemy.m)
How can I solve these issues?
Bridge casting:
ballBody->SetUserData((__bridge void*)self);
enemyBody->SetUserData((__bridge void*)enemySprite);
and the reverse:
CCPhysicsSprite* enemySprite = (__bridge CCPhysicsSprite*)enemyBody->GetUserData();

c++ defining a member-function pointer without knowing the type of the object

I know that the title is not very clear but I didn't know how to write it down in one sentence. So the problem is that I want something like this:
void(typeof(this)::*function)(int,int);
I know that is not going to work but I was wandering whether a solution exists for this problem in c++ or not?
Update:
class MainPage
{
public:
MainPage()
{
void (std::remove_reference<decltype(*this)>::*callback)(int, int) = &MainPage::myFunction;
((*this).*callback)(nullptr,nullptr);
}
~MainPage()
{
}
void myFunction(int a, int b)
{
}
}
Errors:
error C2440: 'newline' : cannot convert from 'MainPage *' to 'std::remove_reference<_Ty> *'
error C2647: '.*' : cannot dereference a 'void (__thiscall std::remove_reference<_Ty>::* )(int,int)' on a 'MainPage'
Yes, use decltype:
void (std::remove_reference<decltype(*this)>::type::*function)(int, int);

SQLite3 object not understood?

Now I'm geting an error:
1>c:\development\document_manager\document_manager\storage_manager.h(7) : error C2079: 'storage_manager::db' uses undefined struct 'sqlite3'
with
#pragma once
#include "sqlite3.h"
class storage_manager
{
sqlite3 db;
sqlite3** db_pp;
public:
void open()
{
sqlite3_open("data.db", db_pp);
}
};
Old Question:
Hi everyone. I downloaded sqlite-amalgamation-3_6_13.zip from http://www.sqlite.org/download.html, but I'm not able to compile it in my project. I receive many errors like:
c:\pathtoproject\sqlite3.c(11337) : error C2440: '=' : cannot convert from 'void *' to 'char *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
c:\pathtoproject\sqlite3.c(12023) : error C2440: '=' : cannot convert from 'void *' to 'sqlite3_int64 *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
What do I need to do to compile my project properly? Thanks!
Edit:
I don't want to compile the whole program as C, I just want to compile three files as c, is this possible?
EDIT: FIXED! I created an new project.
It looks like you might be trying to compile a C program using a C++ compiler. While there is a lot of C code which is also valid C++, they are different languages.
Your compiler may have some switch or setting to compile C code. Check your compiler documentation.
You need to compile the file as C code rather than C++.
Right click on either the project or just the .c file, and in properties, make sure it is set to compile as C, rather than C++. (You may want to set this setting just for the file, not the entire project)
Doesn't the compiler tell you what to do? You need an explicit cast:
void *pv = /* some value */;
char *pc = (char*) pv;
This is of course not a problem in C, but an issue of C++.