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

C++ undefined reference to function error in g++ compiler only

我已经查看了其他一些类似的问题,但似乎所有功能都应该正确设置。在 CodeLite 中 运行 时一切都正确编译,但随后使用 g++ 在 Linux 服务器上编译时,我得到了对函数 add_fraction、subtract_fraction、multiply_fraction 的未定义引用, divide_fraction 'main.cpp' 错误 我该如何解决这个问题?谢谢!

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 中没有提到它。您需要将其编译并 link 到您的可执行文件中。试试这个:

search: main.cpp header.cpp
    ${CXX} ${CXXFLAGS} main.cpp header.cpp -o search