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
Related
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 months ago.
Improve this question
Is there any way to use a class member function from outside?
As far as I know for this it would be necessary to somehow inject a private component into it.
For example (this is just an example the notFooClass function does not compile):
class FooClass{
private:
int i;
public:
FooClass(int x){
this->i = x;
}
int f(int num){
return i+num;
}
};
int notFooClass(int num1, int num2){
return FooClass(num1)::f(num2); //
}
int main(){
FooClass x(10);
std::cout<<x.f(5)<<std::endl;
std::cout<<notFooClass(10, 5)<<std::endl;
return 0;
The output should be:
15
15
Is it even possible to do something similar?
public methods can be called from outside. Thats what they are for.
notFooClass creates an instance and calls a member function. Thats basically the same as you do in main. The difference is only that you are using an unnamed temporary and wrong syntax:
int notFooClass(int num1, int num2){
return FooClass(num1).f(num2);
}
or with a named object to illustrate the similarity to main:
int notFooClass(int num1, int num2){
FooClass x(num1);
return x.f(num2);
}
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.
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 3 years ago.
Improve this question
I have a problem with constructors and member functions. When I try to compile the program, it shows: error:
‘int Vehicle::range()’ cannot be overloaded.
What should I do in this case ?
#include <iostream>
using namespace std;
class Vehicle{
public:
int passengers;
int fuelcap;
int mpg;
int range();
Vehicle(int p,int f,int m);
int range();
};
Vehicle::Vehicle(int p,int f,int m){
passengers=p;
fuelcap=f;
mpg=m;
}
int Vehicle::range(){
return mpg*fuelcap;
}
I haven't shown the main function because the problem must be here.
I expect the output of 336 and 168.
You're declaring Vehicle::range twice inside the class body:
int range();
Vehicle(int p,int f,int m);
int range();
Remove the second one and it should work.
Here's a minimal example, replicating the error you're getting.
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 4 years ago.
Improve this question
I am getting the error below:
..\src\test.cpp:17:20: error: expected unqualified-id before 'try'
friend int try(Complex);
Please help and tell me why this code is producing errors:
#include <iostream>
using namespace std;
class Complex
{
private:
int a, b;
public:
showData()
{
cout<<"\na= "<<a<<" b= "<<b;
}
Complex(int x, int y)
{ //constructer
a = x;
b = y;
}
friend int try(Complex);
//friend function
};
int try(Complex c)
{
cout<<"You areworking in try now";
cout<<"\noutput from friend fun : "<<c.a<<" "<<c.b;
}
int main()
{
Complex c1(3, 4);
try(c1);
return 0;
}
First let's simplify the problem to a Minimal Complete and Verifiable example that duplicates the problem:
int try;
Not much code is required because try is a reserved word. You cannot use try in a program without the compiler expecting a try/catch` block.
Solution: Do not use try as an identifier. Instead use try_func or something that describes what is being tried.
Additional note: showData() needs a return type. Most likely it should be void showData()
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
I have seen other similar questions being asked but I could not figure out what the problem is. I have a declaration in an inventory class as:
class Inventory
{
Public:
void print();
void sell(Item*);
void add();
void find(string);
Private:
Item* first;
}
And then in the inventory.cpp I have:
void sell(Item* item_name)
{
..........................
}
And the error comes from calling it in main() as:
Inventory store_inventory;
Item* cur_item;
cout<<"Item name: ";
string name;
cin>>name;
cur_item = find(name); //find returns Item*
store_inventory.sell(cur_item);
The error is one the line for the call to sell. Any ideas?
The definitions need to indicate that it is a member function of Inventory:
void Inventory::sell( Item* item_name )
{
// ...
}
Also, there are issues with the find() function. You declare it as a void member function, but use at as if it is a free function that returns an Item*.
Is Item a class?
You should change the void type to the type you want to be returned item.
Also whem you call your sell function, at the parameter if you want to pass the same pointer, you must use the & before the variable name like sell(item* &varname).