0

I am new in Abstract classes so please excuse any ignorant mistakes

The exercise is given from my school, so the main.cpp file is to be used, almost as it is

I am trying to create a simple calculator in Eclipse using C++11

There exists a simple Abstract class with two virtual methods.

The two derived classes are simply the "Result" and the "Const" classes. This is the header file of the Abstract class called

Expression.h

class Expression
{
public:
    Expression();
    virtual ~Expression();
   //methods
};

Following is the source file of Expression

Expression.cpp

#include "expression.h"
#include <iostream>

Expression::Expression(){}
Expression::~Expression(){}

Then I have created two classes called Const and Result

Const.h

#include <iostream>
#include "expression.h"

class  Const : public Expression
{
public:
    Const(int value);
    //inherited methods
private:
    int value;
};

and the source file

Const.cpp

#include "expression.h"
#include "Const.h"

Const::Const(int x)
{
    value=x;
};

//inherited methods

Result.h

#include <iostream>
#include "expression.h"
#include "Const.h"

class  Result : public Expression
{
public:
    Result(Const& c);
    //inherited methods
private:
    double value;
};

Result.cpp

#include "expression.h"
#include "Result.h"
Result::Result(Const& c)
{
    value=c.point;
};
//inherited methods

So what i need is to understand

main.cpp

#include <iostream>
#include "expression.h"
#include "const.h"
#include "result.h"
void testResult()
{
    Result  res (new Const(4));
    //Here an inherited method will be used to print the contents of object res
}
int main()
{
    testResult();
    return 0;
}

The problem i can't solve is the line

Result res (new Const(4));

The error i get is

Conversion from 'Const* to non-scalar type 'Result' requested

The thing is that what is described in this line should be used as it is, and i can't seem to find exactly what it is.

EDIT

The question as asked firstly was apparently misleading due to my fault, tried to fix the question so as to describe exactly my problem

  • Try provide a [mcve], this will not only let other people read but also help you find the root of the problem – Passer By Jul 09 '17 at 16:40

4 Answers4

0

If you had two derived classes DerivedClass1 and DerivedClass2 both derived from some BaseClass then to instantiate a pointer to a DerivedClass1 and use it in polymorphic way use:

BaseClass* p = new DerivedClass1;

To create a base class pointer to a DerivedClass2 use:

BaseClass* p = new DerivedClass2;

And not the:

DerivedClass1* p = new DerivedClass2;

In your example both Result and Const classes are derived from Expression class hence the confusion. To pass in an argument of SomeClass& type, create a temporary object of SomeClass and pass it in:

SomeClass o;
someFunction(o);
Ron
  • 14,674
  • 4
  • 34
  • 47
0

You started correctly, by creating a common base class for both Const and Result, but then completely ignored it. All your problems are indeed in this line:

Result  res = (new Const(4));

First of all, operator new in C++ returns a pointer, not a reference. Also, this would be a good place to make use of your base class:

Expression* res = new Const(4);

Since you declared methods evaluate() and print() as virtual, the object res is pointing to will be correctly resolved as an instance of Const when you call res->print() or res->evaluate().

This will use Const version of print(). If you want to use the Result version - abstract classes won't help you here, you need to use casting. Create your own operator=(Const &) in Result or operator Result() in Const.

Pontifex
  • 136
  • 6
  • I think your answer came closer to my problem, i suspect the line i cna't decipher is a new-operator overload, but am not sure Any help would be appreciated – Konstantinos Zafeiris Jul 09 '17 at 23:06
  • Situation in your edited question is simplier, although I believe the error would now be about conversion from `Const*` to `Const&`, wouldn't it? Modifying constructor of `Result` to take pointer instead of reference should solve it - even though the parameter being a reference makes much more sense, but since you cannot change `main()`... Just remember that object of type `Result` now takes ownership of newly created `Const`s and you have to `delete` them when you're done with them. ... And I still don't see where the base class would come into play. – Pontifex Jul 10 '17 at 09:35
  • Since the constructor is also inherited and ought to have the same parameteres with the base class, should i create a second one which takes pointer? That means that while having `Result();` i should create `Result(Const*)?` – Konstantinos Zafeiris Jul 10 '17 at 10:11
  • Constructors could not be inherited until C++11, and even now require specific syntax (see [here](https://stackoverflow.com/a/434784/8087509)). I don't know your exact requirements, but if this line: `Result res (new Const(4));` is to stay that way, I don't see an option other than overloading constructor. – Pontifex Jul 10 '17 at 10:17
  • Thanks for explaining me what the problem is I did some research based on the material you gave me, altough there is another problem Out of all the examples there was no overloaded constructor who took as parameter another class, even more specifically of another derived class I think i am coming to a dead end here – Konstantinos Zafeiris Jul 10 '17 at 11:08
  • Not sure what the problem is, in a similar manner to your existing `Result(Const& c);` you can declare a constructor accepting a pointer: `Result(Const* c);` or `Result(Expression* c);`. Then, in constructor body you would use operator `->` instead of `.` to dereference the pointer `c`. – Pontifex Jul 10 '17 at 11:36
  • I can't thank you enough for helping me. Firstly `Result(Const* c)` wouldn't work because it couldn't recognize what Const is even though i had included the Const.h file,nevertheless `Result(Expression* c)` worked. I guess it worked because any base class expression can be used by its derivates(?) – Konstantinos Zafeiris Jul 10 '17 at 12:26
  • It worked because that's what the `virtual` specifier is supposed to do - decide at runtime which derived class methods to use when provided with base class pointer. I don't understand why `Result(Const* c)` didn't work, though, maybe I'll check that later. – Pontifex Jul 10 '17 at 12:51
0

Class Result and Const are two different classes. This means the type conversion

Result *res = (new Const(4));

is not possible. To do what you want, since both classes inherit from Expression, do:

  Expression * res = new Result( objConst);

Where objConst is a Const object.

ytobi
  • 535
  • 1
  • 9
  • 19
0

One issue is that

double point= value;

doesn't initialize your member, but a new unused local variable.

just do

point = value;
Jarod42
  • 203,559
  • 14
  • 181
  • 302