0

Simply, I am unable to call non-inherited methods from an object in a Vector.

I'm using Qt Creator 2.7.0 which to my knowledge doesn't use the full C++11 yet (?) - I'm unsure if this is what is causing the following problem (although I'm very sure it's me not the IDE/Compiler):

I have 3 classes which inherit from the base class and have an additional primitive/getter/setter each.

Simply, my classes look like:

class A 
{
  public:
    virtual std::string getName() = 0 ;
    virtual void setName(std::string) = 0 ;
    virtual int getNumber() ;
    virtual void setNumber(int) ;
  protected:
    std::string name ;
    int number ;
}

class B : public A
{
  public:
    std::string getName() ;
    void setName(std::string) ;
    int getNumber() ;
    void setNumber(int) ;

    std::string getEmail() ;
    void setEmail(std::string) ;
  protected:
    std::string email ;
}

In my main I have a Vector of pointers, i.e.:

std::vector<A*> contacts ;

//Add Pointers to Vector
A *a ;
B *b ;
contacts.push_back(a) ;
contacts.push_back(b) ;

I then Check Object Class Type, to ensure it's of class type B.

if (dynamic_cast<B*>(contacts.at(1)) != NULL) //nullptr not working in Qt yet
{

I can access the getters & setters of Class A, but not B:

std::string name = contacts.at(1)->getName() ; //Works

std::string email = contacts.at(1)->getEmail() ; //Compiler Error: 'class A' has 
                                                 //no member named 'getEmail'

}

The Error ('class A' has no member named 'getEmail') is happening at compile time and not at run-time.

I can't see it being Object Slicing as this should all be polymorphic, should I be using some type of C++ Casting?

Any help or kick in the right direction would be appreciated, thanks.

Community
  • 1
  • 1
nldn
  • 44
  • 9
  • 1
    Note that Qt Creator has no say in whether you're writing C++11 code or not. For example, `nullptr` works perfectly fine even if Creator doesn't highlight it as a keyword, and so does every other C++11 feature. It's the compiler you're using that decides what you can use, not the editor or IDE. – Nikos C. Jul 13 '13 at 08:02
  • I had found previously contradicting comments here and on the Qt forums which led me to believe that there was a problem between the compiler and the IDE. Having read your comment I found [this compiler flag](http://stackoverflow.com/a/16509983/1312555) & so I can use 'nullptr' – nldn Jul 13 '13 at 09:03

3 Answers3

2

You need to use a dynamic_cast to get a pointer to the derived type. You are not actually doing that, you are calling a B method on an A*:

contacts.at(1)->getEmail() ; // look, no dynamic_cast

You would need to do the equivalent of this:

B* b = dynamic_cast<B*>(contacts.at(1));
std::string email = b ? b->getEmail() : "N/A";
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

contacts pertains to A which is base class.! You've to perform the operation on derived one.!

Instead of contacts.at(1)->getName use vector<B*> b->getName.

Hope this clears :)

Frankenstein
  • 420
  • 1
  • 5
  • 17
0

Your contacts vector contains A*. C++ verifies types at compilation so there is no problem when executing the code. Since contacts is of A* type, only methods of class A can be called. In your case, you want to cll a B method on an objct that might be a B, or not! C++ is sure it is an A, but it might be a C (if class C inherits from A). Whay you want to do is to get the instance of the B object in the vector and cast it into a B so you can call the method: B* b = dynamic_cast<B*>(contacts.at(1)); noyw use the b to do whatever you want.

excalibur1491
  • 1,201
  • 2
  • 14
  • 29