体系结构x86_64的未定义符号:“ operator <<”

时间:2019-02-23 23:12:39

标签: c++ class operator-overloading overloading

我正在尝试重载<<操作符,以输出一个名为有理数的类,该类代表有理数。

函数如下:

ostream &operator<<(ostream &os, const rational_t& r)
{ 
   os << r.get_num() << "/" << r.get_den() << "=" << r.value() << endl;
   return os;
} 

无需使用它即可编译良好。但是,当我尝试在主页中使用它时,会抱怨像这样:体系结构x86_64的未定义符号: “运算符<<(std :: __ 1 :: basic_ostream>&,Rational_t const&)”

有什么想法吗?谢谢!

主要:

#include <iostream>

#include <cmath>
#include <vector>

#include "rational_t.hpp"

using namespace std;

int main()
{

cout << a; // Problem here..

}

cpp类:

#include <iostream>
#include <math.h>
#include <fstream>

#include "rational_t.hpp"

rational_t::rational_t(const int n, const int d)
{
    assert(d != 0);
    num_ = n, den_ = d;
}

// Other class methods here..

ostream &operator<<(ostream &os, rational_t &r)
{
    os << r.get_num() << "/" << r.get_den() << "=" << r.value() << endl;
    return os;
}

hpp类:

#pragma once

#include <iostream>
#include <cassert>
#include <cmath>
#include <fstream>

#define EPSILON 1e-6

using namespace std;

class rational_t
{
  int num_, den_;

public:
  rational_t(const int = 0, const int = 1);
  ~rational_t() {}

// Other methods like get_num(), get_den(), set... HERE

rational_t operator+(const rational_t&);
rational_t operator-(const rational_t&);
rational_t operator*(const rational_t&);
rational_t operator/(const rational_t&);

void write(ostream &os = cout) const;
void read(istream &is = cin);

friend ostream &operator<<(ostream &os, const rational_t&);

};

0 个答案:

没有答案