?
Solved

RTTI Question/Problem - trying to print class name

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

807 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