# operator and ofstream

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by BregmaA 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 on other sites
It's irrelevant to your issue at hand, but you should if possible make the reference to the object you're outputting a reference-to-const. If not, you cannot print temporaries and other rvalues.

##### Share on other sites
Irrelevant, but interesting :) Didn't know that, thx !

##### Share on other sites
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;}

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 on other sites
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 on other sites
You should probably base your operators on std::ostream, not std::ofstream.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627654
• Total Posts
2978451

• 10
• 12
• 22
• 13
• 33