1
#include <iostream>
#include <string>

using namespace std;

class Person{
    public:
        Person(string n, int a, string g) {
            setName(n);
            setAge(a);
            setGender(g);
        }
        void setName(string x) {
            name = x;
        }
        void setAge(int x) {
            age = x;
        }
        void setGender(string x) {
            gender = x;
        }
        get() {
            return "\nName: " + name + "\nAge: " + age + "\nGender: " + gender + "\n";
        }
    private:
        string name;
        int age;
        string gender;
};


int main() {

    return 0;
}

That is my code, all I wanted to do was make a basic class with a constructer, with three parametres that defines the name, age and gender, for some reason, when I try and run this to check that everything is working fine, I get an error stating (line 23) : mismatched types 'const __gnu_cxx::__normal_iterator.

Can someone please help by fixing my code? I really don't understand what I've done wrong, thanks in advance!

samir
  • 759
  • 2
  • 8
  • 13

5 Answers5

4

Problem is here:

public:
    ...
    get() {
        return "\nName: " + name + "\nAge: " + ... + gender + "\n";
    }

since the return value of this method is not defined and you are trying to append a value of int to std::string with +, which isnot possible. Since you need more complex output-formatting than just appending strings, you can use std::ostringstream:

public:
    ...
    std::string get() {
        std::ostringstream os;
        os << "\nName: " << name << "\nAge: " << ... << gender << std::endl;
        return os.str();
    }

just don't forget #include <sstream>


Side note:

Person(string n, int a, string g) {
    setName(n);
    setAge(a);
    setGender(g);
}

is within the Person class, you can access private members directly:

Person(string n, int a, string g) : name(n), age(a), gender(g) { }
LihO
  • 41,190
  • 11
  • 99
  • 167
2

Your get function needs a return type. Furthermore, in C++ you can't just + strings and other objects together freely. Try using a std::stringstream instead, which will let you feed in strings, numbers, etc:

string get() {
    basic_stringstream ss;
    ss << endl
       << "Name: " << name << endl
       << "Age: " << age << endl
       << "Gender: " << gender << endl;
    return ss.str();
}

You'll need to add a #include <sstream> at the top.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
2

You cannot add int type (age) to string type (name, gender). Convert age to string first.

Check C++ concatenate string and int

Community
  • 1
  • 1
cpp
  • 3,743
  • 3
  • 24
  • 38
2

2 mistake in your code.

1.you are not using return value as string in your get method. 2.you cant add string and int directly.

Check how to add string and int here

Community
  • 1
  • 1
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
1

Im not sure, but I think its because your get() function does not declare a return type. It should be string get(). That being said, its a weird error message for such an error.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122