简体   繁体   English

错误 C2676 二进制“<<”:“std::ostream”未定义此运算符或转换为预定义运算符可接受的类型

[英]Error C2676 binary '<<': 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator

I don't understand the compilation error C2676我不明白编译错误 C2676

for the below code对于下面的代码

#ifndef __VEC_3D_H__
#define __VEC_3D_H__

#include <vector>
#include <cmath>

namespace Internal
{
    /** very simple 3D vector/ point */
    class Vec3D
    {
    public:
        float mX;
        float mY;
        float mZ;

        /// null constructor
        Vec3D(void) {}

        /// construct from data
        Vec3D(float x, float y, float z) : mX(x), mY(y), mZ(z) {}

        inline friend std::ostream& operator<< (std::ostream& os, const Vec3D& v) 
        {
            os << "(" << v.mX << ", " << v.mY << ", " << v.mZ << ")";
            return os;
        }
    };

}

#endif

I have put a functionally identical code in another class and it compiles and runs fine.我在另一个类中放置了一个功能相同的代码,它编译并运行良好。 What is wrong here?这里有什么问题?

EDIT1: corrected BOBVec3d to Vec3D, was a typo EDIT1:将 BOBVec3d 更正为 Vec3D,是一个错字

EDIT2: removed using namespace Internal; EDIT2: using namespace Internal;删除using namespace Internal; , it is indeed point-defeating to have it in a header file , 把它放在头文件中确实有点失败

missing #include <iostream> at the top.顶部缺少#include <iostream>

Fixed it.修复。 (Oh what terribly terribly poor compilation errors in C++ can be..) (哦,C++ 中非常糟糕的编译错误可能是......)

I had this error我有这个错误

Error C2676 binary '<<': 'std::ofstream' does not define this operator or a conversion to a type acceptable to the predefined operator Project2c++ c:\\users\\dinga\\desktop\\practice projects for c++\\project2c++\\project2c++\\fruit.h错误 C2676 二进制“<<”:“std::ofstream”未定义此运算符或转换为预定义运算符 Project2c++ c:\\users\\dinga\\desktop\\practice projects for c++\\project2c++\\project2c++\\fruit 可接受的类型。H

As a solution i just used #include <fstream> (ie fstream) in the header file where the class was defined.作为解决方案,我只是在定义类的头文件中使用了#include <fstream> (ie fstream) this worked for me.这对我有用。

Change BOBVec3d to Vec3D :更改BOBVec3dVec3D:

BOBVec3D(void) {}
BOBVec3D(float x, float y, float z) : mX(x), mY(y), mZ(z) {}
inline friend std::ostream& operator<< (std::ostream& os, const BOBVec3D& v);

to

Vec3D(void) {}
Vec3D((float x, float y, float z) : mX(x), mY(y), mZ(z) {}
inline friend std::ostream& operator<< (std::ostream& os, const Vec3D& v);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 错误 C2676:二进制“==”:“Person”未定义此运算符或转换为预定义运算符可接受的类型 - C++ error C2676: binary '==': 'Person' does not define this operator or a conversion to a type acceptable to the predefined operator error C2676 binary '==': 'block' 未定义此运算符或转换为预定义运算符可接受的类型 - error C2676 binary '==': 'block' does not define this operator or a conversion to a type acceptable to the predefined operator 错误代码 C2676 二进制“==”:“std::pair<std::string,int> '没有定义此运算符或转换为预定义运算符可接受的类型</std::string,int> - error code C2676 binary '==': 'std::pair<std::string,int>'does not define this operator or a conversion to a type acceptable to the predefined operato C2676:二进制“&lt;”:“const _Ty”未定义此运算符或转换为预定义运算符可接受的类型 - C2676: binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator c2676-二进制&#39;++&#39;未定义此运算符 - c2676 - binary '++' does not define this operator C2676二进制&#39;++&#39;:SetIterator <TElement> 没有定义这个运算符 - C2676 Binary '++': SetIterator<TElement> does not define this operator binary &gt; T 未定义此运算符或转换为预定义运算符可接受的类型 - binary > T does not define this operator or a conversion to a type acceptable to the predefined operator 二进制&#39;[&#39;:&#39;std :: initializer_list <const char *> &#39;未定义此运算符或未转换为预定义运算符可接受的类型 - binary '[' : 'std::initializer_list<const char *>' does not define this operator or a conversion to a type acceptable to the predefined operator C++“二进制'&lt;':'const _Ty'未定义此运算符或转换为预定义运算符可接受的类型”使用“映射<any,any> "</any,any> - C++ "binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator" error using "map<any,any>" 错误 C2676:std::set::const_iterator 没有 operator+ 函数? - Error C2676: std::set::const_iterator doesn't have operator+ function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM