简体   繁体   English

C ++指向在另一个结构内部的结构中定义的变量(由用户填充)

[英]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.

相关问题 用用户定义的构造函数初始化C ++结构 - Initialization of a C++ Struct with a user defined constructor 将一个结构变量分配给 C++ 中不同结构中的另一个相同类型的变量 - Assigning one struct variable to the another varible of a same type which is in different struct in C++ C和C ++头文件:在另一个结构内部定义全局结构 - C and C++ header: Define global struct inside of another struct 初始化一个结构,该结构是另一个结构的成员-ISO C ++ - Initializing a struct which is a member of another struct - ISO C++ 从C ++中的另一个文件指向结构内部的结构的指针 - pointer to a struct inside of a struct from another file in c++ 访问在C ++函数内部的struct中定义的枚举类属性 - access enum class properties which defined in struct inside a function in c++ 在 header 文件中定义的枚举 class 在位于另一个 header 文件中的结构中不起作用? C++ - Enum class defined in header file does not work in struct which is located in another header file?? C++ 如何在C ++中访问结构内部的结构? - How to Access a Struct Inside a Struct in C++? 如何初始化 C++ 中用户定义结构的最小值? - How to initialize a minimum value for a user defined struct in C++? C ++使用在另一个导致无限循环中定义的结构 - C++ use struct defined in another caused endless loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM