Jump to content
  • Advertisement
Sign in to follow this  
Mko000

C++ D3DXVECTOR3 insertion operator

This topic is 2135 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 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).

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!