Hi all,

I am learning about RTTI, but I really don't know where the error is. Please check it out for me.

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

class Base
{
    virtual void f() {};    // make Base polymorphic
    // ...
};

 class Derived1: public Base
  {
    // ...
};

class Derived2: public Base
{
    // ...
};

int mian()
{
    Base *p, baseob;
    Derived1 ob1;
    Derived2 ob2;

    p = &baseob;
    cout << "p is pointing to an object of type ";
    cout << typeid(*p).name() << endl;

    p = &ob1;
    cout << "p is pointing to an object of type ";
    cout << typeid(*p).name() << endl;

    p = &ob2;
    cout << "p is pointing to an object of type ";
    cout << typeid(*p).name() << endl;

    return 0;
}

The compile's error is the following:

    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/rtti.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

Best Regards,
zawpai

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
Code tags, learn to use CODE TAGS

After 62 posts and a lot of reminders (I told you 3 times already) you're still not using code-tags. Edit your post and put the code in [code=cpp] //your code here [/code] tags.

Did you even read the error message? It says it can't find main(). Now look in your code: int mian() . See it?

Comments
Quite so!

but I really don't know where the error is.
The compile's error is the following:
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

Change mian() => main().

Also, really please use the code-tags feature when you post code.
http://www.daniweb.com/forums/misc-explaincode.html

EDIT( this post just became so redundant because Niek_e cooled down a bit ... :icon_wink: )

Hi niek_e & mitrmkar,

Next time, I will use the code-tags to post my codes.
But, the program still have a problem. When the program arrive this line
cout << typeid(*p).name() << endl;
, the system will prompt out debug error window. How should i do?


zawpai

cout << typeid(*p).name() << endl;
, the system will prompt out debug error window.

You should have posted the error message you are receiving, so I am guessing that either the pointer does not point to a valid object or you have not compiled with "enable RTTI" -option enabled.
You can find the RTTI option by opening Project/Settings/C/C++ tab and selecting "C++ Language" from the Category list.

Hi mitrmkar,

You are right. I should enable RTTI mode in setting. Now, my code can be run well.
I just a learner in C++. If so, I am not familiar in debugging.

Thanks a lot,

zawpai

This article has been dead for over six months. Start a new discussion instead.