简体   繁体   中英

Problems with Structs (C++)

I want to use a Struct but dont't know where to define it. That's my code at the moment:

Header file (Datos.h)

#ifndef DATOS_H
#define DATOS_H

using namespace std;

class Datos {
public:
    Datos();
    void rellenarDatos();

    struct KeyData;
    struct BBDD;

};

Implementation File (Datos.cpp)

#include <string>
#include <string.h>
#include "Datos.h"

using namespace std;

Datos::Datos() {  
};

struct KeyData {
        int Key;
        string data;
};

struct BBDD {
    KeyData DNI[N];
    KeyData Nombre[N];
    KeyData Apellido[N];
    KeyData Direccion[N];
};

void Datos::rellenarDatos() {
    BBDD.DNI[i].Key = 100;
    BBDD.Nombre[i].Key = 100;
    BBDD.Apellido[i].Key = 100; 
    BBDD.Direccion[i].Key = 100;

    BBDD.DNI[i].data = "hello";
    BBDD.Nombre[i].data = "hello";
    BBDD.Apellido[i].data = "hello";
    BBDD.Direccion[i].data = "hello";   
};

The compiler drops error like this:

Datos.cpp:25:7: error: expected unqualified-id before ‘.’ token
   BBDD.DNI[i].Key = 100;

Where is the problem? Thanks.

There are a couple of problems.

First of all, BBDD is a type, not an object.
This is the immediate cause of the error message.

Second, in your class definition, you have forward-declared two structs in the scope of the class Datos .

The full names of these are Datos::KeyData and Datos::BBDD , so to define them you could write

struct Datos::KeyData {
        int Key;
        string data;
};

struct Datos::BBDD {
    Datos::KeyData DNI[N];
    Datos::KeyData Nombre[N];
    Datos::KeyData Apellido[N];
    Datos::KeyData Direccion[N];
};

On the other hand, it looks like you want to have a member of the type BBDD .
In that case, put the definitions inside the class definition:

class Datos {
public:
    Datos();
    void rellenarDatos();

    struct KeyData {
        int Key;
        string data;
    };

    struct BBDD {
        KeyData DNI[N];
        KeyData Nombre[N];
        KeyData Apellido[N];
        KeyData Direccion[N];
    };

    BBDD bbdd;
};

// ...

void Datos::rellenarDatos() {
    // ...
    bbdd.DNI[i].Key = 100;
    // ...
};

Structs are data structures like classes. What you did now is define a data structure of type BBDD. To access members of this structure you need to define an instance of this struct. You can do this either as a class member in your class or as a global variable. Just don't forget that with structs you need to append the keyword struct to your definition.

Your Problem is in your Header file, when you declare the struct members of your class:

struct KeyData;
struct BBDD;

When you define a struct, it becomes a type. If you want to use it, you have to create an instance of it, not declare another struct. Say I want a struct called Classroom that holds the room number and the subject taught there. I would define it like your structs in your .cpp file:

struct Classroom {
     int roomNumber;
     string subject;
}

Now when I want to use it, I use Classroom , not struct:

Classroom myClassroom;
myClassroom.roomnumber = 25;
myClassroom.subject = "Computer Science";

so in your case, rather than writing

struct KeyData;
struct BBDD;

you should write:

KeyData KeyData;
BBDD BBDD;

...although I would recommend changing either the struct name or the instance name so that they aren't identical to avoid confusion and potentially compiler errors!

EDIT: Note that this won't solve all the errors with your code - you have some other issues within that snippet of undeclared variables (which may not be an issue for you if "N" and "i" in your .cpp are defined elsewhere) which you will have to declare or pass as arguments as required, but the above will fix the errors with your struct declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM