Errors with classes [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am creating a program, these are the requirements:
We are going to write a program to figure out if several elements are boiling or melting.
Substance---Melting Point---- Boiling Point
Zinc(Zn)--- (787.15°F)--------(1665°F)
Barium(Ba)- (1341°F)----------(3353°F)
Mercury(Hg)-(-37.89°F)------- (674.11°F)
Uranium(U)--(2070°F)----------( 7468°F)
Design a class that stores a temperature in a temperature member variable and has the appropriate accessor and mutator functions.
In addition to appropriate constructors, the class should have the following member functions for each of the elements:
• isZincMelting. This function should return the bool value true if the temperature stored in the temperature field is at or above the melting point and below the boiling point of zinc. Otherwise, the function should return false.
• isZincBoiling. This function should return the bool value true if the temperature stored in the temperature field is at or above the boiling point of zinc. Otherwise, the function should return false.
• Similarly you should have isBariumMelting, isBariumBoiling
• Similarly you should have isMercuryMelting, isMercuryBoiling
• Similarly you should have isUraniumMelting, isUraniumBoiling
Write a program that demonstrates the class.
The program should ask the user to enter a temperature, and then display a list of the substances that will melt at that temperature and those that will boil at that temperature.
For example, if the temperature is 1764 the class should report:
Zinc boils, Barium melts, Mercury boils and Uranium is solid
--This is what I have so far and I am getting some errors.
*In function 'int main()':
69:21: error: no matching function for call to 'Elements::isZincMelting()'
69:21: note: candidate is:
51:6: note: bool Elements::isZincMelting(float)
51:6: note: candidate expects 1 argument, 0 provided
In member function 'bool Elements::isZincMelting(float)':
56:1: warning: control reaches end of non-void function [-Wreturn-type]
Any help is appreciated. Thank you
#include<iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
class Elements
{
private:
float temp;
public:
//float getTemp() const; //Get user temp
void setTemp(float);
bool isZincMelting(float);
bool isZincBoiling(float);
bool isBariumMelting(float);
bool isBariumBoiling(float);
bool isMercuryMelting(float);
bool isMercuryBoiling(float);
bool isUraniumMelting(float);
bool isUraniumBoiling(float);
};
//setTemp
void Elements::setTemp(float temp)
{
float t;
getTemp = t;
}
bool Elements::isZincMelting( float t)
{
if (t >= 787.15 && t < 1665)
return true;
}
int main()
{
Elements info; //Define an instance of Element class
float getTemp;
cout << "Enter a temperature: " << endl;
cin >> getTemp;
//Store in temp of element info object
info.setTemp(getTemp);
info.isZincMelting();
return 0;
}

isZincMelting expects a float as argument you called it without arguments. Also your isZinMelting function should return false in if the condition is not met:
bool Elements::isZincMelting( float t)
{
if (t >= 787.15 && t < 1665)
return true;
return false;
}

Related

Here is a C++ program where it is showing that my function is out of scope. Can someone please tell me the errors and how to fix them [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Question
You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:
get_age, set_age
get_first_name, set_first_name
get_last_name, set_last_name
get_standard, set_standard
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this.
code:
#include <iostream>
using namespace std;
class Student{
private:
int age;
string fname;
string lname;
int std;
public:
void set_age(int a){age=a;;}
void set_fname(string f){fname=f;} //fname= first name
void set_lname(string l){lname=l;} //lname= last name
void set_std(int s){std=s;}
void get_age(){
cout<<age<<endl;
}
void get_fname(){
cout<<fname<<endl;
}
void get_lname(){
cout<<lname<<endl;
}
void get_std(){
cout<<std<<endl;
}
void to_string(){
string fl=lname+", "+fname; //fl stand for first and last
cout<<fl;
}
};
int main() {
Student z;
int a,s;
string f,l;
cin>>a;
z.set_age(a);
cin>>f;
z.set_fname(f);
cin>>l;
z.set_lname(l);
cin>>s;
z.set_std(s);
get_age();
to_string();
get_std();
return 0;
}
output
Solution.cpp:52:11: error: ‘get_age’ was not declared in this scope
cout<<get_age();
^~~~~~~
Solution.cpp:52:11: note: suggested alternative: ‘getdate’
cout<<get_age();
^~~~~~~
getdate
Solution.cpp:53:15: error: no matching function for call to ‘to_string()’
to_string();
^
You create one Student instance, z, and call misc. setter member functions on that instance. When you then call the functions to print some of the values you've set, you forgot to tell the compiler which instance you want to print the values for. Since you only have one instance, z, add that before the member functions:
z.get_age();
z.to_string();
z.get_std();
Improvement suggestion: Functions that does not return a value but that prints the value would be better named print-something rather than get-something.

when we call function, is argument parenthesis () is needed if no argument pass like func; vs func(); [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The static function getROI always returns 1 and also causes a warning :
the address of 'static float Account::getROI()' will never be NULL
#include <iostream>
using namespace std;
class Account
{
private:
int account_no;
int balance;
static float ROI;
public:
void setBalance(int x){balance=x;}
int getBalance(){return balance;}
void setAccountNo(int y){account_no=y;}
int getAccountNo(){return account_no;}
static void setROI(float z){ROI=z;}
static float getROI(){return ROI;}
};
float Account::ROI =0;
int main()
{
cout << "STATIC" << endl;
Account obj;
obj.setAccountNo(3435647);
obj.setBalance(1000000);
obj.setROI(4.9);
cout<<"Account No : "<<obj.getAccountNo()<<endl;
cout<<"Balance = "<<obj.getBalance()<<endl;
cout<<"Rate of int= "<<obj.getROI;
return 0;
}
You want obj.getROI(), not obj.getROI
It says the address will never be zero.
Which is true ... if the address were zero, reading it would be de-referencing a null pointer.
So you think you're looking at the data, but you're really looking at a pointer to the data, and (I presume) comparing that address to zero.

What is the correct way of implementing this custom priority_queue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to build a priority queue of an object having three elements having my own comparator. I tried the following code but is showing error .
#include<bits/stdc++.h>
using namespace std;
class triplet {
public:
int data;
int arr;
int index;
};
bool compare( triplet x, triplet y ){
if(x.data < y.data){
return true;
}
else{
return false;
}
}
int main(){
priority_queue<triplet,vector<triplet>,compare> pq1;
}
getting the following error
enter image description here
Comparison routines must return false if the two elements are equal, but your version returns true.
Try this instead
bool compare(triplet x, triplet y) {
if (x.data < y.data) {
return true;
}
else {
return false;
}
}
or to make it a little simpler like this
bool compare(triplet x, triplet y) {
return x.data < y.data;
}
But the important point is < not <=.
EDIT
Your code also uses compare incorrectly. compare is not a type, so it's not a valid argument for the priority_queue template.
The type of compare is bool (*)(triplet, triplet) so the right way to do this is to use that type in the template and pass the actual comparison function to the constructor. Like this
priority_queue<triplet, vector<triplet>, bool (*)(triplet, triplet)> pq1(compare);
BTW normally you would do this by creating a comparison functor, not a function. It's a little cleaner that way (and potentially more efficient).

c++ too few arguments to function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void kghg();
void menu(float kgs);
float kghg(float kilogram, float kgs){
kilogram=menu(kgs);
float hektogram;
hektogram=(kilogram*10);
return hektogram;
}
int main()
{
menu();
kghg()
return 0;
}
void menu(float kgs){
cout<<"Input values : ";cin>>kgs;
}
void kghg(){
float hektogram, kilogram;
hektogram=kghg(kilogram);
cout<<"Result : "<<hektogram<<endl;
}
when I try to compile this code, then show notice " error : void value not ignored as it though to be" and "error : too few arguments 'void menu(float)'".
Please someone help me.
You call the function menu as
menu();
Though it is declared as
void menu(float kgs)
You said that the function would take a float argument, but did not pass it one.
As a side note, it looks like you are trying to cin a value into kgs. If you want this to act as you expect, you need to pass kgs by reference, otherwise you will input a value to a copy of kgs, then the original would be unchanged. You could change menu to
void menu(float& kgs)
Then call it as
float kg; // Declare a float variable
menu(kg); // Pass that float by reference to your function

Error running classes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello Im taking some C++ studies right now and Im seem to be stuck in Classes. I have made this program where I need to get the price of a slice of pizza. Simply I dont want to input data yet, just with static data in that is already in place.
Code is this :
#include <iostream>
#include <cmath>
using namespace std;
class Circle
{
public:
Circle()
{
radius = 0;
area = 0;
}
inline void setRadius(double r)
{
radius = r;
}
inline double getArea(double radius)
{
return 3.14 * pow(radius, 2);
}
private:
double radius;
double area;`
};
class Pizza
{
private:
double price;
double size;
double costperinch;
Circle Object;
public:
Pizza()
{
price = 0;
size = 0;
costperinch = 0;
}
~Pizza();
inline void setPrice(double p)
{
price = p;
}
inline void setSize(double radius)
{
size = Object.getArea(radius);
}
inline double costPeSqIn(double size, double price)
{
double costperinch = size * price;
}
};
int main()
{
Pizza myPizza;
myPizza.setPrice(5.0);
myPizza.setSize(3.14);
cout << "The cost per square inch of the pizza is ";
cout << myPizza.costPeSqIn(myPizza.setSize, myPizza.setPrice);
return 0;
}
I get the following errors:
Error 1 error C3867: 'Pizza::setSize': function call missing argument
list; use '&Pizza::setSize' to create a pointer to
member c:\users\jorge\documents\visual studio 2013\projects\object
composition\object composition\main 7.21.cpp 91 1 Object Composition
Error 2 error C3867: 'Pizza::setPrice': function call missing argument
list; use '&Pizza::setPrice' to create a pointer to
member c:\users\jorge\documents\visual studio 2013\projects\object
composition\object composition\main 7.21.cpp 91 1 Object Composition
Your problem is the line
cout << myPizza.costPeSqIn(myPizza.setSize, myPizza.setPrice);
at the end of your code. Your compiler complains because setSize and setPrice are functions but you didn't provide them with arguments. I doubt that you wanted to do this in the first place any-ways.
I actually think you wanted to have:
inline double costPeSqIn()
{
return size * price;
}
as implementation of your costPeSqIn function and the line above with the error should be
cout << myPizza.costPeSqIn();
it makes way more sense that way to me.
The way you currently pass the setter functions into costPeSqIn doesn't make sense, also your original costPeSqIn declared a double return type but didn't return anything.
As for the problem with the destructor, you got:
~Pizza();
and therefore got your destructor declared. Once you declare a destructor yourself the compiler won't auto generate one for you, but since you don't have a definition anywhere either your code is ill formed.
To fix this you either gotta provide a definition of it, e.g.:
~Pizza()
{
// whatever code you want
}
or delete the declaration so that the compiler will auto-generate a default constructor for you.