0

I don't know why I get this error. It's like a*a treats the a* as a pointer (float* to float* is it the same as float to float ). Apparently the problem is kind of resolved when I declare a in the sub classes, but the point is that I want my sub classes automatically to have that from my parent class.

One more thing when i add "float a;" to every subclass the program can run. So i did that just to test if it would work but no. The float a doesn't get a value in setA. I just added cout<<a; after a=b;.

#include"Oblik.h"
#include<cmath>

using namespace std;

class Jednakostranicni : public GeometrijskaFigura{

    float P(){
        return (a*a*sqrt(3))/4; //12    13  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
    }
    float O(){
        return a+a+a; //15  12  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'float*' and 'float*' to binary 'operator+'
    }
    
};

class Kvadrat : public GeometrijskaFigura{
    
    float P(){
        return a*a;//23 12  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
    }
    float O(){
        return a+a+a+a;//26 12  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'float*' and 'float*' to binary 'operator+'
    }
    
};

class Krug : public GeometrijskaFigura{
    
    float P(){
        return a*a*3.14;//34    12  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'float*' and 'float*' to binary 'operator*'
    }
    float O(){
        return 2*a*3.14;//37    12  C:\Users\Name\Desktop\prvi.cpp  [Error] invalid operands of types 'int' and 'float*' to binary 'operator*'
    }
    
};

int main(){
    
    GeometrijskaFigura *f;
    int x;
    
    cin>>x;
    
    f->setA(x);
    
    f=new Jednakostranicni;
    cout<<"Jednakostranicni-P: "<<f->P()<<" O: "<<f->O()<<endl;
    f=new Kvadrat;
    cout<<"Kvadrat-P: "<<f->P()<<" O: "<<f->O()<<endl;
    f=new Krug;
    cout<<"Krug-P: "<<f->P()<<" O: "<<f->O()<<endl;
    
    
    
    return 1;
    
    
}

// this is Oblik.h file code
#include<iostream>

using namespace std;

class GeometrijskaFigura{
    protected: float a;
    public:
        
        void setA(float b){
         a=b;
        }
        
        virtual float P()=0;
        virtual float O()=0;
};

1 Answers1

0

You have undefined behavior as f is not initialized:

GeometrijskaFigura *f;
int x;
    
std::cin >> x;
    
f->setA(x); // UB: use uninitialized f.

Should be

int a;

std::cin >> a;

Jednakostranicni j;
j.setA(a);
std::cout << "Jednakostranicni-P: " << a.P() << " O: " << a.O() << std::endl;

Kvadrat k;
k.setA(a);
std::cout << "Kvadrat-P: " << k.P() << " O: " << k.O() << std::endl;

Krug krug;
krug.setA(a);
std::cout << "Krug-P: " << krug.P() << " O: " << krug.O() << std::endl;
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Isn't UB a runtime thing? At least it only gets interesting after passing the build.... – Yunnosch Dec 07 '20 at 09:52
  • 1
    strictly speaking not answering the quesiton. OP asks for "invalid operands of types 'float*' and 'float*' to binary 'operator*'" but there is no such thing in the posted code. Though you do adress the problem in the code that was posted – 463035818_is_not_an_ai Dec 07 '20 at 09:52
  • My assignment was to declare 1 object in main and with the "new" operator declare other subclasses trough that object. Also, I just put cout< – Ethereallie Dec 07 '20 at 10:06