Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

EDIT2: removed using namespace Internal;, it is indeed point-defeating to have it in a header file

like image 441
WurmD Avatar asked Nov 22 '18 11:11

WurmD


2 Answers

missing #include <iostream> at the top.

Fixed it. (Oh what terribly terribly poor compilation errors in C++ can be..)

like image 182
WurmD Avatar answered Oct 22 '22 22:10

WurmD


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

As a solution i just used #include <fstream> (ie fstream) in the header file where the class was defined. this worked for me.

like image 38
Dinga Avatar answered Oct 22 '22 21:10

Dinga