error: could not convert... (strings)

Dear community,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>

using namespace 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);
            return true;
        }
        else return false;
    }
    string sucheNummer(string name_, int PIN_) {
        if (PIN_=PIN) {
            for (unsigned int i=0; i<Telefonbuch.size(); i++) {
                if (name_=Telefonbuch[i].name)                       // THIS IS THE LINE WITH THE PROBLEM!
                    return Telefonbuch[i].nummer;
            }
            return "0";
        }
        else return "-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?

Thank you very much
Madding
Last edited on
Hi,

Use == rather than = in all of your if statements. = is assignment, == is equality comparison.

Cheers
The expression:

name_=Telefonbuch[i].name

evaluates to a string. You're then trying to use that string as if it were a boolean expression, as a condition in an if statement.

I suspect you really meant to write:

name_ == Telefonbuch[i].name
The same idea for the other 2 if statements as well :+)
Oh, yes - you're using = for comparison in several places. That's a mistake.
Ah, yes, you're right - I should have known that ;) Thank you very much.
Topic archived. No new replies allowed.