gpt4 book ai didi

C++ undefined reference to function 错误仅在 g++ 编译器中

转载 作者:行者123 更新时间:2023-11-30 03:56:45 26 4
gpt4 key购买 nike

我已经查看了其他一些类似的问题,但似乎所有功能都应该正确设置。在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com