首页 > 解决方案 > 我怎样才能清除这个错误?我在终点线,需要修复我的输入过载

问题描述

这是我得到的错误代码:

/tmp/cczLRrEI.o: In function main:
main.cpp:(.text+0x95): undefined reference to operator>>(std::istream&, rational const&)'
main.cpp:(.text+0x15b): undefined reference to operator>>(std::istream&, rational const&)'
main.cpp:(.text+0x340): undefined reference to operator>>(std::istream&, rational const&)'
collect2: error: ld returned 1 exit status

这是我的operator>>重载函数:

istream& operator>>(istream& input, rational& fraction) {
    int numerator, denominator;
    char slash;
    input >> numerator >> slash >> denominator;
    fraction = rational(numerator,denominator);
    return input;
}

我相信这就是问题所在!

我的主要功能:

#include <iostream>
#include <string>
#include <stdlib.h>
#include "rational.h"

using namespace std;

int main() {
    char oper;
    string operators = "+-*/?<>";

    rational result;
    rational operand;

    cout << "Enter op1 (in the format of p/q): ";
    cin >> result;

    // Test rational class member function
    do {
        cout << "\nEnter operator [+, -, /, *, =, ?(==), <(less), >(greater), c(lear), a(ccessors), q(uit)]: ";
        // TODO: Read the operator into oper
        cin >> oper;
        // TODO: Change the pseodocode below to test oper for binary operators
        bool check_oper = false;
        for (char const& x : operators) //range based for loop to check the string 
        {
            if (oper == x) 
            {
                check_oper = true;
            }
        }
        if (check_oper) 
        {
            cout << "\nEnter op2 (in the format of p/q): ";
            cin >> operand;
        }

        // ToDo: Implement a switch or multiway if statement with one case for
        // each option above where
        // '+','*','/','-' inputs a rational op1 and calculates
        // result.oper(result,op1)
        // '=' outputs the current result,
        // 'c' to clear current result, use input function to read first
        // operand into result, 'a' to test accessors, 'q' to quit.
        switch (oper) 
        {
        case '+':
            result = result + operand;
            // result += operand;
            break;
        case '-':
            result = result - operand;
            break;
        case '*':
            result = result * operand;
            break;
        case '/':
            result = result / operand;
            break;
        case '=':
            //outputs current result
            cout << "result = " << result;
            break;
        case '?':
            // ternary operator
            // condition ? return if true : return if false
            cout << ((result == operand) ? "Correct! Good job!" : "Oh no! Good Try!") << endl;
            break;
        case '<':
            cout << ((result < operand) ? "True, great!" : "False, try again!") << endl;
            break;
        case '>':
            cout << ((result > operand) ? "True, great!" : "False, try again!") << endl;
            break;
        case 'c':
            cout << "\nEnter op1 (in the format of p/q): ";
            cin >> result;
            break;
        case 'a':
            cout << "result's numerator is: " << result.getNumerator() << endl;
            cout << "result's denominator is: " << result.getDenominator() << endl;
            break;
        }
        if (cin.fail()) 
        {
            cin.clear(); cin.ignore();
        }
    } while (oper != 'q');
    return 0;
}

标签: c++

解决方案


您没有显示所有代码,但我们可以看到问题。

在某处,您宣布:

istream& operator>>(std::istream&, rational const&)

这就是正在使用的东西。

除非它不能,因为你的定义是:

istream& operator>>(std::istream&, rational&)

你丢失了const,所以它是一个单独的函数。您正在使用的那个没有定义。

密切注意代码中类型的详细信息,以及错误中提到的类型。


推荐阅读