Sign in to follow this  
paic

operator and ofstream

Recommended Posts

paic    645
Hi, I've got a little problem with ofstream and operator <<. In my program, I define a set of operator << to make writing to files a little easier. For example :
std::ofstream & operator << (std::ofstream & stream, D3DXVECTOR3 & value)
{
    stream << value.x << " " << value.y << " " << value.z;
    return(stream);
}
This works fine when I write something like :
D3DXVECTOR3     vect(0.0f, 0.0f, 0.0f);
std::ofstream  file("something.txt");
file << vect << "\n";
But I then I added the same kind of operator to handle D3DVECTOR. D3DXVECTOR3 and D3DVECTOR are defined like that :
typedef struct _D3DVECTOR {
    float x;
    float y;
    float z;
} D3DVECTOR;

typedef struct D3DXVECTOR3 : public D3DVECTOR
{
    // blah blah ... just a few methods
} D3DXVECTOR3, *LPD3DXVECTOR3;
And each time I want to write a D3DVECTOR to my file, I get the error : "error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'D3DVECTOR' (or there is no acceptable conversion)" Why does D3DVECTOR3 works and not D3DVECTOR ??? Do you have any idea ? Is there something going on that I don't know and would cause such a problem ? Thx in advance for any help.

Share this post


Link to post
Share on other sites
Bregma    9200
A D3DVECTOR is not a D3DXVECTOR3. You have provided an overload of the stream insertion operator for a D3DXVECTOR3 but not a D3DVECTOR. You have not provided an operator that takes a right-hand operand of type D3DVECTOR.

The error message is not lying. It's telling you exactly what the problem is.


As an aside, you should write your operator to take a std::ostream&, not a std::ofstream&.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Untested, but if you implement operator << for D3DVECTOR3, the compiler should be able to accept a D3DXVECTOR3 too (Since a D3DXVECTOR3 is a derived D3DVECTOR3)

Share this post


Link to post
Share on other sites
paic    645
Quote:
Original post by Bregma
A D3DVECTOR is not a D3DXVECTOR3. You have provided an overload of the stream insertion operator for a D3DXVECTOR3 but not a D3DVECTOR. You have not provided an operator that takes a right-hand operand of type D3DVECTOR.


Quote of myself :
Quote:
But I then I added the same kind of operator to handle D3DVECTOR.


Here's the code I use (as it seems people only read code :s)

// in StreamUtils.h :
extern std::ofstream & operator << (std::ofstream & stream, D3DXVECTOR3 & value);
extern std::ofstream & operator << (std::ofstream & stream, D3DVECTOR & value);

// in StreamUtils.cpp :
std::ofstream & operator << (std::ofstream & stream, D3DXVECTOR3 & value)
{
stream << value.x << " " << value.y << " " << value.z;
return(stream);
}

std::ofstream & operator << (std::ofstream & stream, D3DVECTOR & value)
{
stream << value.x << " " << value.y << " " << value.z;
return(stream);
}




Now :


void AClass::Save(std::ofstream & file, )
{
// this line compiles and works
file << "D3DXVECTOR3 " << m_D3DVECTOR3_member_variable << "\n";

// this line throw the C2679 error
file << "D3DVECTOR " << m_D3DVECTOR_member_variable << "\n";
}



As a note, I have the same problem with the D3DCOLORVALUE type. I have a custom color structure almost the same as D3DCOLORVALUE, and the << operator I defined for my structure works, whereas the operator << for D3DCOLORVALUE is not recognized (throw a C2679 error if I try to send a D3DCOLORVALUE to the stream)

Any idea ??

Share this post


Link to post
Share on other sites
Aardvajk    13207
The following minimal program compiles and runs fine (Visual Studio Express 2008):


#include <fstream>

#include <d3d9.h>
#include <d3dx9.h>

std::ofstream & operator << (std::ofstream & stream, D3DXVECTOR3 & value)
{
stream << value.x << " " << value.y << " " << value.z;
return(stream);
}

std::ofstream & operator << (std::ofstream & stream, D3DVECTOR & value)
{
stream << value.x << " " << value.y << " " << value.z;
return(stream);
}

int main()
{
std::ofstream os("test.txt");

D3DXVECTOR3 x;
D3DVECTOR v;

os << x;
os << v;
}




The ofstream overload functions are copy-pasted direct from your source above.

So, I don't know what is wrong, but it is something other than what you have posted. Sorry this is not so helpful, but I thought it might be of use to confirm that the code you have posted is not at fault.

Share this post


Link to post
Share on other sites
paic    645
I must be doing something wrong with operator overloading somewhere else then ...
Thx, I didn't think about testing this simple code in another empty project ... I simply wrote all the components of my D3DVECTORs by hand and moved on to something else :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this