Click here to Skip to main content
15,077,265 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
No matter what I try I get errors. I tried both in MSVS 2019 and Eclipse. Here's what I have:

Backend.h
C++
#pragma once

#include "Vec3.h"

namespace Interop
{
	extern "C"
	{
	//...	

		struct Vector3
		{
			float x;
			float y;
			float z;
		};
	}
}


Vec3.h
C++
#pragma once

#include "Backend.h"

typedef long long llong;

// I'm thinking that reincluding "Backend.h" both in Vec.h and Vec.cpp is bad so
// instead, I tried to forward declare Vector3 and for this I needed to forward
// declare the namespace, so:
namespace Interop{struct Vector3;}
// also tried
namespace Interop{}
struct Interop::Vector3; // ERROR "'Vector3' in namespace 'Interop' does not name a type"

namespace Framework
{
	struct Vec3
	{
	public:
		llong x;
		llong y;
		llong z;
		//...

		// type-cast:
		operator Interop::Vector3(); // ERROR: "expected type-specifier"

		Vec3 operator+(Vec3 const& vRight);
		//...
	};
}


Vec3.cpp
C++
#include "Vec3.h"
#include "Backend.h"

typedef long long llong;

namespace Framework
{
	Vec3::operator Interop::Vector3() // ERROR "no declaration matches 'Framework::Vec3::operator Interop::Vector3()'"
	{

	}

	Vec3 Vec3::operator+(Vec3 const& vRight)
	{
		return Vec3(x + vRight.x, y + vRight.y, z + vRight.z);
	}
}


What I have tried:

So I tried forward declarations:
C++
namespace Interop{struct Vector3;}
// also tried
namespace Interop{}
struct Interop::Vector3; // ERROR "'Vector3' in namespace 'Interop' does not name a type"


Although not good to include a header both in a h. and cpp., I ultimately tried to do it, so in Vec3.h I tried #include "Backend.h".

EDIT:
So overloading + operator works without a problem but conversion operator doesn't.
I've searched on Google but no success so far, as my case has in addition namespaces. Moreover examples out there don't present the conversion operator split into header and implementation files.
So it's a matter of syntax, I mean it must be a standard (I'd say unique) way of writing it. What is it?
Posted
Updated 26-Feb-21 0:57am
v4
Comments
Greg Utas 25-Feb-21 13:39pm
   
You say you're have trouble writing a cast, but I don't see one, though it looks like you're trying to define a conversion operator. You're trying to define Vector3 and Vec3, where one of them uses llong and the other uses float. Mixing those two types isn't going to work very well, because the llong will drop any fractional part.
amirea 25-Feb-21 13:45pm
   
I must got them confused. I thought they were the same thing.
Greg Utas 25-Feb-21 13:49pm
   
An llong is a 64-bit integer, and a float is a 32-bit floating point. There's also double, which is a 64-bit floating point, for greater accuracy.

See https://en.cppreference.com/w/cpp/language/types.
amirea 25-Feb-21 13:53pm
   
I know the types, I chose to use integer instead of floating point for precision and consistency reasons. And thank you for pointing that out, there's never too much or too obvious information.
Greg Utas 25-Feb-21 13:45pm
   
You're also #including Vec3.h in Backend.h, and Backend.h in Vec3.h! I've never tried such a thing but have to believe this kind of loop will cause problems. I wouldn't worry about your .h and .cpp layout for now. Just put everything in a .cpp and get it working before you worry about how to organize the code.
amirea 25-Feb-21 13:49pm
   
Thank you for taking a look at the code. I don't want to just get things done. I want to learn the proper way to do it. Learn once, use it forever :)

See user-defined conversion function[^]. That's a great site, by the way. My go-to for obscure C++ information.

So if you're trying to convert between Vec3 and Vector3, it would be something like this:
C++
struct Vec3
{
   operator Vec3(const Vector3& v);  // constructor that converts v to a Vec3
   operator Vector3();               // operator that converts a Vec3 to a Vector3
   // other members
};

struct Vector3
{
   operator Vector3(const Vec3& v);  // constructor that converts v to a Vector3
   operator Vec3();                  // operator that converts a Vector3 to a Vec3
   // other members
};

v1a Vec3(<args1>);
v2a Vector3(<args2>);
v1b Vec3(v2a);
v2b Vector3(v1a);
And now each vector has both versions.

EDIT: Those were converting constructors. If you want a conversion operator, it has to go in the class being converted. I've edited the above to show them. You can make any of the converters explicit if you have problems with unintended conversions, but I don't think that will happen here. It's more likely when providing conversion to basic types, like bool.

From the above,
v1a = v2a;
should invoke the conversion operator Vector3::operator Vec3.
   
v6
Comments
CPallini 26-Feb-21 2:23am
   
5.
amirea 26-Feb-21 3:54am
   
Thanks. There's a problem though, all information and examples on custom conversion function I found on Google, including that at the link you provided don't involve namespaces. And they don't present it split into header and implementation files.
_This_ apparently makes the difference in my case.
Greg Utas 26-Feb-21 6:40am
   
It can be split into headers and implementation files, and namespaces, in the same way as any other C++ code. See below.
C++
// Vector3.h
//
namespace Framework
{
   struct Vec3;  // forward declaration
}

using Framework::Vec3;  // now allows "Vec3" instead of "Framework::Vec3"

// Vector3 stuff from above
C++
// Vector3.cpp
//
#include Vector3.h
#include Vec3.h
using namespace Framework;  // now don't have to prefix "Framework" to anything

// implementation of Vector3 functions
C++
// Vec3.h
//
namespace Interop
{
   struct Vector3;  // forward declaration
}

using Interop::Vector3;  // now allows "Vector3" instead of "Interop::Vector3"

// Vec3 stuff from above
C++
// Vec3.cpp
//
#include Vec3.h
#include Vector3.h
using namespace Interop;  // now don't have to prefix "Interop" to anything

// implementation of Vector3 functions
This still leaves your extern "C" thing, which isn't something I've had to do, so you'll have to look for information about it elsewhere.
   
v3
Comments
amirea 1-Mar-21 8:56am
   
Thank you for sticking around and helping me! It turned out that my code was valid but it took me 1.5 days to figure out that the errors are a bug of the IDE's. After building with the errors still there, the errors were gone.
It didn't occur to me but I found this:

https://stackoverflow.com/questions/3015624/how-to-declare-type-conversion-in-header-file-and-implement-in-cpp-file

and please see the op comment saying it worked with clean project:

https://stackoverflow.com/questions/3015624/how-to-declare-type-conversion-in-header-file-and-implement-in-cpp-file#comment3082516_3015624

and I admit I had my reserves, I tried clean -- didn't work, then built and that solved it. Still, that's a shame... What would happen if the project would need 5 minutes of build time? Imagine building at every error :)

Back to your code: it is clearer than what I wrote and I'll go with that. Thanks again.
Greg Utas 1-Mar-21 9:26am
   
Thanks for the follow-up. Sorry I didn't think of the const!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900