c++ - 我怎样才能清除这个错误?我在终点线,需要修复我的输入过载
问题描述
这是我得到的错误代码:
/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;
}
解决方案
您没有显示所有代码,但我们可以看到问题。
在某处,您宣布:
istream& operator>>(std::istream&, rational const&)
这就是正在使用的东西。
除非它不能,因为你的定义是:
istream& operator>>(std::istream&, rational&)
你丢失了const
,所以它是一个单独的函数。您正在使用的那个没有定义。
密切注意代码中类型的详细信息,以及错误中提到的类型。
推荐阅读
- python - 如何使用相应的字典值自动填充可选函数参数?
- python - 为什么我的爬虫只在csv文件中写错了部分数据?
- sql - 如何在 SQL 中满足条件之前对行求和
- r - R:用上面的值填充一组列中的所有 NA
- swift - 如何删除与不存在的变量相关的控制台错误
- python - Matplotlib 极坐标绘图:以度数显示极坐标刻度,采用十进制格式
- python - 增加 Tkinter Notebook 中选项卡按钮的大小
- javascript - 谷歌浏览器不接受分页符
- azure - 使用 ADB2C 和共享的 Auth Cookie 在 Azure 应用服务上获取无限重定向循环
- powerbi - 如何在 Power BI 中为每个日历月生成日历表并使其从今天停止