C++ D3DXVECTOR3 insertion operator

Started by
1 comment, last by Mko000 10 years, 4 months ago

Hi guys. New here and I couldn't find a solution to my problem in older topics...

I want to take the 3 vectors describing my models (position, scale, rotation) and save them to a text file. Essentially create a map. I designate my models with integers but that is not important.
I have a struct ModelData for that: one int for model type and three D3DXVECTOR3. I pull it from all the models, create a vector and send it to my savemap method.

So I defined an insertion operator for the ModelData, and also for D3DXVECTOR3.
But I am not getting the correct output. For some reason the D3DXVECTOR3 operator is not being used. By tracing I found that it is being casted to float*.

Any ideas what I am doing wrong? Here are the code parts:




#pragma once
struct ModelData {
   int type;
   D3DXVECTOR3 position, size, rotation;
}; 
 
std::ostream& operator<<(std::ostream& os, const D3DXVECTOR3& vector) {
   os << vector.x << 'x' << vector.y << 'x' << vector.z;
   return os;
}

std::ostream& operator<<(std::ostream& os, const ModelData& data) {
   os << data.type << ' ' << data.position << ' ' << data.rotation << ' ' << data.size << std::endl;
   return os;
}

Like this I get what I am pretty sure is the address in HEX of data.position.x, data.rotation.x, and data.size.x as output. If I dereference data.position and so on, I get the integer part of the first float (x).

Advertisement
Your code, as posted, works for me on MSVC 2012.

#include <iostream>
#include <d3dx9.h>

struct ModelData {
   int type;
   D3DXVECTOR3 position, size, rotation;
}; 
 
std::ostream& operator<<(std::ostream& os, const D3DXVECTOR3& vector) {
   os << vector.x << 'x' << vector.y << 'x' << vector.z;
   return os;
}

std::ostream& operator<<(std::ostream& os, const ModelData& data) {
   os << data.type << ' ' << data.position << ' ' << data.rotation << ' ' << data.size << std::endl;
   return os;
}

int main(int, char **) {
  ModelData m = { 0, D3DXVECTOR3(1, 1, 1), D3DXVECTOR3(2, 2, 2), D3DXVECTOR3(3, 3, 3)};
  std::cout << m;

  return 0;
}
This produces "0 1x1x1 3x3x3 2x2x2" as output. Try to produce a minimal code example that demonstrates your problem.

Thank you

You are right, after creating the topic I ran it and it worked just fine. Must have fixed the problem while editing the code for this topic. Maybe it was because I was trying to write the operator for ofstream, and then changed it to ostream. I'm not sure which change made it work.

Looks like I just messed something up.

This topic is closed to new replies.

Advertisement