C2440 compilation error - c++

I'm beginner. I'm using VS2008. In file1.cpp I'm calling the OALPAtoVA function present in file2.c. I'm getting C2440. I believe it is due to some confusion caused between C & C++ files during compilation.
file1.cpp
extern OMAP_MMCHS_REGS *m_pbRegisters;
m_pbRegisters = OALPAtoUA(AM33X_MMCHS1_REGS_PA); //error at this line
file_2.c
VOID* OALPAtoVA(UINT32 pa, BOOL cached)
{
//some code
return va;
}
The error I get is,
error C2440: '=' : cannot convert from 'void *' to 'OMAP_MMCHS_REGS *'
How to resolve this error?

Assuming the difference between OALPAtoUA and OALPAtoVA is just a typo in your question.
The problem is that unlike C, C++ doesn't provide an implicit conversion from void* to a different pointer type. So if your function returns a void*, you have to cast it explicitly:
m_pbRegisters = static_cast<OMAP_MMCHS_REGS*>(OALPAtoVA(AM33X_MMCHS1_REGS_PA));

Related

Compiler Error C2440 IMAGE_NT_HEADERS64 cant be used to initialize an entity of type IMAGE_NT_HEADERS

So im writing a .dll for an injection, i ran into this problem and i have no clue abt how to fix tiError C2440 'initializing': cannot convert from 'const IMAGE_NT_HEADERS64 *' to 'const IMAGE_NT_HEADERS *' mod C:\Users\user\source\repos\mod\mod\Pattern.cpp 21
the code im using here:
const IMAGE_NT_HEADERS* ntHeader = reinterpret_cast<const IMAGE_NT_HEADERS64*>(reinterpret_cast<const uint8_t*>(dosHeader) + dosHeader->e_lfanew);
How can i fix this?
You are trying to assign a const IMAGE_NT_HEADERS64* to a const IMAGE_NT_HEADERS*. They are not the same type.
Either cast the adjusted pointer to const IMAGE_NT_HEADERS*, or declare ntHeader as const IMAGE_NT_HEADERS64*.

C++ error C2664 with CStrings

I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!

GCC allowing implicit int to pointer conversion?

So I mistakenly passed an int into a function parameter expecting a pointer to a const object and I got this warning from GCC:
jni/../../../Source/Controller/UserExperienceManager.cpp: In member function 'void CUserExperienceManager::SendUXPToPOS(Types::CString)':
jni/../../../Source/Controller/UserExperienceManager.cpp:243:78: warning: invalid conversion from 'int' to 'const WebService_POSAgent::CDataCheck*' [-fpermissive]
jni/../../../../../Core/WebServices/Services/POSAgent/POSAgent.h:80:18: warning: initializing argument 2 of 'CEvent& WebService_POSAgent::CPOSAgent::AddUserExperienceID(Types::CString, const WebService_POSAgent::CDataCheck*, Types::CString)' [-fpermissive]
I tried to create a reproducible sample of this but I wasn't able to and I can't share too much of the code here. I compiled the same thing on MSVC9 and it properly gave me an error. I'm just in shock that I'm not getting an error from GCC on this! Anyone know why? Here are some simple snippets:
Function Declaration (class member function):
CEvent& AddUserExperienceID(CString userExperienceId, CDataCheck const* check, CString requestId = REQUEST_ID);
Function Call Site:
int nCheckNum = /*some value*/;
CPOSAgent::Instance().AddUserExperienceID(m_UserExperienceId, nCheckNum);
You're compiling that code with -fpermissive set which is downgrading the error to a warning.
-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings.
Thus, using -fpermissive will allow some nonconforming code to compile.
What does the fpermissive flag do?

C++ in Ruby C extensions, pointer problems

I'm tryign to build a Ruby C extension that uses some c++ libraries. Problem is I can't even get a simple "hello world" to work.
//hello_world.cpp
#include <ruby.h>
static VALUE tosCore;
static VALUE my_function( VALUE self )
{
VALUE str = rb_str_new2( "Hello World!" );
return str;
}
extern "C"
void Init_hello_world( void )
{
tosCore = rb_define_module("Core");
rb_define_module_function(tosCore, "my_method", my_function, 0);
}
The output I get is
compiling hello_world.cpp
hello_world.cpp: In function 'void Init_hello_world()':
hello_world.cpp:17:67: error: invalid conversion from 'VALUE (*)(VALUE) {aka lon
g unsigned int (*)(long unsigned int)}' to 'VALUE (*)(...) {aka long unsigned in
t (*)(...)}' [-fpermissive]
In file included from c:/Ruby200/include/ruby-2.0.0/ruby.h:33:0,
from hello_world.cpp:2:
c:/Ruby200/include/ruby-2.0.0/ruby/ruby.h:1291:6: error: initializing argument
3 of 'void rb_define_module_function(VALUE, const char*, VALUE (*)(...), int)'
[-fpermissive]
make: *** [hello_world.o] Error 1
I'm no C/C++ expert. Ruby is my language. I have compiled a few thousand lines of C++ under Rice with no problem, but since I want this particular extension to compile under Windows, Rice is not an option.
It's because the function callback you present to rb_define_module_function is not what the compiler expects. It want a function looking like this:
VALUE my_function(...)
But your function is
VALUE my_function( VALUE self )
Notice the difference in the argument list.
One way to get rid of the error, is to type cast the argument to the type that rb_define_module_function expects:
rb_define_module_function(tosCore, "my_method",
reinterpret_cast<VALUE(*)(...)>(my_function), 0);
You can read about reinterpret_cast here.

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++.