简体   繁体   中英

Invalid operands of types 'float*' and 'float*' to binary 'operator*'

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;
};

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM