Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include<iostream>
class FooA
{
private :
friend class FooB;
void Hello();
void Hello2();
private:
void Hello3();
int m_iData;
};
class FooB
{
void fun()
{
FooA objA;
objA.Hello(); // right
objA.Hello2(); // right
objA.Hello3(); // right
//ojbA.m_iData = 0; // compile error
}
};
usually if we access Data member Function or Member variable which is private by an object directly throws error.
but in this scenario how it is able to access Hello(),Hello2(),Hello3() Functions and why it's throwing error in accessing m_iData.
The b and the j in objA have been swapped to form the unknown identifier ojbA.
Change ojbA.m_iData = 0; to objA.m_iData = 0;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Is there any way to use a class member function from outside?
As far as I know for this it would be necessary to somehow inject a private component into it.
For example (this is just an example the notFooClass function does not compile):
class FooClass{
private:
int i;
public:
FooClass(int x){
this->i = x;
}
int f(int num){
return i+num;
}
};
int notFooClass(int num1, int num2){
return FooClass(num1)::f(num2); //
}
int main(){
FooClass x(10);
std::cout<<x.f(5)<<std::endl;
std::cout<<notFooClass(10, 5)<<std::endl;
return 0;
The output should be:
15
15
Is it even possible to do something similar?
public methods can be called from outside. Thats what they are for.
notFooClass creates an instance and calls a member function. Thats basically the same as you do in main. The difference is only that you are using an unnamed temporary and wrong syntax:
int notFooClass(int num1, int num2){
return FooClass(num1).f(num2);
}
or with a named object to illustrate the similarity to main:
int notFooClass(int num1, int num2){
FooClass x(num1);
return x.f(num2);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The static function getROI always returns 1 and also causes a warning :
the address of 'static float Account::getROI()' will never be NULL
#include <iostream>
using namespace std;
class Account
{
private:
int account_no;
int balance;
static float ROI;
public:
void setBalance(int x){balance=x;}
int getBalance(){return balance;}
void setAccountNo(int y){account_no=y;}
int getAccountNo(){return account_no;}
static void setROI(float z){ROI=z;}
static float getROI(){return ROI;}
};
float Account::ROI =0;
int main()
{
cout << "STATIC" << endl;
Account obj;
obj.setAccountNo(3435647);
obj.setBalance(1000000);
obj.setROI(4.9);
cout<<"Account No : "<<obj.getAccountNo()<<endl;
cout<<"Balance = "<<obj.getBalance()<<endl;
cout<<"Rate of int= "<<obj.getROI;
return 0;
}
You want obj.getROI(), not obj.getROI
It says the address will never be zero.
Which is true ... if the address were zero, reading it would be de-referencing a null pointer.
So you think you're looking at the data, but you're really looking at a pointer to the data, and (I presume) comparing that address to zero.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am getting the error below:
..\src\test.cpp:17:20: error: expected unqualified-id before 'try'
friend int try(Complex);
Please help and tell me why this code is producing errors:
#include <iostream>
using namespace std;
class Complex
{
private:
int a, b;
public:
showData()
{
cout<<"\na= "<<a<<" b= "<<b;
}
Complex(int x, int y)
{ //constructer
a = x;
b = y;
}
friend int try(Complex);
//friend function
};
int try(Complex c)
{
cout<<"You areworking in try now";
cout<<"\noutput from friend fun : "<<c.a<<" "<<c.b;
}
int main()
{
Complex c1(3, 4);
try(c1);
return 0;
}
First let's simplify the problem to a Minimal Complete and Verifiable example that duplicates the problem:
int try;
Not much code is required because try is a reserved word. You cannot use try in a program without the compiler expecting a try/catch` block.
Solution: Do not use try as an identifier. Instead use try_func or something that describes what is being tried.
Additional note: showData() needs a return type. Most likely it should be void showData()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The function call below generates compiler error:
void myfun() {}
myfun(); // error:
// gcc -- error: expected constructor, destructor, or type conversion before ';' token
// clang -- error: C++ requires a type specifier for all declarations
int main()
{
// ...
}
Can somebody explain it? How is the function call confused with a declaration? Why does the error go away if the function call is moved inside main()?
You cannot execute function calls outside of functions unless you use them for variable initialization. So you should write ...
void myfun() {}
int main()
{
myfun();
}
or
int myfun() { return 1; }
int dummy = myfun();
int main() {
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have an abstract class with N protected members:
class Something {
protected:
UINT someVal;
std::vector<SomeType> g_MyVec;
// some virtual abstract methods ...
public:
UINT getSomeVal() { return someVal; }
std::vector<SomeType> GetVec() { return g_MyVec; }
}
class SubClass : public Something {
public:
SubClass() { // no members in this class, all inherited from super
someVal = 5; // this sticks
g_myVec = { .. correct initialization }; // this doesn't stick
}
}
The client of this code does:
Something* s = &SubClass();
s->getSomeVal(); // OK, has 5 in it.
s->GetVec(); // Nada, it returns 0 size, nothing at all... WHY?!
Enlightenment is much appreciated.
You are taking an address of the temporary. It's a UB and incorrect code. Subclass gets destroyed along with the vector after the ;
Correct way to do this would be (Assuming no C++11):
Something* s = new Subclass();
s->getSomeVal(); // OK, has 5 in it.
s->GetVec();
delete s;