[英]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 :更改BOBVec3d到Vec3D:
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.