API Question #define ORBITER_MODULE, C2440 error

n72.75

Addon Developer
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,259
Reaction score
786
Points
128
Location
Biddeford ME
Website
mwhume.space
Preferred Pronouns
he/him
I've been bitten by the addon dev bug again recently and I'm getting back into the Orbiter API and C++.

I'm having trouble with the very simple bit of code below.

Code:
#define STRICT
#define ORBITER_MODULE

#include <OrbiterSDK.h>

DLLCLBK void opcPreStep(double simt, double simdt, double mjd)
{
	sprintf(oapiDebugString(), "%.10f", oapiGetSimStep());
}

The compiler gives me this error:
Code:
1>------ Build started: Project: Moth, Configuration: Debug Win32 ------
1>moth.cpp
1>c:\orbiter\orbiter2016\orbitersdk\include\orbiterapi.h(7227): error C2440: 'return': cannot convert from 'const char [12]' to 'char *'
1>c:\orbiter\orbiter2016\orbitersdk\include\orbiterapi.h(7227): note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>Done building project "Moth.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

when I try to compile it, which doesn't make sense to me.

If I remove the
Code:
#define ORBITER_MODULE
line it will compile and orbiter will load it, and it does exactly what I want.

My question is, why??
 

LeePalmer

Member
Joined
Apr 7, 2013
Messages
40
Reaction score
0
Points
6
Hi.

The oapiDebugString() returns 'const char [12]'.
The sprintf() bit is attempting to convert it. to *char , a char pointer type.

Try:
{
*char x;
x= oapiDebugString();
sprintf(x, "%.10f", oapiGetSimStep());
}


Cheers
 

LeePalmer

Member
Joined
Apr 7, 2013
Messages
40
Reaction score
0
Points
6
Of course, its the STRICT thats giving the error.
sprint() expects a *char type. Its not getting that type.
the conversion is done by returning the const value to x first.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
36,825
Reaction score
1,487
Points
203
Location
Langendernbach
Of course, its the STRICT thats giving the error.
sprint() expects a *char type. Its not getting that type.
the conversion is done by returning the const value to x first.

Yes, but I think you are at the wrong line for the error.

The const char [12] is the formatted string. "%.10f" fits perfectly there (11 + \0).

oapiDebugStr() is also a fixed length buffer, but its more than 12.

---------- Post added at 13:51 ---------- Previous post was at 13:01 ----------

EDIT: Can somebody get me the lines 7217 - 7237 in "orbiterapi.h"
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
4,874
Reaction score
1,445
Points
138
EDIT: Can somebody get me the lines 7217 - 7237 in "orbiterapi.h"

from line 7211 to 7230 (EOF) (file corresponding to the Orbiter 2016 release)
Code:
inline VECTOR3 POINTERTOREF (VECTOR3 *p)
{
	VECTOR3 v;
	v.x = DBL_MAX;            // flag
	*((VECTOR3**)&v.z) = p;   // address
	v.z = 0.0;
	return v;
}

// ======================================================================
// Internal data structures
// ======================================================================

#ifdef ORBITER_MODULE
void dummy();
void calldummy () { dummy(); }
DLLCLBK char *ModuleDate () { return __DATE__; }
#endif

#endif // !__ORBITERAPI_H


and for the Orbiter BETA version, from line 7207 to 7233
Code:
/**
 * \ingroup vec
 * \brief Construct a rotation matrix from an axis and an angle
 * \param axis rotation axis direction (must be normalised)
 * \param angle rotation angle [rad]
 * \return rotation matrix
 */
inline MATRIX3 rotm (const VECTOR3 &axis, double angle)
{
	double c = cos(angle), s = sin(angle);
	double t = 1-c, x = axis.x, y = axis.y, z = axis.z;

	return _M(t*x*x+c, t*x*y-z*s, t*x*z+y*s,
		      t*x*y+z*s, t*y*y+c, t*y*z-x*s,
			  t*x*z-y*s, t*y*z+x*s, t*z*z+c);
}

inline VECTOR4 _V(double x, double y, double z, double w)
{
	VECTOR4 vec = {x,y,z,w}; return vec;
}

inline VECTOR4 _V(const VECTOR3 &v)
{
	VECTOR4 vec = {v.x, v.y, v.z, 1.0};
	return vec;
}
 
Last edited:

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
36,825
Reaction score
1,487
Points
203
Location
Langendernbach
So the ModuleDate() function collides with STRICT because ___DATE___ is a constant string literal.
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,399
Reaction score
311
Points
83
Website
orbit.medphys.ucl.ac.uk
I realise that ModuleDate() should return a const char*, but if I change that it will probably break every single addon.

What about a dirty hack of casting away the const from __DATE__?

return (char*)__DATE__;

Would that even compile? Would it get rid of the error? Is it too nasty to contemplate?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
36,825
Reaction score
1,487
Points
203
Location
Langendernbach
Well, one option would be using a "const_cast<char*>(__DATE__)" there.

Since the data referenced by ModuleDate() is never overwritten, it should work without side effects.

Cleanest solution:

Code:
char* ModuleDate() {
    static char buffer[64];
    strcpy(buffer, __DATE__);
    return buffer;
}

But likely also the most inefficient one.
 
Last edited:

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,349
Reaction score
506
Points
153
Location
Vienna
The strange thing is: I used those defines and oapiDebugString-call just recently for the sound bridge code and got no compiler error at all. This is with Visual Studio 2015. Is the OP perhaps using a different compiler? Or beta versions of the libs?
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
4,874
Reaction score
1,445
Points
138
Just checked and SSU has "#define STRICT 1", while the OP has "#define STRICT"...
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,349
Reaction score
506
Points
153
Location
Vienna
I can reproduce the problem if I add the compiler option "/Zc:strictStrings" to the project, just as described in the MSDN help for error C2440:
Code:
1>S:\Development\Orbiter2016\Orbitersdk\include\OrbiterAPI.h(7227): error C2440: 'return': cannot convert from 'const char [12]' to 'char *'
 

n72.75

Addon Developer
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,259
Reaction score
786
Points
128
Location
Biddeford ME
Website
mwhume.space
Preferred Pronouns
he/him
The strange thing is: I used those defines and oapiDebugString-call just recently for the sound bridge code and got no compiler error at all. This is with Visual Studio 2015. Is the OP perhaps using a different compiler? Or beta versions of the libs?

VS2017, and the standard 2016 release version of orbiterapi.
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,349
Reaction score
506
Points
153
Location
Vienna
VS2017, and the standard 2016 release version of orbiterapi.

So perhaps 2017 is adding the above mentioned check automatically, and you have to set a switch that deactivates it.
 

n72.75

Addon Developer
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,259
Reaction score
786
Points
128
Location
Biddeford ME
Website
mwhume.space
Preferred Pronouns
he/him
So perhaps 2017 is adding the above mentioned check automatically, and you have to set a switch that deactivates it.

I'll poke around when I get home tonight.

---------- Post added at 06:50 PM ---------- Previous post was at 11:42 AM ----------

Yeah, adding: "/Zc:strictStrings-" option fixes the error.
 

n72.75

Addon Developer
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,259
Reaction score
786
Points
128
Location
Biddeford ME
Website
mwhume.space
Preferred Pronouns
he/him
So this is still a thing. It's always fun searching for an answer and finding your own thread, lol.
 
Top