c++ - 错误:无法在实现文件中绑定(bind)‘std::ostream

标签 c++ g++ operator-overloading netbeans-8 ostream

首先:我已经尝试根据不少于 7 个其他类似问题阅读和修改我的代码。充其量,其他选项会引发大量错误。使用我当前的代码,我只遇到了一个错误。

将它放在类中并使用“friend”不起作用,使用 ostream& operator<< (ostream &out, const Fraction &rhs) 会产生更多错误。 令人沮丧的是,此代码在 c9.io 中有效,但在 Netbeans 中无效。

在我的 main.cpp 中:

#include <iostream>
#include "fraction.h"
using namespace std;
int main() {
    Fraction f(3, 4);
    cout << f;   
    return 0;
}

在分数.h中:

#ifndef FRACTION_H
#define FRACTION_H

class Fraction{
    public:
        //constructor defs            
        //accessors           
        //modifiers/mutators
        void setNumer(int newNum);
        void setDenom(int newDenom);
        void reduce();

    private:
        //instance variables
        int numer;
        int denom;
        //helper functions
        int gcd(int a, int b);
}; 
#endif /* FRACTION_H */

在 fraction.cpp 中:

#include "fraction.h"
#include <cmath>
#include <iostream>
using namespace std;

//code for constructors

//accessors
int Fraction::getNumer(){
    return numer;
}

int Fraction::getDenom(){
    return denom;
}

//modifiers/mutators

//other operator definitions

ostream& operator<< (ostream &out, Fraction &rhs){
    if(rhs.getNumer() == 0){
        out << 0;
    } else if(rhs.getNumer() == rhs.getDenom()){
        out << 1;
    } else {
        rhs.reduce();
        out << rhs.getNumer() << "/" << rhs.getDenom();
    }
}

输出是:

g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:13: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
     cout << f;   
             ^
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from main.cpp:1:
/usr/include/c++/4.8/ostream:602:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Fraction]’
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^

最佳答案

不知道operator<< Fraction.cpp 中定义的函数在main.cpp因此,这条线

cout << f;

是个问题。

在.h文件中添加函数的声明。

#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>

class Fraction{
    public:
        //constructor defs            
        //accessors           
        //modifiers/mutators
        void setNumer(int newNum);
        void setDenom(int newDenom);
        void reduce();

    private:
        //instance variables
        int numer;
        int denom;
        //helper functions
        int gcd(int a, int b);
}; 

std::ostream& operator<< (std::ostream &out, Fraction const& rhs);
//                                                    ^^^^ Using const&, not just Fraction&

#endif /* FRACTION

关于c++ - 错误:无法在实现文件中绑定(bind)‘std::ostream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35111073/

相关文章:

c++ - 为什么某个 .cpp 文件不能编译?

c++ - 使用 g++ 在终端中运行 .cpp 文件时出错

c++ - 自定义类的 Cin 参数

c++ - 我的自定义 Vector 类的 operator+ 有什么问题?

c++ - 奥尔森时区与Posix时区转换

c++ - 允许导航到需要证书 QML 的站点

c++ - 为什么在 for 循环中没有初始化的声明没有任何警告?

c++ - 使用模板可视化代码中的2D和3D形状

c++ - 在构造函数调用中出现错误 C2512

c++ - 在 C++ 中将数据从一种类型转换为另一种类型?