class member declaration is not accessible [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
#include <iostream>
using namespace std ;
class maths{
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}

In C++ class members default to private. Private members can only be accessed from within the class itself. You need to set the members to be public, like this:
class maths {
public:
// Everything after this label is now public
int a;
int b;
// etc.
// If you want to define some members as private later, do this
private:
int a_private_variable;
};

One way to solve this would be to just replace the keyword class with struct as shown below.The use of struct makes all the member of the class public by default which means the user of the class can access them directly.
#include <iostream>
using namespace std ;
struct maths{//NOTE THE KEYWORD STRUCT HERE
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}

Related

Why does friend function not able to access private variables of a class? [closed]

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 1 year ago.
Improve this question
#include <iostream>
using namespace std;
class base_A {
int a = 5, b = 6;
friend void derive::print(base_A);
};
class derive {
public:
void print(base_A obj) {
cout << obj.a << " " << obj.b;
}
};
int main() {
base_A obj1;
derive obj2;
obj2.print(obj1);
}
Though I've used friend key word I can't able to access the private variables. Anyone please help me on this.
You need correctly to place declarations relative to each other in the program. Otherwise the compiler will issue an error that either a name is undeclared or a type is incomplete. For example
#include <iostream>
using namespace std;
class base_A;
class derive {
public:
void print(base_A obj);
};
class base_A {
int a = 5, b = 6;
friend void derive::print(base_A);
};
void derive::print(base_A obj) {
cout << obj.a << " " << obj.b;
}
int main() {
base_A obj1;
derive obj2;
obj2.print(obj1);
}
In this case the program output is
5 6
That is as the name base_A is referred in the class derive then at least the declaration (forward declaration) of the name base_A shall precede the definition of the class derive.
On the other hand, the definition of the friend function shall know the complete definition of the class base_A. So its definition shall be placed after the definition of the class base_A.
Seems that base_A don't know that derive::print(base_A) exists.
Try divide definition and initialization classes

How to set default value of a class type parameter in c++? [closed]

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
In below code snippet i want to set default value of a classA type variable obj=class(3,5) in func().How to do it without using pointer?Can you give me an example with default contructor ?
#include<iostream>
using namespace std;
class classA{
int a,b;
public:
classA(int x,int y){a=x; b=y; cout<<"contructing........\n";}
~classA(){cout<<"destructing........\n";}
int add(){return a+b;}
};
void func(classA obj=class(3,5);
int main()
{
classA obj(5,10);
func(obj);
func();
}
void func(classA obj=classA(3,5))
{
int getObjVal;
getObjVal=obj.add();
cout<<"\nMultiply returned value with 5 gives: "<<getObjVal*5;
}
For declaration like classA obj; default constructor is needed.
The function default argument is provided in declaration and may not be repeated by function definition.
Using in-class member initializers like int a{}, b{}; the members a and b are 0-initialized by default.
#include<iostream>
using namespace std;
class classA{
int a{},b{}; // using {} -> a and b 0-initialized by default
public:
classA() = default;
classA(int x,int y){a=x; b=y; cout<<"contructing........\n";}
~classA(){cout<<"destructing........\n";}
int add(){return a+b;}
};
void func(classA obj=classA(3,5));
int main()
{
classA obj; // <- default constructor needed here
func(obj);
}
void func(classA obj)
{
int getObjVal;
getObjVal=obj.add();
cout<<"\nMultiply returned value with 5 gives: "<<getObjVal*5;
}
Live

Static member functions in namesapce [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have static member function inside my class which I would like to add in my namespace.
class A{
public:
static void func();
};
namespace myNamespace{
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
I get the following errors:
error: definition of ‘void A::func()’ is not in namespace enclosing ‘A’ [-fpermissive]
error: ‘myNamespace::A’ has not been declared
myNamespace::A::func();
void A::func(){
what is the reason?
because you put a function that belongs to the class A inside namespace. just make the class and function A together inside namespace your code should be like this
namespace myNamespace {
class A {
public:
static void func();
};
void A::func() {
}
}
int main()
{
myNamespace::A::func;
return 0;
}
The declaration and the definition both should be inside the NameSpace for it to work. Here in your code, you have declared the class outside the namespace. You can fix it as follows.
namespace myNamespace{
class A{
public:
static void func();
};
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
Also try to understand the error messages which would save you a lot of time googling.
https://www.crashhandler.com/ ---> Best Practices in C++ {EDIT :: A blog by myself}

How do i Declare methods in public and write the code in private [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
{
private:
vector<int> vector_1;
void function_1(string name)
{
"code for function one goes here"
}
public:
void function_1(string name);
};
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
{
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
};
int main(void) {
MapFunction a;
return 0;
}
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
{
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
};
void MapFunction::AskForInput() {
cin >> input;
};
void MapFunction::Output() {
cout << input;
};
int main(void) {
MapFunction a;
a.AskForInput();
a.Output();
return 0;
}
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
{
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
};
void outer()
{
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
}
void outerFriend()
{
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
}

error in declaring the class type variable

#include <iostream>
using namespace std;
class A{
private:
int x;
public:
A(){
x=0;
}
A(int i)
{
x=i;
}
int Get_x(){
return x;
}
};
class B{
private:
A objA(1);
public:
objA.Get_x();
};
This is my code and it has two classes i.e A and B ..First class runs fine but in class B ..compiler gives the syntax error in the declaration of objB.....But as far as i know it should be correct ...so plz help ....thanks
This initialization is invalid for a data member:
A objA(1);
You need
A objA{1};
or
A objA = A(1);
Besides that, this kind of statement can only happen inside of a function:
objA.Get_x();
The compiler is trying to interpret A objA(1) as a function declaration, which is wrong. You may declare objA as A objA = A(1); (please note that thi is a C++11 feature, you may need to enable it before).
Also, I don't really know what objA.Get_x() should do, but this is also wrong, you can't just access a member outside of a function. Probably, you meant this:
int Get_x() {
return objA.Get_x();
}