This question already has answers here:
Why is an overloaded function with two arguments of type double called when passing a long long?
(2 answers)
Closed 3 months ago.
I am getting compiler error of overloaded function is ambiguous.
although i understand what it means but how to resolve this problem?
#include <iostream>
using namespace std;
int area(int );
int area(int ,int );
float area(float );
int main()
{
cout << "square: " << area(5) << "\n";
cout << "rectangle: " << area(22,14) << "\n";
cout << "circle: " << area(6.5) << "\n";
return 0;
}
inline int area(int a)
{
return (a*a);
}
inline int area(int b,int c)
{
return (b*c);
}
float area(float d)
{
return (3.14*d*d);
}
This is because 6.5 is a double not a float, the compiler then tries to find area(double) which doesn't exist. It's ambiguous between area(float) and area(int).
One solution is that you should call it using 6.5 as a float.
area(6.5f)
Related
When we start learing about "Function Overloading" then we are given with the example of functions of same name but with either different number of parameters or different datatypes of parameters like:
void add(int a, int b) //function 1
{
cout << "sum = " << (a + b);
}
void add(double a, double b) //funciton 2
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
In this both the overloaded add functions are delared globally and they have same scope(global) also.
Now, my question is that if I define two functions with same name inside different blocks(one as global and other one inside a class) then will it still be called as "Function Overloading"?
Like:
void display() //function 1
{
cout<<"global diaplay() function";
}
class example
{
public:
void display(char a) //funciton 2
{
cout<<"display() function inside a class called by passing argument:"<<a;
}
};
int main()
{
display();
example M;
M.display('o');
return 0;
}
Explaination with an example will be very helpful.
i'm new to this site, after doing some research I could not find a problem similar to mine(some questions looked like mine but their code was different)
So basically what i'm trying to do is to representing the framebuffer matrix with all different colors values. I'm coding a class named "Point", and I have one constructor, using default arguments, here it is :
Point.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
class Point
{
protected:
int x;
int y;
public:
Point(int=0,int=0);
Point(const &Point);
void showC() const;
static void showC(Point);
virtual ~Point();
};
#endif // POINT_H
Point.cpp
#include "Point.h"
using namespace std;
Point::Point(int a,int b)
{
x=a;
y=b;
}
Point::~Point()
{}
void Point::showC() const
{ cout << x << " " << y << endl; }
void Point::showC(Point P)
{ cout << P.x << " " << P.y << endl; }
But the problem is when I try to compile the program
main.cpp
#include <iostream>
#include "Point.h"
using namespace std;
int main()
{
Point P1;
Point P2(2);
Point P3(4,-7);
cout << "Call of function member showC\n";
P1.showC();
P2.showC();
P3.showC();
cout << "Call of static function showC\n";
Point::showC(P1);
Point::showC(P2);
Point::showC(P3);
return 0;
}
There is an error when I create Point P2 :
"Call of overloaded 'Point(int)' is ambigous"
On all the others question i read, either it was not the same problem or they had a default constructor in addition to a constructor with default argument which cause ambiguity of which constructor to use if you create an object without argument.
On a book i'm reading to improve skills on c++, there is this sample that is working somehow, and that's why I don't really understand
Here is the sample :
main.cpp
class point
{
private :
int x;
int y;
Point (int abs=0, int ord=0) //inline constructor
{x=abs; y=ord;}
bool coincide(point);
};
bool point::coincide(point pt)
{ return ( (pt.x==x) && (pt.y==y) );
}
int main()
{
point a, b(1), c(1,0);
cout << "a and b : " << a.coincide(b) << " ou " b.coincide(a) << "\n"
cout << "b et c : " << b.coincide(c) << " ou " << c.coincide(b) << "\n"
}
However he grouped everything in the main.cpp files, and his constructor is inline.
Can anyone explain to me why is the sample working, and why my program is not ? I guess there is a mechanism that i don't understand...
Thanks in advance
RE-EDIT : I copied all the code
I think you are mixing both python and c++ way of creating class
python do use : class Point:
for declaring in class , c++ uses {} like class Point {};
Below works by changing the class declaration.
Just added a cout in your constructor
#include <iostream>
#include <vector>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(int=0,int=0);
};
Point::Point(int a, int b)
{
x = a;
y = b;
cout<<x<<y<<endl;
}
int main()
{
Point P1;
Point P2(2);
Point P3(4,-7);
return 0;
}
Output
00
20
4-7
Program ended with exit code: 0
After question edit
Removed your buggy line and it works perfectly
Point(const &Point);
#include <iostream>
#include <vector>
using namespace std;
class Point
{
protected:
int x;
int y;
public:
Point(int=0,int=0);
//Point(const &Point);
void showC() const;
static void showC(Point);
virtual ~Point();
};
Point::Point(int a,int b)
{
x=a;
y=b;
}
Point::~Point()
{}
void Point::showC() const
{ cout << x << " " << y << endl; }
void Point::showC(Point P)
{ cout << P.x << " " << P.y << endl; }
int main()
{
Point P1;
Point P2(2);
Point P3(4,-7);
cout << "Call of function member showC\n";
P1.showC();
P2.showC();
P3.showC();
cout << "Call of static function showC\n";
Point::showC(P1);
Point::showC(P2);
Point::showC(P3);
return 0;
}
Output
Call of function member showC
0 0
2 0
4 -7
Call of static function showC
0 0
2 0
4 -7
Program ended with exit code: 0
After edit I guess you want to use copy constructor just change it to
Point(const Point &p2) {x = p2.x; y = p2.y; }
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
I have been attempting to create a function getLocation() that utilizes a pointer to return the value of the struct Location declared in the Character class. I was curious as to the problem with my syntax (or my structure). Knowing that the asterisk * should refer to the value, why is it that my function using an ampersand string& Character::getInventory is able to return the value of that particular index (its return does not need to be converted)?
Trying Location& Character::getLocation() {return position; }
when run results in error C2679: binary '<<': no operator found
Nor
Location*
Which cannot be run as there is no conversion.
I read that the following is likely the most proper because it specifies the scope in which the structure resides, but still results in needing and returning a temporary.
Character::Location* const & Character::getLocation() {return &position; }
Any advice or input would be greatly appreciated, thanks in advance.
Below is my main.cpp, which of course will show the hexadecimal address for Location.
#include <iostream>
#include <string>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Character {
private:
string name;
string inventory[4];
public:
struct Location {
int x; int y;
};
Location position;
public:
void Character::setName(string x) { name = x; }
string Character::getName() { return name; }
void Character::setLocation(int x, int y) {
position.x = x; position.y = y;
}
Location* Character::getLocation() {return &position; }
void Character::setInventory(string(&x)[4]) { for (int i = 0; i < 4; ++i) { inventory[i] = x[i]; } }
string& Character::getInventory(int itemNumber) { return inventory[itemNumber]; }
};
void showUser(Character Character);
int main() {
try {
string items[4] = { "Sword", "Shield", "Potion", "Cloak" };
Character CharacterI;
CharacterI.setName("Some Character");
CharacterI.setInventory(items);
CharacterI.setLocation(1, 30);
cout << "\n" << "Retrieving Character Info..." << "\n" << endl;
showUser(CharacterI);
}
catch (std::exception & e) {
cerr << "\nError : " << e.what() << '\n';
}
system("pause");
return 0;
}
void showUser(Character character) {
cout << "Name : " << character.getName() << endl;
cout << "Location : " << character.getLocation() << endl;
for (int i = 0; i < 4; ++i) {
cout << "Inventory " << i + 1 << " : " << character.getInventory(i) << endl;
}
}
Ok, I think I understand the question better now. The reason why getInventory can successfully return a reference while getLocation does not is because getLocation returns a reference to a temporary variable, which is not good. See the link in #NathanOliver's comment for details. Additionally, to paraphrase a previous comment by #Peter Schneider, an * in an expression dereferences a pointer to return a value, while in a declaration it signifies that a variable will be of pointer type. The two usages are more or less opposites of each other. Example:
int* p = new int; //Declares a pointer to int
int x = *p; //Dereferences a pointer and returns an int
What you need to do is create a member variable to hold the Character's location, then set/get from that variable instead of creating temporaries. You did this already for name and inventory, just keep using that same pattern.
Additionally, whenever you use the Location struct outside of the Character class scope, you need to fully-qualify it with Character::Location.
Example:
#include <iostream>
using namespace std;
class Character {
public:
struct Location {
int x;
int y;
};
Location loc;
void SetLocation(int x, int y) {loc.x = x; loc.y = y;}
Location& GetLocation() {return loc;}
};
int main ()
{
Character c;
c.SetLocation(1,42);
Character::Location l = c.GetLocation();
cout << l.x << endl << l.y << endl;
return 0;
}
Output:
1
42
I am getting a wrong answer.I should get 6, but I am getting 548.
#include<iostream>
using namespace std;
//Fuction of add
int add(int x,int y)
{
return x+y;
}
//Function of multiply calling add fuction
int mul(int x,int y)
{
cout << add(2,3);
return x*y;
}
//Main Function
int main()
{
cout << mul(6,8);
return 0;
}
You are getting what you have programmed.
first the result of addition which is 2 + 3 = 5 and then the result of multiplication 6 * 8 = 48.
Since you are not giving providing escape characters the result is being printed right beside each other.
#include<iostream>
using namespace std;
//Fuction of add
int add(int x,int y)
{
return x+y;
}
//Function of multiply calling add fuction
int mul(int x,int y)
{
cout << "Addition of 2 and 3 is "<< add(2,3) << "\n";
return x*y;
}
//Main Function
int main()
{
cout << "Multiplication of 6 and 8 is "<<mul(6,8)<<"\n";
return 0;
}
By providing statements will help you better understand your program.
mul(6,8) outputs the result of add(2,3), i.e. 5, and then returns the result of 6*8, which is 48. That result is printed by main. Since there are no intervening spaces or newlines, what you see is 548.
I think you can solve this problem yourself if you add a newline to each cout statement: cout << add(2,3) << endl;
I have a C++ program:
struct arguments
{
int a, b, c;
arguments(): a(3), b(6), c(9) {}
};
class test_class{
public:
void *member_func(void *args){
arguments vars = (arguments *) (*args); //error: void is not a
//pointer-to-object type
std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
}
};
On compile it throws an error:
error: ‘void*’ is not a pointer-to-object type
Can someone explain what I am doing wrong to produce this error?
You are dereferencing the void * before casting it to a concrete type. You need to do it the other way around:
arguments vars = *(arguments *) (args);
This order is important, because the compiler doesn't know how to apply * to args (which is a void * and can't be dereferenced). Your (arguments *) tells it what to do, but it's too late, because the dereference has already occurred.
Bare bones example to reproduce the above error:
#include <iostream>
using namespace std;
int main() {
int myint = 9; //good
void *pointer_to_void; //good
pointer_to_void = &myint; //good
cout << *pointer_to_void; //error: 'void*' is not a pointer-to-object type
}
The above code is wrong because it is trying to dereference a pointer to a void. That's not allowed.
Now run the next code below, If you understand why the following code runs and the above code does not, you will be better equipped to understand what is going on under the hood.
#include <iostream>
using namespace std;
int main() {
int myint = 9;
void *pointer_to_void;
int *pointer_to_int;
pointer_to_void = &myint;
pointer_to_int = (int *) pointer_to_void;
cout << *pointer_to_int; //prints '9'
return 0;
}
You have the * in the wrong place. So you're trying dereference the void*.
Try this instead:
arguments vars = *(arguments *) (args);
std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
Alternatively, you can do this: (which also avoids the copy-constructor - as mentioned in the comments)
arguments *vars = (arguments *) (args);
std::cout << "\n" << vars->a << "\t" << vars->b << "\t" << vars->c << "\n";
The problem as bdonlan said is "dereferencing void* before casting".
I think this example would help:
#include <iostream>
using namespace std;
int main()
{
void *sad;
int s = 23;
float d = 5.8;
sad = &s;
cout << *(int*) sad;//outputs 23//wrong: cout << *sad ;//wrong: cout << (int*) *sad;
sad = &d;
cout << *(float *) sad;//outputs 5.8//wrong: cout << *sad ;//wrong: cout << (float*) *sad;
return 0;
}
*args means "the object(value) args points to". Therefore, it can not be casted as pointer to object(argument). That's why it is giving error
The problem above there is that you are trying to deference a void pointer which is not allowed in C or C++.
However, this still works:
#include <iostream>
using namespace std;
int main()
{
int b=10;
void *a=&b;
int *ptr=(int*)a;
cout<<*ptr;;
}
We can deference int* pointers after casting void pointers to int* pointers.