This question already has an answer here:
c++ member function hides global function
(1 answer)
Closed 5 months ago.
#include <iostream>
#include <unistd.h>
using namespace std;
class A{
public:
bool close(){ return true; }
};
class B: public A{
public:
void fun(){
(void) close(1);
}
};
int main() {
B b;
b.fun();
return 0;
}
In class B I want to call the function close(1) which is in library unistd.h. FYI: close(int fileDes) used to close the file descriptors in process.
So how to overcome the issue. I got the below error:
source.cpp: In member function 'void B::fun()':
source.cpp:11:27: error: no matching function for call to 'B::close(int)'
11 | (void) close(1);
| ^
source.cpp:6:14: note: candidate: 'bool A::close()'
6 | bool close(){ return true; }
| ^~~~~
source.cpp:6:14: note: candidate expects 0 arguments, 1 provided
So how to overcome the issue, so that It will call the function in unistd.h file.
In the scope of the member function fun the name close as an unqualified name is searched in the scope of the class
void fun(){
(void) close(1);
}
And indeed there is another member function close in the base class with such a name.
So the compiler selects this function.
If you want to use a function from a namespace then use a qualified name as for example
void fun(){
(void) ::close(1);
}
Related
This question already has answers here:
What is the difference between private and protected members of C++ classes?
(19 answers)
What are public, private and protected in object oriented programming?
(7 answers)
Closed 1 year ago.
Dear all I have this code running on visual studio . I am getting error as below
I have taken example from books. I have installed visual studio recently. I have tested basic code working well.
I am getting Error for Include files so try to correct Include path error. Kindly suggest me what can be done
Link video
IntialError
Correction Made
#include <iostream>
#include <cstring>
using namespace std;
class BaseClass1
{
private:
int Num1;
public:
void GetNumber(int);
};
class BaseClass2
{
private:
int Num2;
public:
void GetNumber1(int);
};
class Derived :public BaseClass1,public BaseClass2
{
public:
void Display_Result(void);
};
//************************************************************
void BaseClass2::GetNumber1(int b)
{
Num2=b;
}
//************************************************************
void BaseClass1::GetNumber(int a)
{
Num1=a;
}
//************************************************************
void Derived::Display_Result()
{
cout<<"Num1"<<Num1<<endl;
cout<<"Num2"<<Num2<<endl;
cout<<"Result"<<(Num2*Num1)<<endl;
}
int main(int argc, char** argv) {
Derived D;
D.GetNumber(50);
D.GetNumber1(100);
D.Display_Result();
return 0;
}
Error I am getting
C:\Desktop\C++ coding\Visual_basic> cd "c:Desktop\C++ coding\Visual_basic\" ; if ($?)
{ g++ Inheitance.cpp -o Inheitance } ; if ($?) { .\Inheitance }
Inheitance.cpp: In member function 'void Derived::Display_Result()':
Inheitance.cpp:55:16: error: 'int BaseClass1::Num1' is private within this context
cout<<"Num1"<<Num1<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~
Inheitance.cpp:56:16: error: 'int BaseClass2::Num2' is private within this context
cout<<"Num2"<<Num2<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:19: error: 'int BaseClass2::Num2' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:24: error: 'int BaseClass1::Num1' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~
I do understand why the following would be a problem if no namespaces were used. The call would be ambiguous indeed. I thought "using stD::swap;" would define which method to use.
Why does it work for "int" but not a "class"?
#include <memory>
namespace TEST {
class Dummy{};
void swap(Dummy a){};
void sw(int x){};
}
namespace stD {
void swap(TEST::Dummy a){};
void sw(int x){};
class aClass{
public:
void F()
{
using stD::swap;
TEST::Dummy x;
swap(x);
}
void I()
{
using stD::sw;
int b = 0;
sw(b);
}
};
}
This is the error message:
../src/Test.h: In member function ‘void stD::aClass::F()’:
../src/Test.h:26:9: error: call of overloaded ‘swap(TEST::Dummy&)’ is ambiguous
swap(x);
^
../src/Test.h:26:9: note: candidates are:
../src/Test.h:17:6: note: void stD::swap(TEST::Dummy)
void swap(TEST::Dummy a){};
^
../src/Test.h:10:6: note: void TEST::swap(TEST::Dummy)
void swap(Dummy a){};
^
I thank you very much in advance for an answer.
This line is using argument dependent lookup
TEST::Dummy x;
swap(x);
So it will find both void stD::swap(TEST::Dummy) as well as void TEST::swap(TEST::Dummy) because x carries the TEST:: namespace.
In the latter case int b = 0; the variable b is not in a namespace, so the only valid function to call would be stD::sw due to your using statement.
I am trying to implement an execution pattern which takes any function and executes it with its own conditions/preparations. Regardless of this being a useful thing to do, it just doesn't work. It seems i can't access the template overload of the "Execute"-function (called in "main").
Specifically: Why can't i call the overloaded template function of Execute?
This is the full program:
#include "stdafx.h"
#include <functional>
class TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction) = 0;
template<class C>
C Execute(std::function<C(void)> executeFunction) // template-overload of the abstract function which will implicitly call it
{
C ret;
Execute( // calls the abstract function with a lambda function as parameter
[ret, executeFunction](void) -> C // lambda declaraction
{ //
ret = executeFunction; // lambda body
}); //
return ret;
}
};
class ExampleExecutor : public TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction)
{
printf("executed before.");
executeFunction();
printf("executed after.");
}
};
void DoStuff() {}
int ReturnStuff() { return -5; }
int main()
{
ExampleExecutor executor;
executor.Execute(DoStuff);
int i = executor.Execute<int>(ReturnStuff); // Why does this not work? ERROR: "type name is not allowed"
getchar();
return 0;
}
Note: Visual Studio marks
Execute<int>(ReturnStuff) // "int" is marked as Error: type name is not allowed
The compilation puts out the error
"type 'int' unexpected"
Thanks to everyone willing to help!
ExampleExecutor::Execute is not overriding TransparentFunctionWrapper::Execute, and it is hiding it in the executor.Execute<int> call.
You must explicitly call TransparentFunctionWrapper::Execute, as it is hidden by ExampleExecutor::Execute. Here's a possible way of doing that:
int i = executor.TransparentFunctionWrapper::Execute<int>(ReturnStuff);
live example on coliru
This question already has answers here:
Start thread with member function
(5 answers)
Closed 9 years ago.
I'm just starting to use C++ 11 threads and I've been struggling on a (probably silly) error.
This is my example program:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
class A {
public:
A() {
cout << "A constructor\n";
}
void foo() {
cout << "I'm foo() and I greet you.\n";
}
static void foo2() {
cout << "I'm foo2() and I am static!\n";
}
void operator()() {
cout << "I'm the operator(). Hi there!\n";
}
};
void hello1() {
cout << "Hello from outside class A\n";
}
int main() {
A obj;
thread t1(hello1); // it works
thread t2(A::foo2); // it works
thread t3(obj.foo); // error
thread t4(obj); // it works
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}
Is it possible to start a thread from a pure member function? If it is not, how can I wrap my foo function from object obj to be able to create such thread?
Thanks in advance!
This is the compiling error:
thread_test.cpp: In function ‘int main()’:
thread_test.cpp:32:22: error: no matching function for call to ‘std::thread::thread()’
thread_test.cpp:32:22: note: candidates are:
/usr/include/c++/4.6/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(), _Args = {}]
/usr/include/c++/4.6/thread:133:7: note: no known conversion for argument 1 from ‘’ to ‘void (A::*&&)()’
/usr/include/c++/4.6/thread:128:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.6/thread:128:5: note: no known conversion for argument 1 from ‘’ to ‘std::thread&&’
/usr/include/c++/4.6/thread:124:5: note: std::thread::thread()
/usr/include/c++/4.6/thread:124:5: note: candidate expects 0 arguments, 1 provided
You need a callable object taking no parameters, so
thread t3(&A::foo, &obj);
should do the trick. This has the effect of creating a callable entity which calls A::foo on obj.
The reason is that a non-static member function of A takes an implicit first parameter of type (possibly cv qualified) A*. When you call obj.foo() you are effectively calling A::foo(&obj). Once you know that, the above incantation makes perfect sense.
This question already has answers here:
Start thread with member function
(5 answers)
Closed 9 years ago.
I'm just starting to use C++ 11 threads and I've been struggling on a (probably silly) error.
This is my example program:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
class A {
public:
A() {
cout << "A constructor\n";
}
void foo() {
cout << "I'm foo() and I greet you.\n";
}
static void foo2() {
cout << "I'm foo2() and I am static!\n";
}
void operator()() {
cout << "I'm the operator(). Hi there!\n";
}
};
void hello1() {
cout << "Hello from outside class A\n";
}
int main() {
A obj;
thread t1(hello1); // it works
thread t2(A::foo2); // it works
thread t3(obj.foo); // error
thread t4(obj); // it works
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}
Is it possible to start a thread from a pure member function? If it is not, how can I wrap my foo function from object obj to be able to create such thread?
Thanks in advance!
This is the compiling error:
thread_test.cpp: In function ‘int main()’:
thread_test.cpp:32:22: error: no matching function for call to ‘std::thread::thread()’
thread_test.cpp:32:22: note: candidates are:
/usr/include/c++/4.6/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(), _Args = {}]
/usr/include/c++/4.6/thread:133:7: note: no known conversion for argument 1 from ‘’ to ‘void (A::*&&)()’
/usr/include/c++/4.6/thread:128:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.6/thread:128:5: note: no known conversion for argument 1 from ‘’ to ‘std::thread&&’
/usr/include/c++/4.6/thread:124:5: note: std::thread::thread()
/usr/include/c++/4.6/thread:124:5: note: candidate expects 0 arguments, 1 provided
You need a callable object taking no parameters, so
thread t3(&A::foo, &obj);
should do the trick. This has the effect of creating a callable entity which calls A::foo on obj.
The reason is that a non-static member function of A takes an implicit first parameter of type (possibly cv qualified) A*. When you call obj.foo() you are effectively calling A::foo(&obj). Once you know that, the above incantation makes perfect sense.