- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经查看了其他一些类似的问题,但似乎所有功能都应该正确设置。在 CodeLite 中运行时一切都正确编译,但随后在使用 g++ 的 Linux 服务器上编译时,我在“main.cpp”中得到对函数 add_fraction、subtract_fraction、multiply_fraction、divide_fraction 错误的 undefined reference 我该如何解决这个问题?谢谢!
main.cpp
#include <iostream>
#include <string>
#include "header.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;
cout << "Input the numerator: ";
cin >> numerator;
cout << "Input the denominator: ";
cin >> denominator;
cout << "Input the numerator2: ";
cin >> numerator2;
cout << "Input the denominator: ";
cin >> denominator2;
cout << "Input the operation: ";
cin >> operation;
if (operation != '+' || '-' || '*' || '/'){
cout << "Please input a valid operator: ";
cin >> operation;
}
if (operation == '+'){
Rational addFrac;
addFrac.add_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '-'){
Rational subFrac;
subFrac.subtract_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '*'){
Rational multFrac;
multFrac.multiply_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '/'){
Rational divideFrac;
divideFrac.divide_fraction(numerator, numerator2, denominator, denominator2);
}
return 0;
}
header.h
#ifndef HEADER_H
#define HEADER_H
class Rational{
public:
void reduce_fraction(int &top, int &bottom);
void add_fraction(int numerator, int denominator, int numerator2, int denominator2);
void subtract_fraction(int numerator, int denominator, int numerator2, int denominator2);
void multiply_fraction(int numerator, int denominator, int numerator2, int denominator2);
void divide_fraction(int numerator, int denominator, int numerator2, int denominator2);
};
#endif // HEADER_H
header.cpp
#include "header.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
void Rational::reduce_fraction (int & top, int & bottom)
{
for (int i = top * bottom; i > 1; i--) {
if ((top % i == 0) && (bottom % i == 0)) {
bottom /= i;
top /= i;
}
}
}
void Rational::add_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::subtract_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator - denominator * numerator2;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::multiply_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator * numerator2;
bottom = denominator * denominator2;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::divide_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator;
bottom = numerator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
生成文件
CXX = g++
CXXFLAGS = -g
default: main
search: main.cpp
${CXX} ${CXXFLAGS} main.cpp -o search
clean:
rm -f *.o main
最佳答案
您根本没有构建 header.cpp
。你的 Makefile 中没有提到它。您需要编译它并将其链接到您的可执行文件中。试试这个:
search: main.cpp header.cpp
${CXX} ${CXXFLAGS} main.cpp header.cpp -o search
关于C++ undefined reference to function 错误仅在 g++ 编译器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28334947/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!