0

Hi everyone I'm new here. I just started programming in C++ and I do not seem te get my getter method working. Here is my code:

#pragma once
class Person
{
private:
    std::string name;
    int age;
    double weight;
    double height;

public:
    Person();
    Person(std::string _name, int _age, double _weight, double _height)
    : name{ _name }, age{ _age }, weight{ _weight }, height{ _height }
    {}
    ~Person();


    std::string get_name() { return name; }
    int get_age() { return age; }
    double get_weight() { return weight; }
    double get_height() { return height; }


};

When I try to call the get_name function I get an error, however the other getter methods work just fine.

I called the method like this:

Person *guy = new Person("Chad", 30, 70.0, 185.0);
cout << "Name = " << guy->get_name() << " Age = " << guy->get_age()<< "Weight = " << guy->get_weight() << "Height = " << guy->get_height() << endl;

But is give me the following error:

Error (active) E0349 no operator "<<" matches these operands Project2 c:\Users\Abdullah\source\repos\Project2\Project2\Source.cpp 37

The error disappears as soon as I erase the get_name() method. Does anyone have an idea why this is?

TheNinjaKing
  • 27
  • 2
  • 8
  • 2
    could you give an actual (not) compilable example? Also, don't use new. – The Techel Aug 27 '17 at 13:52
  • Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. Also when asking question about build-errors, please include the *complete* output, in full and unmodified, including possible informational notes. – Some programmer dude Aug 27 '17 at 13:55
  • I somehow fixed it by adding #include at the top of my program. Still don't not why it worked tho. – TheNinjaKing Aug 27 '17 at 13:55
  • It worked because you included the missing definition of `operator<<`. How is the compiler supposed to know a function exists if you don't include its declaration? – StoryTeller - Unslander Monica Aug 27 '17 at 13:56
  • Regarding the "fix", if you don't include `` the compiler doesn't know anything about `std::string`? Furthermore the [string output operators](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) are not members of the output stream function, and for your implementation of the standard library they seem to be declared in the `` header. – Some programmer dude Aug 27 '17 at 13:57

0 Answers0