2
Errors occur when I step the class object Name_pairs
by the exit operator <<
and by the comparative operator ==
.
Header:
class Name_pairs
{
public:
void read_names();
void read_ages();
void print() const;
void sort();
const std::vector<std::string>& get_name() const { return name; }
const std::vector<double>& get_age() const { return age; }
private:
std::vector<std::string>name;
std::vector<double>age;
};
}
Main.cpp:
int main()
{
Name_pairs objeto;
objeto.read_names();
objeto.read_ages();
objeto.sort();
objeto.print();
Name_pairs objetoDois;
objetoDois.read_names();
objetoDois.read_ages();
objetoDois.sort();
objetoDois.print();
if(objeto == objetoDois) cout << "iguais!"; // erro no operador == ;
else cout << "não são iguais!";
cout << objeto; // erro no operador << ;
system("pause");
return 0;
}
cpp of definitions:
void Name_pairs::read_names()
{
cout << "Entre com os nomes desejados, digite 'SemNome' para finalizar a lista." << endl;
string NomesVetor;
while (cin >> NomesVetor && NomesVetor != "SemNome")
{
for (size_t x = 0; x < name.size(); ++x) // confere se há nomes duplicados
{
if (name[x] == NomesVetor) cout << "Nome repetido." << endl;
}
name.push_back(NomesVetor);
}
}
void Name_pairs::read_ages()
{
for (size_t x = 0; x < name.size(); ++x)
{
cout << "Idade de " << name[x] << ": " << endl;
double IdadesVetor;
cin >> IdadesVetor;
age.push_back(IdadesVetor);
}
}
std::ostream& operator<<(std::ostream& os, const Name_pairs& np)
{
for (size_t x = 0; x < np.get_name().size(); ++x)
os << '(' << np.get_name()[x] << ', ' << np.get_age()[x] << ')' << endl;
return os;
}
bool operator==(const Name_pairs& a, const Name_pairs& b )
{
if(a.get_name().size() != b.get_name().size() || a.get_age().size() != b.get_age().size())
return false;
for (size_t x = 0; x <a.get_name().size(); ++x)
{
if (a.get_name()[x] != b.get_name()[x]) return false;
if (a.get_age()[x] != b.get_age()[x]) return false;
}
return true;
}
bool operator!=(const Name_pairs& a, const Name_pairs& b)
{
return !(a == b);
}
Remembering that: The code compiles if removed
if(objeto == objetodois)cout << "iguais!"; // erro no operador ==
else cout << "não são iguais!";
cout << objeto; // erro no operador <<
of main.
Put more excerpts from
main()
, you can even tell the type of variables used only with this excerpt.– Maniero
Okay, I edited with the main.
– Marv
This code does not even compile. It has primary errors. Put a [mcve], it will not do to play random codes, this will not help you.
– Maniero
Let me explain further. As I said, the code compiles but the error only appears if I call the
<<
and the==
in themain
, I passed the type I want to compare, in case two classes with two vectors: a string and a double in each. And I also showed you the overload function. It was not the intention if I passed the impression of "copy paste and solve my problems", I will also show the functions to fill my vectors, if it helps to understand the functioning.– Marv
When you do a [mcve] you warn me, for now you can not answer, reaches the point of using variables that do not exist, this code is not real.
– Maniero
I noticed some mistakes that you had commented, sorry for them. Anyway, the only thing I didn’t put in was the Sort() function that only serves what her name suggests, if something is missing please let me know.
– Marv