#creating an object from another class

21 messages · Page 1 of 1 (latest)

restive falcon
#

I need help with this error so this code is supposed to create a pointer variable of type SelectionAlgorithm then initialize the pointer by creating an object of class AlgorithmSortHeap but I get this error and I dont know what im doing wrong

graceful atlasBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fleet cloak
#

I don't get what you are trying to do.
You can't assign a pointer of incompatible types

restive falcon
#

um so there is supposed to be 3 algorithm classes and all of them have select method in them and setalgorithm method is supposed to create an object of the selected class so that algorithm will be used

#

and this line were given to us like this so
this->algorithm = new AlgorithmSortHeap(k);

#

I dont get whats wrong

bitter solstice
#

I think you are attempting assignment of a polymorphic type.
To understand what is going wrong we need to see more code.

We need to see:
a) the declaration of variable TestBed::algorithm
b) the declaration of class AlgorithmSortHeap

restive falcon
#

yeah I am

#

this is algorithmsortheap

#

but I dont have algorithm in testbed

bitter solstice
bitter solstice
graceful atlasBOT
#

```cpp
int main() {
return 0;
}
```

restive falcon
bitter solstice
# restive falcon

Yep, as suspected.
Your AlgorithmSortHeap is not a kind of SelectionAlgorithm.
There is no inheritance there, so no polymorphism.

restive falcon
#

what

#

how do I fix that

pale flame
#

well you could just inherit

#

but I would recommend Concepts instead if you're allowed to use them

graceful atlasBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.