【发布时间】:2014-05-21 10:58:43
【问题描述】:
我正在尝试将我的项目从仅使用一个主文件转换为两个文件,方法是将所有结构和函数放入另一个头文件。但是我编译的时候好像解决了一个问题。我不知道..我现在到处都在编辑..它说'class ticket'在'void ticket::loadData(ticket::destination*)'和'中没有名为'b'的成员void ticket::Booking(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
【问题讨论】:
-
您在代码中混淆了tiket 和ticket 的命名时不会出现编译器错误吗?
-
其实在我的原始代码中,我使用tiket 作为我的班级n 当我想将它作为票张贴在这里时,我稍微改变了它。我很糟糕,因为我没有设法改变所有这些
标签: c++ class oop header-files