-2

I'm on a C++ project that needs a vector of Derived classes, to go over it and call a method from the base class. For that, I did:

--Edit:

class BaseClass {
    private:
        float  attribute1;
        float  attribute2;

    public:
        constructor;
        virtual destructor;
        virtual float virtualMethod() { return (getter1() + getter2()*getter1());}

    protected:
        virtual float getter1();
        virtual float getter2();
}

Derived classes only have their own attributes and getters:

DerivedClassA : public BaseClass {
    private:
        std::string att1A;
        std::string att2A;
        int att3A;
    public:
        constructor;
        virtual destructor;
    protected:
        //getters for att1,att2 and att3;
}

DerivedClassB : public BaseClass {
    private:
        std::string att1B;
        std::float att2B;
        int att3B;
    public:
        constructor;
        virtual destructor;
    protected:
        //getters for att1,att2 and att3;
}

vector<BaseClass*> vector;
BaseClass *object1 = new DerivedClassA(string, string, int, float, float);
BaseClass *object2 = new DerivedClassB(string, float, float, float);
vector.push_back(object1);
vector.push_back(object2);

for (int i=0; i<vector.size(); i++) { cout << vector[i]->virtualMethod() << endl; } //virtualMethod returns a float.

The desired behavior is: when i create the object from the Derived Classes i give them some values. The interest ones are the 2 last floats in this case. The virtualMethod calls their values and complete the operation: firstfloat + secondfloat*firstfloat and show the return value.

When I try to run it, the program crashes in the virtualMethod call. Am I doing something wrong? I looked for some answers in another examples but they are more or less like this and I even try with iterator and nothing. Thanks!

Will
  • 24,082
  • 14
  • 97
  • 108
  • 6
    I think the key to a solution here is ... correct me if I'm wrong ... in the code that you don't show (virtualMethod()). – Robinson Jan 19 '16 at 10:43
  • Please edit your question with the definitions of `virtualMethod` in `BaseClass`, `DerivedClassA` and `DerivedClassB`. – TartanLlama Jan 19 '16 at 10:45
  • the code of the virtualMethod is only: return (getatribute1() + getatribute2()*getatribute3()) but it doesn't matter. I replaced for "return (0.2)" and nothing – Carlos de la Torre Jan 19 '16 at 10:46
  • 2
    @CarlosdelaTorre Then please create an [MCVE](http://stackoverflow.com/help/mcve). – BoBTFish Jan 19 '16 at 10:46
  • Carlos, edit the question, don't post a comment with the code. – Mad Physicist Jan 19 '16 at 10:51
  • 1
    Your example is still far from complete. I suggest you investigate by debugging your code in more detail, and by adding smoke test code, such as calling object1->virtualMethod() right after creating object1, and the same with object2. You can also add some logging lines with cout instructions. At some point you will be able to locate the crash. – Daniel Daranas Jan 19 '16 at 11:20
  • 1
    Minor thing (well debatable) but naming your variable `vector vector;` Really?! – LiamT Jan 19 '16 at 13:39
  • Now one of the five people who reopened the question despite the code being incomplete should tell you where your mistake is. – Daniel Daranas Jan 19 '16 at 15:38

1 Answers1

0

the code isn't complete and is far away beeing compiled the way you describe it, perhaps you better post the complete code, not just snippets that don't show the problem.

What i see is, that you only rewrite parts of the virtual functions (which leads to the derived classes using the base classes implementation) VirtualMethod() e.g. returns a float, and there is no virtualMethod() for std::string. Keep in mind that "int foo()" and "std::string foo()" are 2 complete different functions, and that "std::string getter1()" does not "override float getter1()"

perhaps your virtualMethod is better implemented using a template?

Tommylee2k
  • 2,683
  • 1
  • 9
  • 22