I am new to C++, for practicing, I am currently working on some exercises I found on the internet: http://www.wsl.ch/fe/gebirgshydrologie/massenbewegungen/wissenstransfer/Cpp/KlassenUebungen.pdf
If you understand German, you might have a look at it, otherwise I think it is not necessary to read the pdf in order to be able to answer my question. In the task on page 3 it says, a telephone number (nummer) should be dealt with as an integer; I changed that to a string, because it seemed better for me, if the number consists of e.g. 12 digits. The code I wrote to complete the task is the following:
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
struct Eintrag {
string name;
string nummer;
};
class SIMcard {
private:
vector <Eintrag> Telefonbuch;
int PIN;
public:
SIMcard(int PIN_): PIN(PIN_) {}
bool trageEin(string name_, string nummer_, int PIN_) {
if (PIN_=PIN) {
Eintrag eintrag;
eintrag.name = name_;
eintrag.nummer = nummer_;
Telefonbuch.push_back(eintrag);
returntrue;
}
elsereturnfalse;
}
string sucheNummer(string name_, int PIN_) {
if (PIN_=PIN) {
for (unsignedint i=0; i<Telefonbuch.size(); i++) {
if (name_=Telefonbuch[i].name) // THIS IS THE LINE WITH THE PROBLEM!
return Telefonbuch[i].nummer;
}
return"0";
}
elsereturn"-1";
}
};
int main() {
SIMcard MyCard(1234);
bool Versuch1 = MyCard.trageEin("Ich", "012345678901",1234);
bool Versuch2 = MyCard.trageEin("Du", "123456789012",1235);
if (Versuch1) cout << "Versuch 1 erfolgreich!" << endl;
else cout << "Versuch 1 erfolglos!" << endl;
if (Versuch2) cout<< "Versuch 2 erfolgreich!" << endl;
else cout << "Versuch 2 erfolglos!" << endl;
cout << MyCard.sucheNummer("Ich", 1234) << endl;
return 0;
}
In the line I commented, I get the following error: error: could not convert 'name_.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(&((SIMcard*)this)->SIMcard::Telefonbuch.std::vector<_Tp, _Alloc>::operator[]<Eintrag, std::allocator<Eintrag> >(i).Eintrag::name)))' from 'std::basic_string<char>' to 'bool'|
I do not understand where the problem is for the compiler. name_ is a string, Telefonbuch is a vector of Eintrag, and Eintrag.name is a string, so Telefonbuch[i].name should be a string as well for any 0<=i<Telefonbuch.size(), shouldn't it?