C++ 获取未定义的符号错误

C++ Getting undefined symbol error

本文关键字:符号 错误 未定义 获取 C++      更新时间:2023-10-16

>我正在尝试做作业,程序在我的家编译得很好PC(Netbeans),但是当我尝试在Uni Sun盒子上编译时,出现以下错误

编译标志:g++ -lm -ansi -Wall -pedantic skyhigh.cpp -o skyhigh

Undefined                       first referenced
 symbol                             in file
Flight::GetSeats()                  /var/tmp//cc49OAwj.o
Flight::GetCost()                   /var/tmp//cc49OAwj.o
Flight::GetAircraft()               /var/tmp//cc49OAwj.o
ld: fatal: Symbol referencing errors. No output written to skyhigh
collect2: ld returned 1 exit status

任何帮助将不胜感激,因为我花了几个小时梳理互联网对于解决方案

飞行。H 文件

        //
// flight.h
//
// Parent Flight class
//
#ifndef __FLIGHT_H__
#define __FLIGHT_H__
#include<iostream>
#include<string>
using namespace std;
/*TO DO  REQUIRED HEADER FILES AND NAMESPACES*/

class Flight
{
protected:
   string aircraft;
   string category;
   int seats;
   double cost;


public:
   virtual ~Flight(){};
  // virtual void Display() =0;
   void SetAircraftType(string air);
  string GetAircraft();
  int GetSeats();
  double GetCost();
  void SetSeats(int seat);


 //TO DO
  // Prototypes of all the Item functions
};
#endif

FLIGHT.CPP FILE

        #include "flight.h"

 string Flight::GetAircraft(){
    return aircraft;
 }
 int Flight::GetSeats(){

    return seats;
 }
 double Flight::GetCost(){

    return cost;
 }
 void Flight::SetSeats(int seat){
    seats=seat;
 }
void Flight::SetAircraftType(string category){

   aircraft = category;
}

MAIN FILE
// skyhigh.cpp for CPT 323 Assignment 1 SP3 2013
//
//
// CPT323 2013  assignment 1

#include "skyhigh.h"

/////////////////////////////////////////////////////
int main()
{
   Flight newflight ;
   cout<< "Plane Name:" <<newflight.GetAircraft()<< " Seats: "<< newflight.GetSeats()<<"Cost: "<<newflight.GetCost()<< endl;

   Scenic newflight2 ;
  //cout<< "Plane Name:" <<newflight2.GetAircraft()<< " Seats: "<< newflight2.GetSeats()<<"Cost: "<<newflight2.GetCost()<< endl;
  //Scenic* sptr=&newflight2;
  //BookingSheet booking;
  //cout<< booking.GetDay()<<booking.GetTime()<<booking.GetPassengerName()<<booking.GetPaymentStatus()<<endl;
   int finished = 0;  
   do {
      /*Create array for use in menu options*/


   cout << "Main Menu : " <<endl;
   cout << "1) Add a flight booking "<<endl;
   cout << "2) Remove a flight booking "<<endl;
   cout << "3) View current booking sheet " <<endl;
   cout << "0) Exit n" <<endl;
  cout<< "Please make a selection (0-3) ";

  int selection;
  cin>> selection;
  /*Capture user selection default in switch statement validates 1-9*/
//   fgets(option,sizeof(option),stdin);
// 
// 
   switch (selection) {
       case 0: 
        cout << "Thanks for using the SkyHigh Booking System"<<endl;
        finished=1;
           break;
        case 1: 
        cout << "1) Add a flight booking "<<endl;

           break;
        case 2:
         cout << "2) Add Customer "<<endl;
            break;

      case 3:
         cout << "3) Display Stock " <<endl;
           break;

        default:
            printf("Valid input is 0-3 n");
    }
    }
   while(!finished);

return 0;
}

生成文件

skyhigh: skyhigh.o utility1.o flight.o scenic.o aerobatic.o
    g++ skyhigh.o utility1.o flight.o scenic.o aerobatic.o -o skyhigh
skyhigh.o: skyhigh.cpp skyhigh.h flight.h aerobatic.h scenic.h
    g++ -ansi -Wall -pedantic -gstabs -c skyhigh.cpp
utility1.o: utility1.cpp utility1.h
    g++ -ansi -Wall -pedantic -gstabs -c utility1.cpp
flight.o: flight.cpp flight.h aerobatic.h scenic.h
    g++ -ansi -Wall -pedantic -gstabs -c flight.cpp
scenic.o: scenic.cpp scenic.h 
    g++ -ansi -Wall -pedantic -gstabs -c scenic.cpp
aerobatic.o: aerobatic.cpp aerobatic.h 
    g++ -ansi -Wall -pedantic -gstabs -c aerobatic.cpp

clean:
    rm -f *.o core *.report *.errs

它找不到定义这些缺失函数的对象文件。请确保添加生成的对象文件以进行链接。类似的东西(main 是 exec 名称,main.o 是带有 main 函数的对象,根据需要更改它们):

g++ -o main main.o skyhigh.o utility1.o flight.o scenic.o aerobatic.o
相关文章: