试图将结构对象传递给类处理程序文件
Trying to pass struct object to class haeder file
我正试图将我的项目从仅使用一个主文件转换为两个文件,将所有结构和函数放入另一个头文件。但似乎我已经克服了一个问题,当我编译。我不知道……我在这里和那里编辑,现在……它说'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
成员函数中获取用户输入。尝试将用户交互与数据存储分离。
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 在遍历处理程序的向量时注册和注销处理程序
- 有可能在信号处理程序中设置promise吗
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 通过安装信号处理程序关闭多线程应用程序
- QDateTime::toString() 在退出处理程序中使用时失败
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- async_write完成处理程序最早何时完成?
- C++事件系统 - 多态事件和事件处理程序
- 具有shared_ptr的处理程序中的分段错误
- 为什么我的信号处理程序只执行一次?
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- 如何使用从处理程序调度的最终回调将响应异步返回给调用方on_read?
- C++ 在信号处理程序后继续执行
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 没有信号处理程序的POSIX定时器的目的是什么?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"