I know the thermometer problems have been done to death but I thought I would give this a shot.
I keep getting the error messages "use of undeclared identifier 'converterc'" and "use of undeclared identifier 'converterf'". Any ideas?
Spike
#include <iostream>
#include "converters.h"
using namespace std;
int main()
{
int degree;
int weehoo;
cout<<"\n\n\n\t\t Enter the temperature : ";
cin>>degree;
cout<<"\n\n\t\t If the temperature is in Celsius enter 0, if Farenheit enter 1 :";
cin>>weehoo;
if (weehoo==0)
{
cout<<"\n\n\t\tThe temperature in Farenheit is "<<converterc(degree,weehoo)<<endl;
}
else
{
cout<<"\n\n\t\tThe temperature in Celsius is "<<converterf(degree,weehoo)<<endl;
}
return 0;
}
#ifndef __again_converters_h
#define __again_converters_h
#endif
#pragma once
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
public:
void setCelcius (float c) {degreeC=c;}
void setFarenheit (float f) {degreeF=f;}
float getCelcius (void){return degreeC;}
float getFarenheit (void){return degreeF;}
Thermometer (float degree=0,float f=0, float c=0,float outtemp=0);
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};
converters.cpp file
#include "converters.h"
float Thermometer::converterf(int degree,int weehoo)
{
degreeC=((degree-32) * (.5556));
return degreeC ;
}
float Thermometer::converterc(int degree,int weehoo)
{
degreeF=((1.8)*degree)+32;
return degreeF;
}
converterc and converterf are member function of the class Thermometer but you're calling them without a Thermometer instance.
How about creating a Thermometer instance in your main?
Thermometer tm;
tm.converterc(degree, weehoo);
converterc and converterf are functions in your class. This means they are there to be called on an object being an instance of this class or class derived from this.
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
//...
public:
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};
int degree = 1;
int weehoo = 2;
Thermometer t; //initialize it properly if this is needed before calling functions
float f = t.converterc(degree,weehooo);
using these functions in the way you did this:
float f = converterc(degree,weehooo);
is possible as:
float f = Thermometer::converterc(degree,weehooo);
but then they have to be static what means they don't have this pointer and are common to whole class (still you can call them using an instance of class but it is not necessary):
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
//...
public:
static float converterc(int degree,int weehoo);
static float converterf(int degree,int weehoo);
};
Related
I'm trying to generate a terrain using perlin noise, to improve the quality of the terrain, I want to use multiple noises at once. So I have written a class that should to that for me. Here are the hpp and cpp files:
#include "perlinNoise.hpp"
class MultiPerlinNoise: public PerlinNoise {
public:
MultiPerlinNoise();
std::vector<PerlinNoise*> perlinNoises;
float octaveNoise(float x, float y);
};
cpp:
#include "multiPerlinNoise.hpp"
MultiPerlinNoise::MultiPerlinNoise():
PerlinNoise(0) {
}
float MultiPerlinNoise::octaveNoise(float x, float y) {
float sum = 0.0f;
for(int i = 0; i < perlinNoises.size(); i++)
sum += perlinNoises[i]->octaveNoise(x, y);
return sum;
}
The PerlinNoise class is a wrapper around the code for a octave peril noise I found on the internet. It looks like this:
#include "sivPerlinNoise.hpp"
class PerlinNoise {
public:
PerlinNoise(unsigned int seed);
float octaveNoise(float x, float y);
float frequency;
float multiplier;
int octaves;
unsigned int seed;
float offset;
private:
siv::PerlinNoise perlinNoise;
};
cpp:
#include "perlinNoise.hpp"
PerlinNoise::PerlinNoise(unsigned int seed):
perlinNoise(seed), frequency(2.0f), multiplier(1.0f), octaves(1), seed(seed), offset(0.0f) {
}
float PerlinNoise::octaveNoise(float x, float y) {
return perlinNoise.octaveNoise(x / frequency, y / frequency, octaves) * multiplier + offset;
}
Now the problem is, that when I pass a pointer to my noise into my map class, the function always return 0.0f. This is how the constructor of my map class looks like:
Map::Map(PerlinNoise *noise, Shader *shader, const RenderData *data):
noise(noise), shader(shader), data(data), texture("resources/textures/stones.png") {
printf("%f\n", noise->octaveNoise(-(CHUNK_SIZE / 2.0f) + 0.0f, -(CHUNK_SIZE / 2.0f) + 0.0f));
update(glm::vec3(0.0f));
}
When I don't use a pointer to my noise everything is working as it should. How can this be fixed?
You need to declare octaveNoise as virtual, so the method can be overridden by inheriting classes:
class MultiPerlinNoise: public PerlinNoise {
public:
MultiPerlinNoise();
std::vector<PerlinNoise*> perlinNoises;
virtual float octaveNoise(float x, float y);
};
When I run this code and create an instance of cylinderType by passing four parameters, debugger shows the height I want but, radius=x=y=0. So when I call method printVolume() on this object, it displays '0'.
Am I missing something important with inheritance?
Thank you~
#include <iostream>
using namespace std;
class circleType
{
public:
circleType();
circleType(double r);
double getArea() const;
private:
double radius;
};
class cylinderType : public circleType
{
public:
cylinderType(double h, double r);
void printVolume() const;
private:
double height;
};
int main()
{
cylinderType cylinderA(2, 4);
cylinderA.printVolume();
return 0;
};
circleType::circleType()
{
radius = 0;
};
circleType::circleType(double r)
{
radius = r;
};
double circleType::getArea() const
{
return (3.14 * radius* radius);
};
cylinderType::cylinderType(double h, double r)
{
circleType::circleType(r);
height = h;
};
void cylinderType::printVolume() const
{
cout << (circleType::getArea() * height);
};
I am new to coding and I am trying to write a program that has polymorphism but my rectangle part is not working. I have tried to add variables, in all three files to accommodate it but I keep getting errors. The code below is my latest attempt to fix this. The error that I am getting now is [error] request for member 'area' in 'r' which is non-class type 'float' and for parameter in 'r' which is non-class type 'float'. I am at a lost as to how to fix this at this point. Please help if you can!
Main.cpp
#include <iostream>
#include "shape.h"
#include "shape.cpp"
using namespace std;
int main() {
float r, a, b, a1, b1;
cout<<"This program will ask you to input some data in order to find the area and the parameter of 3 shapes."<<endl;
cout<<"\nInput the circles radius --everything should be in inches (i.e 5):";
cin>>r;
Circle c(r);
cout<<"\nPlease input two side of the Right Triangle excluding the hypotenuse-- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
RTriangle rt(a,b);
cout<<"\nPlease input two side of the Rectangle -- everything should be in inches( i.e 5 5): ";
cin>>a>>b;
Rectangle r(a1,b1);
cout<<"\n\nThe Circles Area is:"<<c.area()<<" inches, The Parameter is:"<<c.parameter()<<" inches"<<endl;
cout<<"The Rectangle Area is:"<<r.area()<<" inches, The Parameter is:"<<r.parameter()<<endl;
cout<<"The Right Triangle Area is:"<<rt.area()<<" inches, The Parameter is:"<<rt.parameter()<<" inches"<<endl;
cout<<"Thanks once agin for using this program for your AREA and PARAMETER needs!"<<endl;
system ("PAUSE");
return 0;
}
Shape.cpp
#include"shape.h"
Shape::Shape(){
sideA = sideB = 0;
}
Shape::Shape(int a, int b){
sideA = a;
sideB = b;
}
//these will get overrided
float Shape::area(){return 0;}
float Shape::parameter(){return 0;}
//rectangle definations
Rectangle::Rectangle(float a, float b):Shape(a,b){
//calling parent class constructor
}
float Rectangle::area(){
return sideA*sideB;
}
float Rectangle::parameter(){
return 2*(sideA+sideB);
}
//right triangle definations
RTriangle::RTriangle(float h, float w):Shape(h, w){
}
float RTriangle::area(){
return 0.5*sideA*sideB;
}
float RTriangle::parameter(){
float hyp = sqrt(sideA*sideA + sideB*sideB);
return sideA + sideB + hyp;
}
//circle definations
Circle::Circle(float r){
sideA = r;
}
float Circle::area(){
return 3.14 * sideA * sideA;
}
float Circle::parameter(){
return 2 * 3.14 * sideA;
}
Shape.h
#ifndef SHAPES
#define SHAPES
#include<cmath>
class Shape {
protected:
float sideA, sideB;
float radius;
public:
Shape();
Shape(int,int);
virtual float area();
virtual float parameter();
};
class Rectangle : public Shape{
public:
Rectangle(float a, float b);
float area();
float parameter();
};
class RTriangle : public Shape{
public:
RTriangle(float h, float w);
float area();
float parameter();
};
class Circle : public Shape{
public:
Circle(float r);
float area();
float parameter();
};
#endif
Take a look at Main.cpp, you will find two lines like
float r, a, b, a1, b1;
and
Rectangle r(a1,b1);
As you can see r is defined twice. I suggest to replace Rectangle r(a1,b1); with Rectange rect(a1, b1);
This should help.
I dont understand why i am not getting total.
The question is
Define a class student with the following specification:Private
members of class student admno integer sname 20 character eng.
math, science float total float ctotal() a function to calculate eng +
math + science with float return type. Public member function of
class student Takedata() Function Public member function of class
student Takedata() Function float ctotal() a function to calculate
eng + math + science with floa return type. Public member function
of class student Takedata() Function
#include<stdio.h>
#include<iostream>
using namespace std;
class Student
{
private:
int admno;
char sname[20];
float english,maths,science;
float total;
float ctotal()
{
total=(english+maths+science);
return(total);
}
public:
void Takedata()
{
cout<<"Enter the value of admno:";
cout<<" sname :";
cout<<"eng :";
cout<<"science:";
cout<<"maths:";
cin>>admno;
cin>>sname;
cin>>english;
cin>>science;
cin>>maths;
}
Student(): total(0.0) //constructor
{
}
friend float func(Student);
void Showdata()
{
cout<<"adm no:"<<admno;
cout<<"sname:"<<sname;
cout<<"eng"<<english;
cout<<"science"<<science;
cout<<"maths"<<maths;
}
};
float func(Student t)
{
t.total;
return t.total;
}
int main()
{
Student s1;
s1.Takedata();
s1.Showdata();
cout<"total is:";
cout<<func(s1);
}
Need to make either total or method ctotal() public.
Then call either one of them from
public:
float total;
float func(Student t)
{
t.total;
return t.total;
}
OR
public:
float ctotal() {
total=(english+maths+science);
return(total);
}
float func(Student t)
{
return t.ctotal();
}
func should be defind like this
float func(Student t)
{
return t.ctotal();
}
also edit
cout<"total is:";
to
cout<<"total is:";
Replace t.total with t.ctotal() in
float func(Student t)
{
t.total;
return t.total;
}
I am learning OpenGL w/ C++. I am building the asteroids game as an exercise. I'm not quite sure how to override the constructors:
projectile.h
class projectile
{
protected:
float x;
float y;
public:
projectile();
projectile(float, float);
float get_x() const;
float get_y() const;
void move();
};
projectile.cpp
projectile::projectile()
{
x = 0.0f;
y = 0.0f;
}
projectile::projectile(float X, float Y)
{
x = X;
y = Y;
}
float projectile::get_x() const
{
return x;
}
float projectile::get_y() const
{
return y;
}
void projectile::move()
{
x += 0.5f;
y += 0.5f;
}
asteroid.h
#include "projectile.h"
class asteroid : public projectile
{
float radius;
public:
asteroid();
asteroid(float X, float Y);
float get_radius();
};
main.cpp
#include <iostream>
#include "asteroid.h"
using namespace std;
int main()
{
asteroid a(1.0f, 2.0f);
cout << a.get_x() << endl;
cout << a.get_y() << endl;
}
error I'm getting:
main.cpp:(.text+0x20): undefined reference to `asteroid::asteroid(float, float)'
You can use the : syntax to call the parent's constructor:
asteroid(float X, float Y) : projectile (x ,y);
Ok, just figured it out.
I actually don't have asteroid constructors defined because I thought they would inherit. But I think I have to do the following in asteroid.h:
asteroid(float X, float Y) : projectile(X, Y){];
You need a asteroid.cpp.
Even though inheriting from projectile, for non-default constructors (i.e., asteroid(float,float)), you still need to define the child class constructor.
You'll also need to define get_radius, as it's not defined in your base class.
Here's how that might look (I've taken the liberty of passing values for radius into both ctors):
#include "asteroid.h"
asteroid::asteroid(float r)
: projectile()
{
radius = r;
}
asteroid::asteroid(float x, float y, float r)
: projectile(x, y)
{
radius = r;
}
float asteroid::get_radius()
{
return radius;
}