Visual Studio 2017 C++, Can't Use Typeid() to Take Information Object, Missing Pointer? ; [Closed]

Thats my first post here xD.

Recently, I began to remind myself of knowledge in the field of c ++ after a few years of break. Previously, I worked in C #.

As part of the exercises, I started to write the code which is to generate the class of the car, and then to enter and write data. But I came across a problem. As part of the exercise I wanted to write the name of the object using the function typeid () and find out what is hash_code. But I got two mistakes and I can not do what. How to correctly declare a pointer.

#include "pch.h"
#include <iostream>
#include <string>

using namespace std;

class Car {
public:
    string _mark;
    string _model;
    int _year;
    int _course;

    void UploadData()
    {
        cout << "Set values" << endl;
        cin >> _mark;
        cin >> _model;
        cin >> _year;
        cin >> _course;
        cout << "Values uploaded"<<endl;
    }

    void Write()
    {
        cout << typeid(this).name <<" " << typeid(this).hash_code << " " << " mark " << _mark << " model " << _model << " year " << _year << " course " << _course;
    }
};

int main()
{
    Car test1;
    test1.UploadData();
    test1.Write();
}

Error Messege:

Severity Code Description Project File Line Suppression State Error C3867 'type_info::name': non-standard syntax; use '&' to create a pointer to member

Severity Code Description Project File Line Suppression State Error C3867 'type_info::hash_code': non-standard syntax; use '&' to create a pointer to member

1

name and hash_code are member functions of class std::typeinfo, so you need to use parentheses:

cout << typeid(this).name() <<" " << typeid(this).hash_code()
     << " " << " mark " << _mark << " model " << _model << " year " << _year
     << " course " << _course;

The error message you got is unfortunately misleading. The compiler thought you were attempting to do something entirely different and more advanced.

But note that typeid(this) is going to be exactly the same as typeid(Car*). If you instead wrote typeid(*this), that would be the same as typeid(Car). The typeid operator starts to get more interesting when used on a class with at least one virtual function....

1
Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest