[英]C++ Pointing a variable (to be filled by the user) that is defined in a struct which is inside another struct
I'm working on lists . 我正在清单上 。 I would like to perform a variable insertion (user side) that is defined in a struct which is inside another struct. 我想执行在另一个结构内部的结构中定义的变量插入(用户端)。
Part of my code is this: 我的代码的一部分是这样的:
#include <iostream>
#include <string>
using namespace std;
// Global variables
struct gioco {string piattaforma;
string titolo;
string genere;
string sviluppatore;
string publisher;
struct data {int giorno;
int mese; // Formato data GG/MM/AAAA
int anno; };
bool disponibile;
gioco * recordSuccessivo; };
// Prototypes
bool stampaLista (gioco *);
gioco * aggiungiGioco (gioco *);
// MAIN Function
int main (void)
{// Local Variables (MAIN)
char sceltaMenu = '\0';
gioco * puntatoreTesta = NULL;
gioco * puntatoreCoda = NULL;
gioco * puntatoreDeposito = NULL;
// [more code]
stampaLista (puntatoreTesta);
// [more code + ASCII menu]
cin>>sceltaMenu;
switch (sceltaMenu)
{case '1':
puntatoreDeposito = aggiungiGioco (puntatoreTesta);
break;
// [more cases + default]
}
return 0;
}
// Other Functions
bool stampaLista (gioco * puntatoreTesta)
{cout<<endl<<"PIATTAFORMA"<<"\t"<<"TITOLO"<<"\t"<<"GENERE"<<"\t"<<"SVILUPPATORE"<<"\t"<<"PUBLISHER"<<"\t"<<"DATA DI USCITA"<<"\t"<<"DISPONIBILE"<<endl<<endl;
if (puntatoreTesta != NULL)
{gioco * puntatoreDeposito = NULL;
puntatoreDeposito = puntatoreTesta;
do {cout<<puntatoreDeposito -> piattaforma<<"\t"
<<puntatoreDeposito -> titolo<<"\t"
<<puntatoreDeposito -> genere<<"\t"
<<puntatoreDeposito -> sviluppatore<<"\t"
<<puntatoreDeposito -> publisher<<"\t"
<<puntatoreDeposito -> gioco.data.giorno<<"/"<<puntatoreDeposito -> gioco.data.mese<<"/"<<puntatoreDeposito -> gioco.data.anno<<"\t"
<<puntatoreDeposito -> disponibile<<endl;
puntatoreDeposito++;
} while (puntatoreDeposito != NULL);
return false;
}
else
{cout<<"La lista e' vuota!"<<endl<<endl<<endl;
return true;
}
}
gioco * aggiungiGioco (gioco * puntatoreTesta)
{// Local Variables
gioco * puntatoreNuovo = new gioco;
puntatoreNuovo -> recordSuccessivo = NULL;
cout<<"NUOVO RECORD"<<endl<<endl;
cout<<"Piattaforma:"<<"\t";
cin>>puntatoreNuovo -> piattaforma;
cout<<"Titolo:"<<"\t";
cin>>puntatoreNuovo -> titolo;
cout<<"Genere:"<<"\t";
cin>>puntatoreNuovo -> genere;
cout<<"Sviluppatore:"<<"\t";
cin>>puntatoreNuovo -> sviluppatore;
cout<<"Publisher:"<<"\t";
cin>>puntatoreNuovo -> publisher;
cout<<"Data di Uscita:"<<endl;
cin>>puntatoreNuovo -> gioco.data.giorno>>puntatoreNuovo -> gioco.data.mese>>puntatoreNuovo -> gioco.data.anno;
return puntatoreNuovo;
}
What I get with this, is an error of "invalid use of struct gioco
", both in print and field filling functions, as I use the syntax gioco.data.giorno
/ .mese
/ .anno
to access these nested variables by pointing to them with the new node
constructor pointer. 我得到的是在打印和字段填充功能中出现“无效使用struct gioco
”的错误,因为我使用gioco.data.giorno
/ .mese
/ .anno
语法通过指向以下.anno
来访问这些嵌套变量它们具有new node
构造函数指针。
This is almost surely a problem of wrong syntax, though I cannot see the right way to work this out. 尽管我看不出解决该问题的正确方法,但这几乎肯定是语法错误的问题。 Any suggestions on how to achieve what am I trying to? 关于如何实现我的目标的任何建议?
You need to define your struct in this way: 您需要通过以下方式定义您的结构:
// Global variables
struct gioco {string piattaforma;
string titolo;
string genere;
string sviluppatore;
string publisher;
struct data {int giorno;
int mese; // Formato data GG/MM/AAAA
int anno; } data_field;
bool disponibile;
gioco * recordSuccessivo; };
And then you can access it: 然后您可以访问它:
gioco* puntatoreNuovo = new gioco;
puntatoreNuovo->recordSuccessivo = NULL;
......
cout<<"Data di Uscita:"<<endl;
cin>>puntatoreNuovo->data_field.giorno >> puntatoreNuovo->data_field.mese >> puntatoreNuovo->data_field.anno;
......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.