//function overloading
#include <iostream>
using namespace std;
//declaration of function protype
int area(int);
int area(int, int);
float area(float);
int main()
{
cout << "calling the area() function for computiong the area of a square (side=5) - " << area(5) << "\n";
cout << "calling the area() function for computiong the area of a rectangle (length = 5, bredth = 10) - " << area(5, 10) << "\n";
cout << "calling the area() function for computiong the area of a cirlce (radius 5.5) - " << area(5.5) << "\n";
return 0;
}
int area(int side)
{
return (side * side);
}
int area(int length, int breadth)
{
return (length * breadth);
}
float area(float radius)
{
return (3.14 * radius * radius);
}
error
program4_5.cpp: In function 'int main()':
program4_5.cpp:14:106: error: call of overloaded 'area(double)' is ambiguous
14 | cout << "calling the area() function for computiong the area of a cirlce (radius 5.5) - " << area(5.5) << "\n";
| ^
program4_5.cpp:6:5: note: candidate: 'int area(int)'
6 | int area(int);
| ^~~~
program4_5.cpp:8:7: note: candidate: 'float area(float)'
8 | float area(float);
| ^~~~
PS C:\Users\hasht\Desktop\coding\OOP with c++>
this code while compiling shows function overloading error but from what I know it's correct as I have defined the float area( float radius) function. Can anyone explain why this error occurs. I am new to coding and i don't know how to fix this.
5.5 is a literal of type double, so the compiler doesn't know you want to call the overload taking an int or the one taking a float.
You can use a float-literal instead:
// -V-
cout << "..." << area(5.5f) << "\n";
Type area(5.5f) to force the value to be a float.
The compiler does not know if it should cast your double value to an int or a float. Therefore, it is ambiguous.
Related
Im practicing memberwise assignment in C++, where you can set the values of one object to another object of the same class. The idea of the program is to initialize a rectangle object with some values and create another rectangle object but assign the value of the first into the second.
Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol
This is my Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double, double);
double getLength() const;
double getWidth() const;
};
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
#endif
This is my Rectangle.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
This is the error when I compile.
skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp
rectangle.cpp:7:12: error: no matching constructor for initialization of
'Rectangle'
Rectangle box1(10.0, 10.0);
^ ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
not viable: requires 1 argument, but 2 were provided
class Rectangle {
^
./rectangle.h:4:7: note: candidate constructor
(the implicit default constructor) not viable: requires 0 arguments, but 2
were provided
1 error generated.
EDIT: This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.
EDITED rectangle.cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Rectangle();
Rectangle(double = 0.0, double = 0.0);
double getLength() const;
double getWidth() const;
};
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
The only changes I made were giving default arguments to my user-defined constructor. However, it wasn't able to work when the changes were in rectangle.h. However, when I moved the class and member function definitions to rectangle.cpp it was able to work. So, I got the program to work but I didn't address the real issue, which is when the class and member function definitions are in rectangle.h, it won't compile.
If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)
In the line
Rectangle box2; // no default constructor, error
you are trying to invoke the default constructor of Rectangle. The compiler does not generate such a default constructor anymore, because your Rectangle has a user defined constructor that takes 2 parameters. Therefore, you need to specify the parameters, like
Rectangle box2(0,10);
The error I get when compiling your code is:
Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()'
Rectangle box2;
A solution is to create a default constructor for Rectangle, since it is not automatically generated anymore due to your user defined one:
Rectangle(); // in Rectangle.h
Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)
Another solution (and the preferable one, since it doesn't leave the data un-initialized) is to assign default arguments to your existing constructor.
Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h
In this way, you make your class Default-Constructible.
A compiler generated default constructor is only generated if you have no defined constructors. You define a constructor, so if you want a default constructor you have to provide it yourself. Probably the easiest (arguably) is to provide it by using default arguments in your two argument constructor:
Rectangle(double l=0, double w=0)
Also you should use the inline keyword as shown below or you may find you get linker errors:
inline Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }
This question already has an answer here:
Using float gives "call to overloaded function is ambiguous" error [duplicate]
(1 answer)
Closed 6 years ago.
I'm learning C++ through Sololearn. I have a doubt about function overloading
this is the code
#include<iostream>
using namespace std;
void printSomething(int x) {
cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x) {
cout << "I'm printing a float " << x << endl;
}
int main() {
int a =3;
float b = 2.65;
printSomething(a);
printSomething(b);
return 0;
}
it gives output as
I'm printing an integer 3
I'm printing a float 2.65
but if I directly give argument when calling function
like this
#include<iostream>
using namespace std;
void printSomething(int x) {
cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x) {
cout << "I'm printing a float " << x << endl;
}
int main() {
printSomething(3);
printSomething(2.65);
return 0;
}
i get following error
..\Playground: In function 'int main()':
..\Playground:19:24: error: call of overloaded 'printSomething(double)' is ambiguous
printSomething(2.65);
^
..\Playground:19:24: note: candidates are:
..\Playground:5:6: note: void printSomething(int)
void printSomething(int x) {
^
..\Playground:9:6: note: void printSomething(float)
void printSomething(float x) {
^
but if I change
void printSomething(float x) {
cout << "I'm printing a float " << x << endl;
}
to
void printSomething(double x) {
cout << "I'm printing a float " << x << endl;
}
I will get output as
I'm printing a float 2.65
why is it?
but if it's only the integer it works fine
#include<iostream>
using namespace std;
void printSomething(int x) {
cout << "I'm printing an integer " << x << endl;
}
void printSomething(float x) {
cout << "I'm printing a float " << x << endl;
}
int main() {
printSomething(3);
return 0;
}
Result
I'm printing an integer 3
Why isn't this working with float
Thankyou
2.65 is not a float literal, it's a double literal.
So the compiler doesn't know whether you want to convert the double to a float or an int and so issues the error.
In your first case, when writing float b = 2.65; the compiler assumes you know what you're doing, and calling the overload with b is unambiguous.
If you had written printSomething(2.65f); then that would also have been unambiguous: 2.65f is a float literal.
2.65 is considered a double. But you didn't provide an overload void printSomething(double x). Therefore the compiler must cast the value and it doesn't know if it should cast to float or int (both with precision loss`).
If you write 2.65f it is considered a float and it should work.
The reason for that is the conversion rules and overload resolution strategy. If C++ is unable to find an exact match on parameters, it looks for a conversion. The best conversion is an implicit one, that is widening (casting a data type to one that can hold all the values of the original type and potentially more), then a narrowing conversion (casting to a smaller data type, which may cause errors or loss of precision for some values), then a user-defined conversion.
As literal 2.65 is of type double, the compiler looks for conversions. There are two: double -> float and double -> int. They are both narrowing, which means they are equally good. The compiler is unable to pick the best one, thus reports an error.
To remedy this, you can either:
define overload for double as you did
use a float literal (2.65f) instead of a double
Im practicing memberwise assignment in C++, where you can set the values of one object to another object of the same class. The idea of the program is to initialize a rectangle object with some values and create another rectangle object but assign the value of the first into the second.
Its giving me an error, which is posted below, and I can't figure out what it is and its driving me nuts lol
This is my Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double, double);
double getLength() const;
double getWidth() const;
};
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
#endif
This is my Rectangle.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
This is the error when I compile.
skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp
rectangle.cpp:7:12: error: no matching constructor for initialization of
'Rectangle'
Rectangle box1(10.0, 10.0);
^ ~~~~~~~~~~
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor)
not viable: requires 1 argument, but 2 were provided
class Rectangle {
^
./rectangle.h:4:7: note: candidate constructor
(the implicit default constructor) not viable: requires 0 arguments, but 2
were provided
1 error generated.
EDIT: This is how I was able to make it work. I moved everything into rectangle.cpp and gave the constructor default arguments.
EDITED rectangle.cpp
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Rectangle();
Rectangle(double = 0.0, double = 0.0);
double getLength() const;
double getWidth() const;
};
int main()
{
Rectangle box1(10.0, 10.0);
Rectangle box2;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
box2 = box1;
cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl;
cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl;
return 0;
}
Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
The only changes I made were giving default arguments to my user-defined constructor. However, it wasn't able to work when the changes were in rectangle.h. However, when I moved the class and member function definitions to rectangle.cpp it was able to work. So, I got the program to work but I didn't address the real issue, which is when the class and member function definitions are in rectangle.h, it won't compile.
If anyone has faced this problem and has found a solution to this, please let me know how you did it. Thanks :)
In the line
Rectangle box2; // no default constructor, error
you are trying to invoke the default constructor of Rectangle. The compiler does not generate such a default constructor anymore, because your Rectangle has a user defined constructor that takes 2 parameters. Therefore, you need to specify the parameters, like
Rectangle box2(0,10);
The error I get when compiling your code is:
Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()'
Rectangle box2;
A solution is to create a default constructor for Rectangle, since it is not automatically generated anymore due to your user defined one:
Rectangle(); // in Rectangle.h
Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11)
Another solution (and the preferable one, since it doesn't leave the data un-initialized) is to assign default arguments to your existing constructor.
Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h
In this way, you make your class Default-Constructible.
A compiler generated default constructor is only generated if you have no defined constructors. You define a constructor, so if you want a default constructor you have to provide it yourself. Probably the easiest (arguably) is to provide it by using default arguments in your two argument constructor:
Rectangle(double l=0, double w=0)
Also you should use the inline keyword as shown below or you may find you get linker errors:
inline Rectangle::Rectangle(double l, double w) {
length = l;
width = w;
}
inline double Rectangle::getWidth() const { return width; }
inline double Rectangle::getLength() const { return length; }
I have a little question about how default values are given to function parameters in C++. The problem I faced is probably due to my lack of understanding as to where the parameters are supposed to be declared/defined in the function prototype or the function header, or both? Codes are below with the errors noted:
#include <iostream>
using namespace std;
float volume(float l, float w, float h);
int main() {
float length;
float width;
float height;
cout << volume() << endl; // Here, with 'volume()' underlined, it says:
//no matching function for call to 'volume()'
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l = 1, float w = 1, float h = 1){
float vol = l * w * h;
return vol;
}
In another attempt, here's what happened:
#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
float length;
float width;
float height;
cout << volume() << endl;
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l = 1, float w = 1, float h = 1){ //Here, Xcode says that
// that the error is: Redefinition of default argument. < which I believe I understand.
float vol = l * w * h;
return vol;
}
In my last attempt, which is the one that worked, I did this:
#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
float length;
float width;
float height;
cout << volume() << endl;
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
cout << "Volume = " << volume(length, width, height) << endl;
}
float volume(float l, float w, float h){
float vol = l * w * h;
return vol;
}
Could someone please explain to me the logic behind why the latter worked while the first two did not? Is there another way that the code would still work in the same way with the parameters specified elsewhere or the default values set in some place else? Are there any conventions or more favored practices in this area?
Adam
C++ and C are parsed top-down. When the compiler interprets a statement, it doesn't know about things it hasn't read yet.
In your first example, you declare a function called "volume", prototyped as taking 3 floats and returning a float. You then try to call a function called "volume" that takes no parameters, which doesn't exist yet (it would be a different function, since C++ supports polymorphism). You later define a function that can take 0, 1, 2, or 3 floats, but it is both too late and has an incompatible prototype to the first.
Your second example intuitively makes sense to be wrong, kind of like defining variables twice, but I don't have any specific information about why it is invalid code when the default values are identical.
Default parameters must be specified in the function prototype, which must occur prior to first usage in order for the compiler to know about it. Typically, you would put the prototypes with their default values in a header file that gets included above the code.
One thing to watch out for when dealing with default parameters from a shared header file, especially if you use it with dynamic libraries: The default values for the parameters are stored with the caller, and not the function being called. That is, if you update the function with new default values and don't rebuild the code calling that function, the old defaults will still be used by the calling code.
Adding to the above answer from Bjarne Stroustrup
A default argument is type checked at the time of the function declaration and evaluated at the time of the call. Default arguments may be provided for trailing arguments only. For example:
int f (int, int =0 , char * =0 ); // ok
int g (int =0 , int =0 , char *); // error
int h (int =0 , int , char * =0 ); // error
A default argument can be repeated in a subsequent declaration in the same scope but not changed.
For example:
void f (int x = 7 );
void f (int = 7 ); // ok
void f (int = 8 ); // error: different default arguments
void g () { void f (int x = 9 ); // ok: this declaration hides the outer one }
Declaring a name in a nested scope so that the name hides a declaration of the same name in an outer scope is error prone.
Default values could be defined in function declaration, as you done in your third attempt. This means usually they're appeared in header files, although this is not a rule.
Note that function declarations are scoped. This means that you could have more than one declaration for a function as long as they have different scopes:
void f(int);
int main() {
f(3); // argument should specified.
void f(int = 1);
f(); // calls f(1)
}
void f(int n = 2) {
}
void g() {
f(); // calls f(2)
}
In your second attempt you put default values on both declaration and definition of the function. This cause the compiler to confused because they are in the same scope.
/* This program */
using namespace std;
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
void readit();
void calcit(int, int);
void writeit(float, float, float);
int distvels[4] = {15, 25, 35, 45};
int main()
{
readit();
system("pause");
return 0;
}
void readit()
{
int targetdist, angl;
cout << "Enter the distance (meters) to the target:" << endl;
cin >> targetdist;
cout << "Enter the angle to fire the cannon:" << endl;
cin >> angl;
cout << "\n---------------\n\n";
calcit (targetdist, angl);
}
void calcit(int targetdist, int angl)
{
double distvals[4];
double tyme[4];
double maxhite[4];
for (int i=0; i<4; i++) {
distvals[i] = (2 * sin(angl) * cos(angl) * (distvels[i] * distvels[i]))/9.8;
tyme[i] = (2 * cos(angl) * distvels[i])/9.8;
maxhite[i] = ((cos(angl) * cos(angl)) * (distvels[i] * distvels[i]))/9.8;
}
writeit(distvals, tyme, maxhite);
}
void writeit(float distvals[4], float tyme[4], float maxhite[4])
{
cout << "Velocity " << "time " << "height " << "distance " <<endl;
for (int i=0; i<4; i++) {
cout << distvals[i] << " " << tyme[i] << " " << maxhite[i] << " " << endl;
}
whenever I run the program I keep getting this error code cannot convert double* to float for argument 1 to void writeit(float, float, float). I've tried everything I can think of to get rid of it with no luck. Can anyone help?
You declared the function as:
void writeit(float, float, float);
but the definition has it as:
void writeit(float distvals[4], float tyme[4], float maxhite[4])
{
// ...
}
Fix the declaration to match:
void writeit(float[4], float[4], float[4]);
It's also worth pointing out at this juncture that this doesn't do what you think it does. In fact, it's the same as this:
void writeit(float[], float[], float[]);
which is the same as this:
void writeit(float*, float*, float*);
That's because you can't pass an array by value, so it degrades into a pointer-to-the-start-of-the-array instead.
However, you can pass it by reference and keep the dimensions:
void writeit(float (&)[4], float (&)[4], float (&)[4]); // declaration
void writeit(float (&distvals)[4], float (&tyme)[4], float (&maxhite)[4]) // definition
{
// ...
}
I'd even recommend passing it as reference-to-const, as you won't be changing it:
void writeit(float (&)[4], float (&)[4], float (&)[4]);
void writeit(const float (&distvals)[4], const float (&tyme)[4], const float (&maxhite)[4])
{
// ...
}
It would also be easier if you used a std::vector<float>, but that's another discussion.
Plenty to think about there; hope it helps.
Edit Just noticed another problem, in that you're trying to pass an array of double to a function that will be expecting an array of float! Pick one and stick with it.
The error is exactly as you note -- writeit is expecting a pointer to an array of floats and you're trying to pass it an array of doubles, which are a different size. The easiest fix would be to declare the args of writeit to be arrays of doubles, so they match. Barring that, you need to copy to arrays of floats before passing (converting each element as you copy)
The function prototype is different from the function definition. So, change it to -
void writeit(double*, double*, double*);
And the function definition to -
void writeit(double distvals[], double tyme[], double maxhite[])
{
// ......
}
Notice that the array size (i.e., optional, in fact compiler won't consider it) because array decays to a pointer. It is the reason why usually array size is also sent as an argument to the functions and is a good practice.