2014-06-14 55 views
1

所以我回到图形编程中,并且我用作参考的书(Frank Luna的DirectX 11 3D游戏编程)使用不再支持的xnamath.h。我已经改变它使用DirectXMath.h貌似没有任何问题。但是,当我将运算符过载到运算符XMVECTORs时,似乎有问题。当我尝试用COUT来打印XMVECTOR对象,我得到这个错误:运算符重载删除函数

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 

只有一个文件,这个程序(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; 
} 

我有一种感觉,我这让运营商负担过重,但我现在还不确定。我在网上找不到任何类似的问题,但我希望我只是忽略了一些基本的东西。任何建议将被认真考虑。

+1

流无法复制。你也试图返回一个局部变量的引用。快速浏览[运算符重载](http://stackoverflow.com/questions/4421706/operator-overloading)将提供正确的签名。 – chris

回答

6

的问题是在这条线:

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

将其更改为

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

第一行会尝试使用拷贝构造函数做的ostream副本。这是不允许的。

+0

啊,非常感谢,我甚至都没有想过要去那里看! – jiro

+0

@jiro欢迎您。很高兴我能够提供帮助。 –