Error reference to 'distance' is ambiguous [duplicate] - c++

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

Related

How to output Classes and Objects [duplicate]

This question already has answers here:
How to print class object using operator<<
(3 answers)
Closed 3 months ago.
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(){
cout<<"Name : "<<studentName<<endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result : "<<(x*y)<<endl;
}
};
int main()
{
cout<<endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}
I am new to C++ and am learning about classes and objects. From what I gathered classes are ways to group functions and objects is the ability to access them? I am having trouble getting this code to work, I receive an error on line 15 for 'error: no match for 'operator<<'. I am trying to fix my class in order for the main function to work. The output should simply be
'Name : Mark Santo'
'Result : 50'
Thank you for your help everyone!
It seems like there is no error on line 15. Instead, you did not define the variable studentName in your void studentName() function in line 8. Also, you used 2 arguments for studentName() in line 23, where the original function is not taking any. This is the corrected code:
#include <iostream>
using namespace std;
class PrintName{
public:
void studentName(string x, string y){
cout<<"Name: " << x << " " << y << endl;
}
};
class MathClass{
public:
void multiplicationFunc(int x, int y){
cout<<"Result: " << x*y << endl;
}
};
int main()
{
cout << endl;
PrintName PN;
PN.studentName("Mark", "Santo");
MathClass MC;
MC.multiplicationFunc(10,5);
}

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);
}

Why am I getting undefined reference errors? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm getting undefined reference errors. Can you please figure out why?
Errors:
undefined reference to Breuken::Breuken(int, int)
undefined reference to Breuken::som()
(I'm kinda new in c++ programming. I am currently working on a project now, using header files another source file and main source file.)
Here is my code:
Header File (Breuk.H):
using namespace std;
class Breuken{
Breuken(int, int);
void som();
void setTeller1(int);
void setNoemer1(int);
int getTeller1();
int getNoemer1();
private:
int teller1;
int noemer1;
};
Source File(Breuk.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
Breuken::Breuken(int tel1, int noem1)
{
setTeller1(tel1);
setNoemer1(noem1);
}
int Breuken::getTeller1()
{
return teller1;
}
int Breuken::getNoemer1()
{
return noemer1;
}
void Breuken::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuken::setNoemer1(int noem1)
{
noemer1 = noem1;
}
int Breuken::som()
{
cout<<"Breuken zijn: "<<getTeller1()<<" / "<<getNoemer1()<<endl;
}
main file(main.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
int main()
{
Breuken br(1, 2);
br.som();
return 1;
}
If I got this right, you want to use a class, but the only thing you did is declare some functions without actually defining them.
What you need to do is put everything in your header inside a class like that:
class Breuk {
private:
int teller1;
int noemer1;
public:
Breuk(int tel1, int noem1);
void som();
void setTeller1(int tel1);
void setNoemer1(int noem1);
int getTeller1();
int getNoemer1();
};
Also, don't forget to give the variables inside the function head a name!
The .cpp file you need to get the reference to the class right (the part before the ::). It has to be the same name as your class. Also, it's not so great to use setter inside the constructor. It's better to reference the member variables directly (the variables inside the class). In order to do that you need to use "this->variable". "this" will refer to the current class you're in. That means if you write this->teller1 it will refer to the variable teller1 inside the class.
#include <iostream>
#include "Breuk.hpp"
using namespace std;
Breuk::Breuk(int tel1, int noem1)
{
// setTeller1(tel1); theoreticly ok but its better to access
// setNoemer1(noem1); member variables (variables inside a class) directly
this->teller1 = tel1;
this->noemer1 = noem1;
}
int Breuk::getTeller1()
{
return teller1;
}
int Breuk::getNoemer1()
{
return noemer1;
}
void Breuk::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuk::setNoemer1(int noem1)
{
noemer1 = noem1;
}
void Breuk::som()
{
cout << "Breuken zijn: " << getTeller1() << " / " << getNoemer1() << endl;
}
in the main.cpp you only made a small mistake with the return 0 and the name of the class.
#include "Breuk.hpp"
using namespace std;
int main()
{
Breuk br(1, 2); // make sure u use the same name your class has
br.som();
return 0; // return 0 at the end: it means that the program executed correctly
// return 1 would mean that an error occured
}
Also, I suggest using .hpp files instead of .h files. There is no real difference but it's more common to use .hpp for c++.

C++ reference to distance is ambiguous [duplicate]

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?

Not able to call a function using function pointer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call a function using pointer-to-member-function
Analyzer.h
class Analyzer
{
public :
void viku();
void Bibek();
void vivek();
void (Analyzer::*point)();
Analyzer(){
}
~Analyzer(){
}
};
Analyzer.cpp
using namespace std
#include"Analyzer.h"
void Analyzer::viku(){
cout<<"Hello viku";
}
void Analyzer::vivek(){
point =&Analyzer::viku;
Bibek();
}
void Analyzer::Bibek(){
point();//Errror
cout<<"Bibek";
}
During compilation it shows the following error:
error C2064: term does not evaluate to a function taking 0 arguments.
Can anyone please tell me how to avoid this?
Pointers to member functions are different than normal function pointer. You need an instance to call them:
#include <iostream>
class A
{
public:
int foo()
{
std::cout << "A::foo here, you can have 42" << std::endl;
return 42;
}
};
int main ()
{
int (A::* point)() = &A::foo;
A a;
(a.*point)();
}
In your case, you'd need to do something like the following:
(this->*point)()