Sign in to follow this  

operator and ofstream

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
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
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
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

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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