Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Why does this code not work:
int main () {
Bob bob;
bob.giveANewFredSomeData();
Fred::sharedFred.getANumber(); //Crashes here due to someData inside fred being NULL
return 0;
}
Class Data {
int aNumber;
}
Class Bob {
void giveANewFredSomeData{
Data data;
Fred::sharedFred = new Fred(data);
}
}
Class Fred {
Data someData;
static sharedFred;
Fred (Data data) {
someData = data;
}
int getANumber(){
return someData.aNumber
}
}
Your code is not working because:
You need a semicolon after the declaration of a class
static is not a valid type
static objects have to be initialized outside of their definition in the class in order to be used elsewhere in the code. You need something like: Fred Fred::sharedFred; before main
Declaration of a function must have () in front of the function name before the {} braces
the classes have to be in the scope of main function for them to be used, and also in the scope of each other depending on what is calling what. i.e. main has to be declared after the class and same for each class that calls another
Properties/methods declared in a class are private by default. To make group of properties/methods public, have the keyword public followed by a colon at the top of the group
Related
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 8 years ago.
Improve this question
I learnt that declaring a class as a friend class enables it to use the contents or members of the class in which it is declared. I used the following code:
#include <iostream>
using namespace std;
class two; // forward class declaration
class one {
private:
friend class two; // friend class declared
int a; // to be accessed later
public:
one() { a = 5; }
};
class two {
private:
int b;
public:
two() {
b = a; // intended to access 'a' and assign to 'b'
cout << " " << b << endl;
}
};
int main() {
one one_obj;
two two_obj;
return 0;
}
Error is: 'a' was not declared in this scope
What I've noticed in most examples of friend class is that constructor 'two()' will use 'class one' as the argument and later use the data member 'a'. But I wouldn't always want to make a new object as an argument to constructor two(). For example, calling constructor one() has already been done and the value of 'a' has already been set. Making a new object would mean doing that again, which might not be favorable. So what it all leads to is that, can I access members of class using friend class but without having to declare an object once again?
Class two being a friend of class one only overrides the access checking.
It still means you must actually refer to a static member of the class, or a non-static member of a specific instance of the class the normal way.
You might profit from choosing a tutorial or book from The definitive C++ book list, and reading up about it all.
b = a; // intended to access 'a' and assign to 'b'
Just because it is friend you cannot directly access it's members. You need to create one object to access it's members.
one o;
b = o.a; //now it should work
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 8 years ago.
Improve this question
I'd like to make a "Player" object that I can access with the functions of the mainwindow, like button click actions.
If I do it this way it won't be in scope:
void MainWindow::on_btn_newgame_clicked()
{
Player p;
}
void MainWindow::on_btn_north_clicked()
{
p->location_y++;
}
I tried instantiating globally but then I could not refer to p.
How and where should I do this?
If you want access to state within a class then that state needs to be a member of the class.
Note: This is not unique to Qt.
For example:
class Foo
{
public:
void memberFunction()
{
p->getName(); // This member function may access member data
}
private:
Player* mPlayer; // This is a class member
};
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 8 years ago.
Improve this question
InC++, is it valid to initialize a private access variable outside of the class definition like this?
class Test
{
private: int a;
public: int b;
}
int Test::a = 1;
The example in your question is not valid. If the member variable is static you can initialize it like you do in your example. If the member variable is not static you should initialize in in either the constructor or class definition.
It also does not matter what the access privilege is and the variables are initialized the same way regardless if they are public, protected, or private .
struct Foo
{
Foo() : a{1} // ctor-initializer
{
b = 2; // ctor body
}
int a;
int b;
int c = 3; // At definition.
};
"is it valid to initialize a private access variable ... like this?"
No. If you want to initialize that variable for each instance of Test, do it in a constructor.
class Test
{
Test() : a{1} {}
private: int a;
public: int b;
}
If you want all the instances of your class Test share the variable a, you can make it static. Il you do, you can initialize it like you said.
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 8 years ago.
Improve this question
How to Interpret the name of class in following piece of C++ code ?
Following code is part of a project which is compiling successfully with g++ compiler.
class ABC::DEF
{
public :
int a;
void func();
};
void ABC::DEF::func() { a = 3; }
ABC::ABC() : OBJ(new DEF())
{
}
ABC::~ABC()
{
delete OBJ;
}
How to interpret ABC,DEF and OBJ in the above code?
How the constructor defined above works ?
The definition of ABC most likely looks something like this:
class ABC
{
public:
ABC();
~ABC();
private:
class DEF;
DEF* OBJ;
};
and what you're looking at is the definition of the class ABC::DEF and the constructors of ABC.
(This is a quite normal way of implementing the "pimpl idiom".)
ABC::DEF is a nested class. If you look at the definition for class ABC, you should see a forward declaration of class DEF. You can give the full definition for such a class outside of the outer class as shown in your question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a problem with the initialization of some classes. Simplified code looks like:
class Base
{
Base(int)
};
class BaseChild : public Base
{
};
class mainWindow
{
boost::shared_ptr<Base> pBase;
void init();
};
void mainWindow::init()
{
this->pBase = boost::shared_ptr<Base>(new Base(12));
this->pBase = boost::shared_ptr<Base>(new BaseChild(12));
}
So the problem is with initialization of BaseChild class which is child from Base class. What am I doing wrong? I thought that parentral class pointer can point on child class.
Generaly my program has to work in such way:
When it starts, there is initialization of parental class (in above example: this->pBase = boost::shared_ptr<Base>(new Base(12));). This already works.
In some case, when some flag change its value, pointer which point on parental class object should be change to point on child class object.
You miss a constructor that takes int in BaseChild. Also constructor in Base is private, which makes it unusable outside Base class.
Try
class Base
{
public:
Base(int);
};
class BaseChild : public Base
{
public:
BaseChild(int);
};