Solved

RTTI Question/Problem - trying to print class name

Posted on 1998-12-08
4
233 Views
Last Modified: 2011-09-20
On MSC VC++ 5.0 (SP3), I am trying to print out the name of a
class from a class member function.  In the first case
below, the compiler fatal errors.  In the second case, I get
an obscure error.  Any ideas on how to print a class' name
from withing the class like I am doing?  Thanks.

-------------------------------------------------
#include <iostream>
using namespace std;

#include <typeinfo>

//
// CBorder -- border of an object
//
class CBorder
{
public:
      void      PrintName() {  cout << typeid(this).name << endl; };

};

Here's the error:

junk.cpp(13) : fatal error C1001: INTERNAL COMPILER ERROR
  (compiler file 'msc1.cpp', line 1188)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

-------------------------------------------------
#include <iostream.h>
// using namespace std;

#include <typeinfo>

//
// CBorder -- border of an object
//
class CBorder
{
public:
      void      PrintName() {  cout << typeid(this).name << endl; };

};


Here's the error:

junk.cpp(13) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'overloaded function type' (or there is no acceptable conversion)
0
Comment
Question by:staggart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1179630
several problems.  name is a member function, not a data member.  also you want to get type info on what this points to, not this.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179631
Try

void PrintName() {  cout << typeid(*this).name() << endl; };
0
 

Author Comment

by:staggart
ID: 1179632
Yes, you are so right.  My mistake.  I think the compiler should not get a fatal error!

both typeid(this).name() and typeid(*this).name() work.

Thanks.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1179633
>>I think the compiler should not get a fatal error!
Agreed, but at least this one was easy to fix.  Sometimes it is a real pain.  I've been helping someone else (actually, I haven't) on a fatal compiler error for several days now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 78
Move constructor only called if marked noexcept? 6 114
Header of docx file 17 132
VS2015 Redefinition errors 4 91
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question