c++中的体系结构x86_64错误

architecture x86_64 error in c++

本文关键字:错误 x86 体系结构 c++      更新时间:2023-10-16

当我试图在mac上用g++运行这个c++代码时,我会收到以下错误。有人能告诉我这是什么意思吗。我是编程新手。因此,我们将非常感谢您的帮助。我正在尝试使用抽象类(Expr.cpp)和子类(Binary.cpp)来实现这一点。

我用g++main.cpp编译

错误-

Undefined symbols for architecture x86_64:
"Expr::Expr()", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
 "Expr::setArg(double)", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Expr::setRece(double)", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Binary::sum()", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Expr::Expr()", referenced from:
  Binary::Binary()in cc8Vni4M.o
 ld: symbol(s) not found for architecture x86_64
 collect2: ld returned 1 exit status

代码

main.cpp-

 #include <iostream>
#include <fstream>
#include <string>
using namespace std;
void stepexecution(double x, double y, double z)
{
string STRING;
Expr ex;
Binary bi;
ifstream infile;
char ele1;
char ele2;
char rece;
double d = 7;
infile.open ("program.txt");
while(!infile.eof()) // To get you all the lines.
{
    getline(infile,STRING);
    int length = STRING.length();
        if(length>8)
        {
            rece = STRING.at(0);
            ele1 = STRING.at(4);
            ele2 = STRING.at(8);
            cout << ele1 << ele2;
            if(ele1=='x' && ele2=='y')
            {
                ex.setArg(x);
                ex.setRece(y);
                double a = bi.sum();
                cout << a;
            }
        }

}
infile.close();
}
int main()
{
char c;
int t = 0;
double x = 10;
double y = 0;
double z = 0;
while(t==0)
{
    cout << "Please enter a command:";
    cin >> c;
    if(c=='r')
    {
        x=0;
        y=0;
        z=0;
    }
    if(c=='x')
    {
        cout << "Please enter the value of x:";
        cin >> x;
    }
    if(c=='y')
    {
        cout << "Please enter the value of x:";
        cin >> y;
    }
    if(c=='z')
    {
        cout << "Please enter the value of x:";
        cin >> z;
    }
    if(c=='s')
    {
        stepexecution(x,y,z);
    }
    if(c=='q')
    {
        exit(0);
    }
}
}

Expr.cpp-

#include "Expr.h"
void Expr::setRece(double x)
{
rece = x;
}
void Expr::setArg(double y)
{
arg = y;
}
//Constructor.
Expr::Expr()
{
}

Expr.h-

#ifndef ____Expr__
#define ____Expr__
#include <iostream>
#include "math.h"
using namespace std;
 class Expr {
public:
void setRece(double x);
void setArg(double y);
Expr();
protected:
double arg, rece;
};
#endif /* defined(____Expr__) */

Binary.cpp-

#include "Binary.h"
double Binary::sum()
{
return rece+arg;
}
double Binary::subt()
{
return rece-arg;
}
double Binary::mult()
{
return rece*arg;
}
double Binary::divi()
{
return rece/arg;
}
double Binary::exp()
{
return pow(rece,arg);
}

二进制.h-

#ifndef ____Binary__
#define ____Binary__
#include "Expr.h"
#include <iostream>
using namespace std;
 class Binary : public Expr
 {
public:
double sum();
double subt();
double mult();
double divi();
double exp();
};
#endif

感谢您的帮助:)

此错误表示您没有正确链接对象文件。所有对象文件都必须链接到最终程序。

如果它们是链接的,那么有一些函数不在链接的任何对象文件中。请链接正确的文件。

要编译文件以生成obj文件,请使用

  g++ -o binary.o -c binary.cpp
  g++ -o expr.o -c expr.cpp

最后链接到主文件使用,

 g++ -o main main.cpp expr.o binary.o
相关文章: