尝试将struct对象传递给类头文件

时间:2014-05-21 10:58:43

标签: c++ class oop header-files

我试图将我的项目从仅使用一个主文件转换为两个文件,方法是将所有结构和函数放到另一个头文件中。但似乎我在编译时已经克服了一个问题。我不知道......我现在就在这里和那里进行编辑......它说的是“等级票”#34;没有名为' b' in' void ticket :: loadData(ticket :: destination *)'和'无效票据::预订(ticket :: clientInfo,ticket :: destination *)'。我在学习OOP方面还很陌生,并且不太了解这个概念。有什么我遗漏或做错了吗?

//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 << "\n\t\t====== TICKET BUS SYSTEM ======";
 cout <<"\n\n\t\t  WELCOME TO TICKET BUS SYSTEM";
 cout << "\n\n\t\t     PLEASE KEY IN YOUR DETAILS\n";
 cout<<" \n\t\t===================================";
 cout << "\n\n\t\t  NAME       : "; getline(cin, a.name);
 cout << "\t\t  MATRIC NO. : ";     getline(cin, a.matric);
 cout << "\t\t  PHONE NO.  : ";     getline(cin, a.phoneNum);
 cout<<" \n\t\t===================================";
 cout << "\n\n";

 //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 <<"\n\t\tWELCOME "<<a.name <<" to TICKET BUS SYSTEM \n";
 cout <<"\t\tWE PROVIDE BUS SERVICE TO :-\n\n";
 cout <<left<<setw(25)<<"\t\tDestination"<<setw(12)<<"Ticket Price\n";
 cout <<right<<setw(48)<<"(USD)\n";
 cout <<"\t\t-----------------------------------";

 for (int x=0; x<12; x++)
{
    cout <<"\n\n\t\t"<<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 <<"\n\t\t "<<p+1<<". "<<left<<setw(24)<<hi.b[x].city[p];}
}
 cout <<"\n\t\t-----------------------------------\n"; 

cout <<"\n\n\t\tINSERT YOUR BOOKING INFORMATION :-\n";
cout << "\n\t\tDEPART DATE  (DD/MM/YY) :: " ;           cin>>date;  
cout << "\t\tDestination (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 <<"\n\n\t\tYou 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 <<"\n\n\t\tTHANK YOU FOR USING OUR BUS SYSTEM :)\n";
cout <<"\t\tPLEASE REFER AND PRINT YOUR RESIT\n\t\t";
cout<<"\n\n\t\t";
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

1 个答案:

答案 0 :(得分:0)

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

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