unqualified id before a friend function [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 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()

Related

C++: Is it possible to use a function of a class from outside? [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 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);
}

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.

Problem with overloaded member function in c++ [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 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.

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

I want to do operator overloading in c++ and it is giving error [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.h>
class complex
{
int a,b;
public:
complex(int x,int y)
{
a = x;
b = y;
cout<<"a and b is\n"<<a<<b<<endl;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
};
int main()
{
complex a(5,6);
complex b(7,8);
complex c;
c = a+b;
return 0;
}
I am facing that error:
ll.cpp: In member function âcomplex complex::operator+(complex)â:
ll.cpp:15: error: no matching function for call to âcomplex::complex()â
ll.cpp:7: note: candidates are: complex::complex(int, int)
Since you have no default constructor, change your operator implementation to not require one:
complex operator+(complex ob)
{
ob.a += a;
ob.b += b;
return ob;
}
Note that ob is copied into the function, so you already have a local, private object that's yours to use as you please.
Please add this constructor to your code
complex()
{
a = 0;
b = 0;
}