试图将结构对象传递给类处理程序文件

Trying to pass struct object to class haeder file

本文关键字:处理 程序 文件 结构 对象      更新时间:2023-10-16

我正试图将我的项目从仅使用一个主文件转换为两个文件,将所有结构和函数放入另一个头文件。但似乎我已经克服了一个问题,当我编译。我不知道……我在这里和那里编辑,现在……它说'class ticket '在'void ticket::loadData(ticket::destination*)'和'void ticket::Booking(ticket::clientInfo, ticket::destination*)'中没有名为'b'的成员。我在学习面向对象方面还是新手,并不真正了解这个概念。有什么我遗漏或做错的吗?

//my ticket.h
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
class ticket
{
  private:

  public:
     struct clientInfo 
     {
            string name,
                       matric,
            phoneNum;
     };
     struct destination 
     {
            string destinasi;
                string city[2];
            string code[2];
            float price;
     };
    ticket(){}; 
    void loadData(destination b[13] );
    void MainMenu(clientInfo a);
    void Booking(clientInfo a, destination b[13]);
    void printOut(clientInfo a, destination b[13], int y, string);    
};
void ticket::loadData(destination b[13])
{
  tiket hi;
  ifstream inFile;
  inFile.open("negeri3.txt");
  if(!inFile) { cout << "Error Loading File!";}
  else  
  { 
      for (int x=0; x < 12 ; x++)
      {  
         getline(inFile, hi.b[x].destinasi, ',');
         inFile.ignore();
         //cout <<hi.b[x].destinasi<<"t";   //seem like if i try to output this, there is an output
         if(x<8)
         {
           for(int y=0; y<2; y++)
           {
             getline(inFile, hi.b[x].city[y], ','); inFile.ignore();
             //cout <<hi.b[x].cityr[y]<<"n";
           } 
           for(int l=0; l<2; l++)
           {
             getline(inFile, hi.b[x].code[l], ','); inFile.ignore();
           } 
           inFile>>hi.b[x].price; inFile.ignore();
         }
         else if(x>=8)
         {
           getline(inFile, hi.b[x].city[0], ','); inFile.ignore();
           for(int y=0; y<2; y++)
           {
             getline(inFile, b[x].code[y], ','); inFile.ignore();
           } 
           inFile>>hi.b[x].price; inFile.ignore();
         }
      } 
  }
 inFile.close();    
}
void ticket::MainMenu(clientInfo a)
{
 tiket k;
 //system("cls");
 cout << "ntt====== TICKET BUS SYSTEM ======";
 cout <<"nntt  WELCOME TO TICKET BUS SYSTEM";
 cout << "nntt     PLEASE KEY IN YOUR DETAILSn";
 cout<<" ntt===================================";
 cout << "nntt  NAME       : "; getline(cin, a.name);
 cout << "tt  MATRIC NO. : ";     getline(cin, a.matric);
 cout << "tt  PHONE NO.  : ";     getline(cin, a.phoneNum);
 cout<<" ntt===================================";
 cout << "nn";
 //cout <<k.b[0].destinasi;
 cout << a.name;
 cin.get();

}
void ticket:: Booking(clientInfo a, destination b[13])
{
    tiket hi;
system("cls");
int hii = 0; 
string ngriMana, date;
cout <<"nttWELCOME "<<a.name <<" to TICKET BUS SYSTEM n";
 cout <<"ttWE PROVIDE BUS SERVICE TO :-nn";
 cout <<left<<setw(25)<<"ttDestination"<<setw(12)<<"Ticket Pricen";
 cout <<right<<setw(48)<<"(USD)n";
 cout <<"tt-----------------------------------";
 for (int x=0; x<12; x++)
{
    cout <<"nntt"<<left<<setw(25)<<hi.b[x].destinasi<<""<<right<<setw(5)<<hi.b[x].price;   //but there is no output coming out from this fucntion
    for(int p=0; p<2; p++)
    { 
     cout <<"ntt "<<p+1<<". "<<left<<setw(24)<<hi.b[x].city[p];}
}
 cout <<"ntt-----------------------------------n"; 
cout <<"nnttINSERT YOUR BOOKING INFORMATION :-n";
cout << "nttDEPART DATE  (DD/MM/YY) :: " ;           cin>>date;  
cout << "ttDestination (eg: SA for Shah Alam)  :: ";  cin>>ngriMana;

 for (int y=0; y<12; y++)
 {
     if(ngriMana == b[y].code[0] || ngriMana == b[y].code[1] )
        {
         cin.get();
         cin.get();
         printOut(a,b,y,date);
         hii = 1;
         }
  }
 if ( hii== 0)
 {
     for(int i=0; i<12; i++)
     {
              if( ngriMana != b[i].code[0] || ngriMana != b[i].code[1])
      {
        cout <<"nnttYou have entered a wrong destination code! Please Try again";
        cin.get();
        cin.get();
           Booking(a,b);
      }
    }
   }
}

下面是main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
#include "tiket.h"
using namespace std;
int main()
{
    tiket ha;
    tiket::clientInfo a;
    tiket::destination b[13];
ha.loadData(b);
ha.MainMenu(a);
ha.Booking(a,b);
system("cls");
cout <<"nnttTHANK YOU FOR USING OUR BUS SYSTEM :)n";
cout <<"ttPLEASE REFER AND PRINT YOUR RESITntt";
cout<<"nntt";
system("PAUSE");
return 0;
}

和我的negeri.txt包含

Johor (JOH), Batu Pahat, Mersing, JOH, joh, 30
Kedah (KDH), Sg Petani, Alor Star, KDH, kdh, 65
Kelantan (KEL), Kota Bharu, Tanah Merah, KEL, kel, 60
Kuala Lumpur (KL), Bandar Tasik Selatan, Pudu, KL, kl, 38
Melaka (MEL), Melaka Sentra, Masjid Tanah, MEL, mel, 28
Negeri Sembilan (NS), Seremban, Port Dikson, NS, ns, 30
Pahang (PAH), Kuala Lipis, Kuantan, PAH, pah, 40
Penang (PNG), Butter worth, Sg Nibong, PNG, png, 60
Perak (PRK), Ipoh, PRK, prk, 50
Perlis (PER), Kuala Perlis, PER, per, 70
Selangor (SA), Shah Alam, SA, sa, 32
Terengganu (TRG), Kuala Terengganu, TRG, trg, 50

首先你应该修复你的代码中的错字(tiket - ticket)。然后,您应该创建一个ticket.cpp文件,并将您的函数实现移动到那里。当你把非内联函数的实现放在一个头文件中时,当你从多个cpp文件中包含它时,你会得到多个定义错误。

您还应该避免写入cout并在ticket成员函数中获取用户输入。尝试将用户交互与数据存储分离。