C++ reference to distance is ambiguous [duplicate] - c++

This question already has answers here:
issue with "using namespace std;"
(3 answers)
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
#include<iostream>
using namespace std;
class distance
{private:
int feet1 ,inches1,feet2,inches2,sum1,sum2;
public:
void getdata()
{
cout<<"give the value of distance in inches and feet";
cin>>feet1>>inches1>>feet2>>inches2;
}
void add()
{sum1= feet1+feet2;
sum2=inches1+inches2;
}
void display()
{
cout<<sum1<<sum2;
}
};
int main()
{
distance x;
x.add;
x.display;
return 0;
}
I tried to write a program to add distance in feet and inches but it shows that reference to distance is ambiguous. Can someone help me out?

Related

Reference class member variables give an error "uninitialized reference" in constructor [duplicate]

This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 7 months ago.
I have the following code:
#include<iostream>
using namespace std;
class Vec{
private:
int& var;
public:
Vec(int& tmp){
var = tmp;
}
};
int main(){
int x = 10;
Vec v1(x);
}
but it gives a compilation error:error: uninitialized reference member in ‘int&’ [-fpermissive]
How to resolve this?
You should use an initializer list.
#include<iostream>
using namespace std;
class Vec{
private:
int& var;
public:
Vec(int& tmp) : var(tmp) {}
};
int main(){
int x = 10;
Vec v1(x);
}

Error reference to 'distance' is ambiguous [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I cannot figure out why I am getting the error "reference to 'distance' is ambiguous".
I have passed class object as an argument in friend function.
#include <iostream>
using namespace std;
class distance {
int meters = 0;
public:
distance() {}
void displaydata() {
cout << "Meters Value:" << meters;
}
//Prototype
friend void addvalue(distance &d);
};
void addvalue(distance &d) {
d.meters += 5;
}
int main() {
distance d1; // meters = 0
d1.displaydata(); // 0
// The friend function call
addvalue(d1); // pass by reference
d1.displaydata();
}
If you remove the using namespace std; and change cout to std::cout, then your code compiles w/o error.
Discovering the source of the ambiguity is left as an exercise to the reader

C++ output shown without returning the values [duplicate]

This question already has answers here:
Function with missing return value, behavior at runtime
(4 answers)
Closed 2 years ago.
compiler used-: code blocks
without returning the "c" how is variable "d" getting its value?
#include<iostream>
using namespace std;
int add(int x,int y) {
int c;
c=x+y;
}
int main() {
int a,b;
cin>>a>>b;
int d=add(a,b);
cout<<d;
}
Write this code:
#include<iostream>
using namespace std;
int add(int x,int y)
{
return x+y;//CHANGE THIS
}
int main()
{
int a,b;
cin>>a>>b;
int d=add(a,b);
cout<<d;
}
Doesn't compile in visual C++, but it does indeed give the results when using onlinedgb.
JaMiT's comment above links to the correct answer with explanation.

Why is passing a class instance into its own class constructor right? [duplicate]

This question already has answers here:
What is The Rule of Three?
(8 answers)
Is passing a C++ object into its own constructor legal?
(3 answers)
Closed 4 years ago.
I am curious about the following code which can execute correctly.
#include <iostream>
using namespace std;
class A
{
public:
A(int a=1, int b=2){
this->a = a;
this->b = b;
}
void print_a(){
cout<<a<<endl;
}
private:
int a;
int b;
};
int main() {
A aa = A(A()); //
aa.print_a();
return 0;
}
The output is 1 which is correct.
I am curious about the mechanism.
The difference between this question is that that question's constructor explicitly accepts an instance.

C++, expression must be a modifiable lvalue [duplicate]

This question already has answers here:
How to copy a char array in C?
(14 answers)
Assign array to array
(6 answers)
Closed 6 years ago.
I have this following code:
#include "stdafx.h"
#include<iostream>
using namespace std;
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
bool adj[x][x];
char n;
};
int main(){
Graf graf1;
graf1.adj = graf_adj;
}
in main function when i try to assing graf_adj to graf1.adj
graf1.adj = graf_adj;
complier gives me this error:
Error Expression must be a modifiable lvalue
Can anybody give a solution to this ?
Thank you
Now that you have added the type for the const:
Here is the solution using memcpy
#include<iostream>
#include <cstring>
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
bool adj[x][x];
char n;
};
int main(){
Graf graf1;
std::memcpy(&graf1.adj, &graf_adj, sizeof(graf1.adj));
}