Question:
Let's take the following example:
A movie theater puts season tickets on sale. Each subscription corresponds to a seat in the room to which the member could go throughout the season. In the room there are 25 rows of 20 seats each.
For each seat, the name of the subscriber would have to be stored and if that seat is free or not for purchase.
I have thought about the following:
#include <iostream>
#include <array>
#include <string>
using namespace std;
const int Fil=25;
const int Col=20;
typedef array <string,Fil> TFila;
typedef array <TFila,Col> TFila_asiento; // array de strings locooo!!
struct TSalaCine{
TFila_asiento Fila_asiento; //array de string asiento y fila
string abonado;
};
int main(){
}
void asientos_libres(TSalaCine &sala){
for(int fila=0; fila<25; fila++){
for(int col=0;col<20; col++){
sala.Fila_asiento[fila][col]='0';
}
}
}
Is it possible to make an array of strings, that is to say that in each position of the array what I store is a string?
If so, how do I fill that string to initialize it as an empty set, I've tried putting the value in the for '0'
loop, but it doesn't work.
The goal I have in mind is to first initialize the array of strings to 0 (empty set), and then ask the user for the name of the client and insert it in the place of the respective site.
The problem is that when I go to compare if the site is empty or not if(sala.Fila_asiento[fila][col]=='\0')
I get the following error:
exercise3.cpp:48:36: error: no match for 'operator==' (operand types are 'std::arraystd::__cxx11::basic_string<char, 25ul>::value_type {aka std::__cxx11::basic_string }' and 'char') if(room.seat_row[row][col]=='\0'){ In file included from /usr/include/c++/6/iosfwd:40:0, from /usr/include /c++/6/ios:38, from /usr/include/c++/6/ostream:38, from /usr/include/c++/6/iostream:39, from exercise3.cpp:1: /usr/include/c++ /6/bits/postypes.h:216:5: note: candidate: template bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&) operator==( const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
Thanks for the reply. Greetings 😀
Answer:
The error has nothing to do with the string
array inside a structure. You would get exactly the same error outside of a structure:
error: no match for ‘operator==’ (operand types are ‘std::array<std::__cxx11::basic_string<char>, 25ul>::value_type {aka std::__cxx11::basic_string<char>}’ and ‘char’) if(sala.Fila_asiento[fila][col]=='\0'){
To begin with, it seems that you do not share the code that generates the error. In the error it indicates that you are using the comparison 'operator=='
, in your code you show the assignment operator:
for(int fila=0; fila<25; fila++){
for(int col=0;col<20; col++){
sala.Fila_asiento[fila][col]='0';
// Asignación ---> ^
}
}
The code you show does not give any error. If instead of an assignment it was a comparison, what would the error mean?
-
error: no match for 'operator=='
: No match for the comparison operator. -
(operand types are 'std::array<std::__cxx11::basic_string<char>, 25ul>::value_type {aka std::__cxx11::basic_string<char>}' and 'char')
: Operands areTFila::value_type
(which is astd::string
) andchar
.
That is, you are comparing a string ( std::string
) with a character ( char
), we can see that this operator has two overloads :
constexpr bool operator==( const std::string& lhs, const std::string& rhs ) noexcept;
constexpr bool operator==( const std::string& lhs, const CharT* rhs );
And none of the overloads compare a string to a character, hence the error.
Regarding your doubts:
Is it possible to make an array of strings, that is to say that in each position of the array what I store is a string?
Of course, in your case you're not doing that but an std::array
of std::array
of std::string
, but the concept is the same. You could apply the following improvements to your design:
- Transform the room object into a template and parameterize its rows and columns.
- Defines type aliases internally in the room object.
- Use aliases like C++11.
template <auto Fil, auto Col>
struct TSalaCine{
using TFila = array<string,Fil>;
using TFila_asiento<TFila,Col>;
TFila_asiento Fila_asiento; //array de string asiento y fila
};
// Cine con 25 filas y 20 columnas:
using cine_25x20 = TSalaCine<25, 20>;
How do I fill that string to initialize it as empty set?
You don't have to do anything, by default std::string
are built empty. You can check if an element is empty by calling std::string::empty
:
if (sala.Fila_asiento[fila][col].empty()){