1

So I am getting back into graphics programming, and the book I'm using for reference (Frank Luna's 3D Game Programming with DirectX 11), uses no-longer-supported xnamath.h. I have changed it to use DirectXMath.h seemingly without any problems. However, there seems to be problem when I overload the ostream << operator to work with XMVECTORs. When I try to use cout to print an XMVECTOR object, I get this error:

Error   1   error C2280: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)' : attempting to reference a deleted function

There is only one file in this program (main.cpp):

#include <Windows.h>
#include <DirectXMath.h>
#include <iostream>

using namespace DirectX;
using namespace std;
// Overload the "<<" operators so that we can use cout to output XMVECTOR objects
ostream& operator<<(ostream os, FXMVECTOR v); 

int main() {
    cout.setf(ios_base::boolalpha);

        // Check support for SSE2 (Pentium4, AMD K8, and above
        if (!XMVerifyCPUSupport()) {
        cout << "DirectX Math not supported" << endl;
        return 0;
    }

    XMVECTOR n = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
    XMVECTOR u = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
    XMVECTOR v = XMVectorSet(-2.0f, 1.0f, -3.0f, 0.0f);
    XMVECTOR w = XMVectorSet(0.707f, 0.707f, 0.0f, 0.0f);

    // Vector addition: XMVECTOR operator +
    XMVECTOR a = u + v;


    cout << a;
} 

ostream& operator<<(ostream os, FXMVECTOR v) {
    XMFLOAT3 dest;
    XMStoreFloat3(&dest, v);

    os << "(" << dest.x << ", " << dest.y << ", " << dest.z << ")";
    return os;
}

I have a feeling I'm messing up the operator-overloading, but I'm not really sure at this point. I couldn't find any similar problems online, but I hope I'm just overlooking something really basic. Any advice will be appreciated.

jiro
  • 31
  • 2
  • 1
    Streams cannot be copied. You're also trying to return a reference to a local variable. A quick glance at [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) would provide a correct signature. – chris Jun 14 '14 at 00:08

1 Answers1

6

The problem is in this line:

ostream& operator<<(ostream os, FXMVECTOR v) {

Change it to

ostream& operator<<(ostream& os, FXMVECTOR v) {

The first line tries to make a copy of ostream using a copy constructor. That is not allowed.

R Sahu
  • 204,454
  • 14
  • 159
  • 270