C++ :: Class Data Member Access?
Jul 5, 2013
I have some doubt regarding class data member accessing in another file.Follwing code showing error.
class A://file a.cpp
{
public:
int add;
int sub;
};
//file b.cpp
extern class A
void cal()
{
A::add=A::sub;
}
View 4 Replies
ADVERTISEMENT
Sep 4, 2014
So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?
View 19 Replies
View Related
Sep 9, 2013
if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class. Is there anyway to acces the private data members? Here's a coding example
class A {
private :
int a;
};
class B : public class A {
public :
void displayA() { cout<<a<<endl; }
};
how i can acces the a of base class A in derived class B without acces modifiers.
View 16 Replies
View Related
Jan 10, 2015
I have the following scenario :
Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}
I need to access mem_ptr (pointer to a class member) through :
a. object (t1)
b. pointer to the object (ptr).
How can i do that ?
View 4 Replies
View Related
Oct 24, 2013
This question is more from a design point-of-view rather than coding it to be a fully functional.
So here it goes:
I have multiple files which each require their own object of same class type (ref. First Class). File contents are read from a file to a unordered_map<std::string, std::vector<std::string>> which is either private or protected member inside First Class. First Class does not need any public functions to add, remove or change the data during runtime, but changes are only being made by checking if the file size has changed during the day, if the size is not equal to the last check, map gets updated.
Now, I have a Second Class which is a data handler class. It has public member functions with arguments that needs to be get from First Class's unordered_map using const_iterator. Which way to go with design and implementation.
I know there's two methods to do this. Re-doing handler class is also not out of the question. These two methods I'm aware of are:
1. Declare these maps to local scope, build few global functions and here we go. (Probably the easiest way.)
2. Create public member functions to a First Class which either return a pointer or a reference to a protected/private member. (I'm under the impression that I really shouldn't be doing this because of a bad coding practice.)
Note that I don't need any code here, just some other point-of-views regarding the subject itself for learning better coding practices.
View 5 Replies
View Related
Aug 22, 2013
I have a method:
int filetodb(std::wstring szwf, SQLHANDLESTR *h);
I want to use it, in a thread.
std says, use thread, as:
std::thread second (bar,0); // spawn new thread that calls bar(0)
How can I do this, for my method, that uses more than one, i.e., two, parameters?
My code is:
std::thread thread = std::thread(filetodb, filesP->at(i), h);
compiler says:
Error10error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'c:program files (x86)microsoft visual studio 11.0vcincludexmemory06061ConsoleApplicationa
How, can I do this?
View 2 Replies
View Related
Feb 28, 2014
Suppose I make a class, something like having the constructor being invoked first makes sense, I don't have a problem with that. But, how could I limit access to functions until certain functions are called? Perhaps this isn't built into the language so you can't. And maybe this problem never comes up. For example if you have a set() and get() functions, if they are both public functions, there doesn't seem to be a way for the compiler at least now if set() never gets called you shouldn't call get(). I just see this as error prone if you need to use libraries, you have to know not to do it from documentation instead of something the compiler can check.
View 11 Replies
View Related
Dec 4, 2014
I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem. My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members. I understand that compiler should know that this class instances will exist during all program execution. I have tried to set static member class Foo instance in another class , but this didn't solve the problem. How to correctly implement signal handling in such case.
Here is example of my code
class MyContainer {
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] ....
View 1 Replies
View Related
Feb 25, 2013
When I put boost::thread Thread; in my struct I get the error error C2248: 'boost::thread::thread' : cannot access private member declared in class 'boost::thread'
The whole struct is
struct Player {
bool key[256];
char nameString[64];
bool lMouseButton;
bool rMouseButton;
double mouseX;
double mouseY;
[Code] ....
View 1 Replies
View Related
Dec 30, 2013
I have `MainShop`(base class) then `SwordShop` and `BowShop`(derived classes) and another derived class called `Inventory`. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?
//MainShop.h
#pragma once
class MainShop {
private:
//some variables
protected:
vector <string> WeaponInventory;
[code]......
Here every time I run it, it goes straight to the `else` and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items.
View 3 Replies
View Related
Apr 1, 2014
I am currently doing the assignment about linked list. Here are some details information about what I am doing.. This program is C++ and should run on Visual Studio 2010. And it contains three file, two datastructure header and one main cpp file.
This program is trying to arrange and show some sports records. The main program which contain the functions such as reading the result text file(each result text file contain several records of athletes), removing a file, arranging the totalresult and printing it out. And the main program is already given and I cannot overwrite it.
But when I finished and try to build the solution and run it, I am not able to run the program and it give me somethings like these...
warning C4172: returning address of local variable or temporary
error C2248: 'Datastructure1::Datastructure1' : cannot access private member declared in class 'Datastructure1'
see declaration of 'Datastructure1::Datastructure1'
see declaration of 'Datastructure1'
This diagnostic occurred in the compiler generated function 'Result::Result(const Result &)'
And I have tried to comment each function part of the header file and see if can run or not. But I still fail to do so. Here are my codes...
#ifndef DATASTRUCTURE1_H
#define DATASTRUCTURE1_H
class Datastructure1 {
Public:
Datastructure1( );
[Code] ....
There are two header files and look quite long. They are all some linked list functions . I have read and learn linked list data structure before I complete this programs. However, when I complete the functions required, the function cannot be compile....
View 11 Replies
View Related
Oct 17, 2014
My code is here [URL]
void Player::Display() const
{
cout << "
Player Name: " << GetName() <<
"
Grade: " << GetGrade() << "
G.P.A. " << GetGPA() << endl;
}
The problem occurs in here, I get access violations, is there a way to this while keeping Display const or is this code valid and my problem is somewhere else and not being caught in the debugger? I tried to make the return types const - but that didn't work .....
//Getters need const twice for this to work?
const char* Player::GetName() const {return m_name;}
const int Player::GetGrade() const {return m_grade;}
const double Player::GetGPA() const {return m_gpa;}
[Code].....
View 2 Replies
View Related
Dec 4, 2014
I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
What is correct approach to do this. How to correctly implement signal handling in such case.
Here is example of my code
Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] .....
View 4 Replies
View Related
Aug 18, 2013
can we make a class without any data member ? but it may have member functions ! in c++
View 2 Replies
View Related
Dec 30, 2013
If I want a class with a vector data member, can I specify it as follows?
std::vector< bool > integers( 101 )
I'm having some problems when compiling code.
View 3 Replies
View Related
Aug 18, 2013
is this possible to make a class without any data member in c++ ?
View 1 Replies
View Related
Oct 29, 2014
I recently discovered the new - new to me anyway! - feature of modern C++ that allows you to set the initial value of a data member when you declare it:
class CINTWrapper{
private:
int m_iData=0;
};
This even extends to calling member functions that work with initialization I believe:
class CStringWrapper{
private:
wchar_t* Allocate_Array(const int iBufferSize);
wchar_t* m_pString=Allocate_Array(1);
};
At first, this seemed an extremely useful piece of functionality that C++ had been lacking all along. However, the more I thought about it the more it struck me this feature actually undermines one of the principle design elements of the language - that being the Constructor.
As I understand it the primary purpose of the Constructor is specifically to give the programmer a place where it is guaranteed he can always initialize his data members before anything else is done with the class. However, given the new initialization rules this is no longer necessary. So it largely seems to me that Constructors as a whole are no longer necessary either! Copy-Constructors are a special and vital case. Admittedly when I was using them for their intended purpose I hated either the redundancy you had to introduce across multiple Constructors; those with and without arguments and so on, or alternately the fine tuning of helper-functions to do common initialization between these variants. Now however I sort of regret this cast-iron rule has been taken away.
As a last point, I am trying to change the way I think about programming. I am trying to employ more objects than pure C-style ('int' or 'double', etc) data types and especially to move into templates (although absolutely NOT the Hewlett Packard template library!). Given my current understanding of inheritance in particular it seems to me that using pre-initialized data members rather than Constructor-initialization makes object derivation even more complicated, not less so.
View 16 Replies
View Related
Aug 20, 2013
What is the size of object in c++ , if there is no data member in the class ?
View 3 Replies
View Related
Jul 25, 2012
Let's take a look at the code first,
Code:
class B
{
public:
int data;
};
class D : public B
{
public:
int data;
};
Both B and D defines data. I wonder if there is any difference between them?
View 2 Replies
View Related
Jan 9, 2014
I have a small class with a static int data member. I want to zero init it. I am thinking that making a .cpp file with only one line seems too much, isn't it?
So, can I do it inside the the header file? The variable is going to enumerate how objects were created (so any alternative will do).
View 7 Replies
View Related
Apr 17, 2013
"You cannot initialize the static data member in the class definition — that’s simply a blueprint for an object and initializing values for members are not allowed. You don’t want to initialize it in a constructor, because you want to increment it every time the constructor is called so the count of the number of objects created is accumulated."
Why don't you want to initialize it in a constructor?
Edit: Because every time it is called it will set it back to 0 or whatever the initializing value.
View 2 Replies
View Related
Jan 24, 2014
I have to implemente the to_string method. Whats the fastest way? Stringstreams. But I have to use C++ without any headers, so I need to implement the stringstream class. How can an stringstream hold one float? An double? Hoq cqn I implement an strigstream myself?
View 8 Replies
View Related
Jul 11, 2012
For example, in a header file A.h, I define an abstract class,
Code:
// A.h
class A {
public:
virtual void foo() = 0;
private:
static int _x;
};
How'd I initialize static member data _x?Normally, we initialize a static member data in a cpp file. However, there is not cpp file for A.h. If I intialize _x in header file, there will be linker errors like mulitple defined symbols. What is appropriate way to do that?
View 4 Replies
View Related
Sep 9, 2013
Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.
class A{
protected:
static derivable int val;
// A::val
}
class B : public A{
// static derivable int val is already here
// A::val AND B::val
}
This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)...
View 11 Replies
View Related
Apr 17, 2013
From my book:
"A static function might have this prototype:
static void Afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following:
aBox.Afunction(10);
The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:
CBox::Afunction(10);
where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."
Why exactly cant Afunction access non-static members?
View 7 Replies
View Related
Dec 16, 2012
In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.
void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}
View 4 Replies
View Related