建筑x86_64的未定义符号。我的代码有什么问题?

Undefined symbols for architecture x86_64. what's wrong with my codes?

本文关键字:代码 我的 什么 问题 符号 x86 未定义 建筑      更新时间:2023-10-16

devices.h文件

#ifndef DEVICES_H
#define DEVICES_H
class malfunctionRecord
{
private:
int Num_Malfunction_Detection;
int Type_Malfunction;
int Last_Malfunction;
public:
    malfunctionRecord();
    void malfunction(int mlfn,int tm);
    void display();
    int getRecord(int*m,int*t);
};
class device
{
private:
    char *Name;
    malfunctionRecord malfunctionRec;
    int Serial_Num;
    int Value_Device;
public:
    device(char*n,int sn);
    ~device();
    void display();
    void recordMalfunction(int m,int t);
    int getMalfunctionRecord(int *m,int *t);
    int amIThisDevice(char*n);
};
#endif

(这是我的.cpp文件)

#include<iostream>
#include "devices.h"
using namespace std;
malfunctionRecord::malfunctionRecord()
{
    Num_Malfunction_Detection=0;
    Last_Malfunction=0;
    Type_Malfunction=='No_Malfunction';
}
void malfunctionRecord::malfunction(int mlfn,int tm)
{
    Num_Malfunction_Detection=Num_Malfunction_Detection+mlfn;
    Last_Malfunction=Last_Malfunction+tm;
}
void malfunctionRecord::display()
{
    if (Num_Malfunction_Detection=0)
        cout<<" the device has never malfunctioned";
    else
        cout<<"The device has malfunctioned"<<Num_Malfunction_Detection<<"times. the malfunction is type"<<Type_Malfunction<<" and last malfunction time is"<<Last_Malfunction<<endl;
}
int malfunctionRecord::getRecord(int*m,int*t)
{
     cout<<"The device has malfunctioned"<<Num_Malfunction_Detection<<"times.";
     Type_Malfunction=*m;
     Last_Malfunction=*t;
}
/*int device::device(char*n,int sn)
{
}
void device::display()
{
}
void device::recordMalfunction(int m,int t)
{
}
int device::getMalfunctionRecord(int *m,int *t)
{
}
int device::amIThisDevice(char*n)
{
}
*/

(这是我的主cpp文件,给定)

#include "definitions.h"
#include "devices.h"
#include <iostream>
using namespace std;

int main() {
malfunctionRecord m1, m2;
device d1("Turn Signal Lever", 195), d2("Accelerator", 247), *d3;
int c, m, t;          // Temp variables to hold values retrieved from getRecord.
char ch;              // Used to hold display until user presses enter.
// malfunctionReport tests.
cout << "Initially both m1 and m2 should have no malfunctions:nn";
m1.display();
cout << "n";
m2.display();
cout << "n";
// Set a malfunction in m1.
m1.malfunction(MALFUNCTION_UNDER_RANGE,10);
cout << "m1 should now have one malfunction with time 10 and under range.n";
m1.display();
// Now add some malfunctions and verify that only the last is kept.
m1.malfunction(MALFUNCTION_OVER_RANGE,25);
cout << "nm1 should now have two malfunctions, the last one with time 25 and over range.n";
m1.display();
m1.malfunction(MALFUNCTION_STOPPED_WORKING,32);
cout << "nm1 should now have three malfunctions, the last one with time 32 and stopped working.n";
m1.display();
// Now test the retrieval function.
c = m1.getRecord(&m, &t);
cout << "nnValues returned from getRecord are " << c << ", " << m << ", and " << t << ".n";
cout << "nEnd of malfunctionReport tests.nnn";
// Hold display so user has a chance to check results so far.
cout << "Press ENTR when ready to continue:";
ch = cin.get();

// device class tests.
cout << "nnnBeginning tests for device class.n";
cout << "Display original d1 and d2:nn";
d1.display();
cout << "n";
d2.display();
cout << "nnTest the amIThisDevice function.  n";
if(d1.amIThisDevice("Turn Signal Lever"))  cout << "  First test on d1 passed.n";
else                                       cout << "  First test on d1 failed.n";
if(d1.amIThisDevice("Accelerator")==0)     cout << "  Second test on d1 passed.n";
else                                       cout << "  Second test on d1 failed.n";
cout << "nnNow test record and get malfunction function members.n";
d1.recordMalfunction(MALFUNCTION_UNDER_RANGE,25);
cout << "  Should see count equal 1 and under range malfunction at time 25:n";
d1.display();
d1.recordMalfunction(MALFUNCTION_OVER_RANGE,50);
cout << "n  Should see count equal 2 and over range malfunction at time 50:n";
d1.display();
d1.recordMalfunction(MALFUNCTION_STOPPED_WORKING,64);
cout << "n  Should see count equal 3 and stopped working malfunction at time 64:n";
d1.display();
cout << "nnTry to retrieve malfunction report.  Should see same values as above.n";
c = d1.getMalfunctionRecord(&m,&t);
cout << "Values returned from getRecord are " << c << ", " << m << ", and " << t << ".n";
// Test destructor.  Create a new device and then delete it.
cout << "nnTesting create and destroy n object.  If you don't see the";
cout << "nend of tests message, there is something wrong with your destructor.nn";
d3 = new device("Temporary Device", 100);
d3->display();
delete d3;

cout << "nnEnd of tests for Program 3.n";

return 0;
}

(这是我的definition.h文件)

#define NO_MALFUNCTION                                   20
#define MALFUNCTION_OVER_RANGE                           21
#define MALFUNCTION_UNDER_RANGE                          22
#define MALFUNCTION_STOPPED_WORKING                      23

当我编译它时,我不断地得到这些错误:

g++ devices.cpp main.cpp -o a
devices.cpp:9:23: warning: character constant too long for its type
main.cpp: In function ‘int main()’:
main.cpp:11: warning: deprecated conversion from string constant to ‘char*’
main.cpp:11: warning: deprecated conversion from string constant to ‘char*’
main.cpp:53: warning: deprecated conversion from string constant to ‘char*’
main.cpp:56: warning: deprecated conversion from string constant to ‘char*’
main.cpp:76: warning: deprecated conversion from string constant to ‘char*’
Undefined symbols for architecture x86_64:
  "device::device(char*, int)", referenced from:
      _main in cc7F618W.o
  "device::display()", referenced from:
      _main in cc7F618W.o
  "device::amIThisDevice(char*)", referenced from:
      _main in cc7F618W.o
  "device::recordMalfunction(int, int)", referenced from:
      _main in cc7F618W.o
  "device::getMalfunctionRecord(int*, int*)", referenced from:
      _main in cc7F618W.o
  "device::~device()", referenced from:
      _main in cc7F618W.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

有人能告诉我我的密码出了什么问题吗?谢谢

您在devices.cpp中注释掉了一堆函数定义(底部的五个)。他们仍然在头部。

这是链接器抱怨你通知它一个符号,然后没有定义它

要么定义函数,要么删除对它们的所有引用。

相关文章: