?
Solved

RTTI Question/Problem - trying to print class name

Posted on 1998-12-08
4
Medium Priority
?
244 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 400 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

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 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