0

I am using Code Blocks v16*

I got this error and I cannot figure out what seemed to be the problem:

Person.cpp:17:62: note: mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘void’ std::cout << "Birthdate: " << BirthdateObj.showBirthDate(); ^ Process terminated with status 1 (0 minute(s), 0 second(s)) 1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Here is my simple program:

Person.h

#ifndef PERSON_H
#define PERSON_H

#include <Birthdate.h>

class Person
{
    public:
        Person(int age, Birthdate BirthdateObj);
        virtual ~Person();

        void showPersonInformation();

    private:
        int age;
        Birthdate BirthdateObj;
};

#endif // PERSON_H

Person.cpp

#include "Person.h"
#include <iostream>

Person::Person(int age, Birthdate BirthdateObj) : age(age), BirthdateObj(BirthdateObj)
{
    //ctor
}

Person::~Person()
{
    //dtor
}

void Person::showPersonInformation()
{
    std::cout << "Current age: " << age << std::endl;
    std::cout << "Birthdate: " << BirthdateObj.showBirthDate();
}

Birthdate.h

#ifndef BIRTHDATE_H
#define BIRTHDATE_H


class Birthdate
{
    public:
        Birthdate(int day, int month, int year);
        virtual ~Birthdate();

        void showBirthDate();

    private:
        int day, month, year;
};

#endif // BIRTHDATE_H

Birthdate.cpp

#include "Birthdate.h"
#include <iostream>

Birthdate::Birthdate(int day, int month, int year) : day(day), month(month), year(year)
{
    //ctor
}

Birthdate::~Birthdate()
{
    //dtor
}

void Birthdate::showBirthDate()
{
    std::cout << day << "/" << month << "/" << year;
}

main.cpp

#include <iostream>
#include "Birthdate.h"
#include "Person.h"

int main()
{

    Birthdate BirthdateObj(7, 16, 1995);
    //Birthdate *pBirthdateObj = &BirthdateObj;

    Person PersonObj(20, BirthdateObj);
    Person *pPersonObj = &PersonObj;

    pPersonObj->showPersonInformation();
    //pBirthdateObj->showBirthDate();

    return 0;
}
Fur
  • 81
  • 9
  • You seem to have a some misunderstandings about how functions and their return values work. Consider picking up a [great book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to ensure you're not learning from questionable material. – Captain Obvlious May 21 '16 at 15:12
  • 1
    BirthdateObj.showBirthDate() returns void which you cannot pass to std::cout. Btw you know you can just call PersonObj.showPersonInformation(), right? – stijn May 21 '16 at 15:13
  • @stijn Hello, yes I got it now. Anyways for me(just for me or maybe some other people), having pointer to an object and access its member via -> is beautiful lol. Well you could tell me if it is a bad practice or not tho. – Fur May 21 '16 at 15:16
  • Using raw pointers when not needed is generally already frowned upon. But if the reason is you find them beautiful, it is *definitely* bad practice. – stijn May 22 '16 at 06:46
  • @stijn Hello there, do you know qt? They do this "QSpinBox *sb = new QSpinBox" I think that is what I was looking for. – Fur May 22 '16 at 14:30
  • That's why I said 'when not needed'. Qt is special because they heavily rely on a parent-child model for managing ownership of objects. – stijn May 22 '16 at 17:44

1 Answers1

0

Your birthday function returns void instead of a string. As cout cannot output void to standard output, this is where your error is coming from. Change this line:

std::cout << "Birthdate: " << BirthdateObj.showBirthDate();

to:

std::cout << "Birthdate: ";
BirthdateObj.showBirthDate();

Alternatively, you could change the return type of showBirthdate to string and keep the above line as is.

user3147395
  • 541
  • 3
  • 7
  • Hello thank you so much, I have figured it out now. Does it have any term or not? I will choose your post as an answer. – Fur May 21 '16 at 15:19
  • What do you mean by *term*? – PcAF May 21 '16 at 15:27
  • @PcAF Is there any way that I do not have to make a class object and just make a pointer out of it? – Fur May 21 '16 at 15:30