错误:无法在实现文件中绑定'std :: ostream

时间:2016-01-31 06:27:50

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

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

将它放入课堂并使用“朋友”不起作用,并使用ostream&运营商LT;< (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;
}

在fraction.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)
     ^

2 个答案:

答案 0 :(得分:1)

operator<< Fraction.cppmain.cpp中定义的cout << f; 函数不知道。

#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

是个问题。

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

function concatCellData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Data');

  var colOneData = sh.getRange(2, 1, sh.getLastRow()-1, 1).getValues();
  var colTwoData = sh.getRange(2, 2, sh.getLastRow()-1, 1).getValues();

  var newData = [],
      newDataColOne = [],
      colOneValue,
      existInNewData = false,
      colB_One,
      colB_Two,
      dataPosition,
      thisValue,
      combinedArrays = [],
      arrayTemp = [];

  for (var i=0;i<colOneData.length;i+=1) {
    colOneValue = colOneData[i][0];
    dataPosition = newDataColOne.indexOf(colOneValue);
    existInNewData = dataPosition !== -1;

    if (!existInNewData) {//If it doesn't exist in the new data, just write the values
      newDataColOne.push(colOneValue);
      newData.push([colOneValue, colTwoData[i][0]]);
      continue;
    };

    colB_One = [];
    colB_Two = [];
    combinedArrays = []
    arrayTemp = [];

    colB_One = colTwoData[i][0].split(",");
    colB_Two = newData[dataPosition][1];
    colB_Two = colB_Two.split(",");

    var combinedArrays = colB_One.concat(colB_Two);

    //Get unique values
    for (var j=0;j<combinedArrays.length;j+=1) {
      thisValue = combinedArrays[j].trim();
      if (arrayTemp.indexOf(thisValue) === -1) {
        arrayTemp.push(thisValue);
      };
    };

    newData[dataPosition] = [colOneValue, arrayTemp.toString()]; //Overwrite existing data

  };

  ss.getSheetByName('Output').getRange(2, 1, newData.length, newData[0].length).setValues(newData);
};

答案 1 :(得分:0)

所以答案部分是@ r-sahu所说的: 我必须将getNumer()getDenom()设置为const成员函数但我只能通过在函数定义中使用const&来消除错误。

好:

std::ostream& operator<< (std::ostream &out, Fraction rhs);

不好:

std::ostream& operator<< (std::ostream &out, Fraction const& rhs);

它现在编译并运行。任何有关为什么使成员函数const但没有将分数作为const传递使其工作的任何见解?我偶然偶然发现了这个解决方案而且不理解。