C/C++ :: Accessing Private Class?
Apr 4, 2014
I am trying to access the private member from main . The code is as follow. I wanted to call below function from main.
uint16_t Modbus::calcCRC(uint8_t u8length)
#define MAX_BUFFER 64
typedef struct {
[Code]....
View 1 Replies
ADVERTISEMENT
Jan 10, 2013
Let's say I have the following class:
class MyClass {
private:
int m_myInt;
public:
int myInt() {return this->m_myInt;};
int myFunc();
};
Which of the following is to prefer;
int MyClass::myFunc() {
return 2*this->m_myInt;
}
or
int MyClass::myFunc() {
return 2*this->myInt();
}
The second one seems better? Are both OK? What's the best practice?
View 13 Replies
View Related
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 Replies
View Related
Aug 31, 2014
How do i access the private array? I tried this and it's not working. Want i want to do is create class object in main and pass the string to constructor, and set m_make, m_model to that string. Then call the member function to output that.
const int BUFLEN = 256;
// class declaration
class CVehicle
{
[Code].....
View 2 Replies
View Related
Apr 18, 2013
I need to translate a C program to C by making variables in structures private(no classes yet!) and putting public inline functions. There's a good chance that I have much more problems with my code than I'm asking right now, but I have 4 spots that I'm currently stuck in and can't access properly.
My structures:
Code: struct Container
{
private:
int count;
char** lines;
int nlines;
[Code] .....
View 4 Replies
View Related
Feb 26, 2012
Basically, I've got one object which has to access private data in another object... and can't.
Here's the specifics: I'm writing a little war game program where players deploy units (soldiers, tanks, planes, etc.) onto a gameboard. Players and Units are modeled as objects:
Code:
class GameUnit {
public:
string GetName() {return Name;}
protected:
string Name;
};
class Player {
[Code] ....
Here's the problem: In the above code, Player's ListUnits() function doesn't work because Player can't access GameUnit's GetName() function.
Specifically, here's the compiler's error message:
Code:
In file included from Main.cpp:18:
Player.h: In member function 'void Player::ListUnits()':
Player.h:47: error: 'GetName' undeclared (first use this function)
Player.h:47: error: (Each undeclared identifier is reported only once for each function it appears in.)
I've tested enough to realize that the problem is the GameUnit::GetName() function is a public function within the GameUnit object. Why can't a Player call this function? Making both friend classes of each other doesn't work.
View 3 Replies
View Related
Dec 27, 2013
i have seen many c++ programs, where the private members from a header file are accessed in the source file. why is happening? As to my knowledge a private member cannot be accessed until it is friend function or member.
View 2 Replies
View Related
Mar 26, 2013
I've been reading the tutorials on Friendship and Inheritance [URL] ..... but I still don't understand why I can't access members of the same struct type.
bool wordBeginsAt (int pos) {
if (pos == 0)
return true;
///use the 'message' string
Message go;
return isAlphanumeric(go.messageText[pos]) && (!isAlphanumeric(go.messageText[pos-1]));
}
The code above is located in a source file, where the function isAlphanumeric passes a char value, and Message is the struct containing the string I want to access. Below is the declaration of the struct and string located in the corresponding header file.
struct Message{
.
.
.
private:
std::string messageText;
};
My frustration comes when I try to call and assign messageText like the tutorial does to its private members, but I keep getting an error saying I can't access the string because it is a private member. Is there a way to access the string without having to pass it through the function wordBeginsAt?
View 6 Replies
View Related
Feb 10, 2015
I have a header file that declares some fields as private, I then have a class that I need to compare two of the objects' information for equality but neither of them are the calling objects. I cannot alter the header file. How would I go about comparing private data fields? I will enter a brief bit of code for clarity.
Code: // Header File
// stuff.h
class stuff
{
private:
int* arr[20];
int size;
};
bool equal (const stuff& a, const stuff& b);
[code].....
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
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
Aug 9, 2014
Why do you use Private fields in a class if an outside class can change the private fields using get and set properties?
View 2 Replies
View Related
Dec 11, 2014
i have private data look like Peiceorder peiceOrders[20]; it looks like a array but does not have name type like int or char? why is that and what should I use the data should I write Peiceorder peiceOrders[20]=........ like that? or?
View 1 Replies
View Related
Oct 30, 2013
I have a class (Quadtree) and three inner class inside (Node, Inner and Leaf). Inner and Leaf inherit from Node.
I have a function in the private scope of Quadtree.
All these are located in fun.h .
Then, in fun.cpp, I am implementing the function (which is named foo <- what a prototype name!), which takes as argument a pointer to an Inner object. Inner seems unable to be resolved however!
fun.h Code: class Quadtree {
private:
class Node{
public:
Node() { std::cout << "Node
"; }
};
class Inner : public Node {
public:
Inner() { std::cout << "Inner
[code]....
View 2 Replies
View Related
Sep 30, 2014
I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable
Is there a way to do this?
static int destructorCount;
View 8 Replies
View Related
Feb 16, 2013
I get a problem with the vector as a private class member: When I did't initialize the vector in constructor(which means the size of the vector would be 0), I used a class function to add two elements to the vector and it worked (because I added a "printf" to output the size of the vector and the elements within that function). However, when I used another class function to visit that vector, no element was in and the size became 0.
Then I tried to add two elements to the vector during the construction, and it turned out that these two elements could be stored in the vector while other elements added through class functions could not.
I guess there may be some problems on the scope of the function. But I feel the class member should not be effected by the scope of the class function.
View 7 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
Apr 14, 2013
The reason that class members are private by default is because, in general, an object of a class should be a self-contained entity such that the data that make the object what it is should be encapsulated and only changed under controlled circumstances. Public data members should be very much the exception. As you’ll see a little later in the chapter, though, it’s also possible to place other restrictions on the accessibility of members of a class.
View 17 Replies
View Related
Mar 10, 2013
I'm trying to make a 3d game. I just started with opengl in c++ but I ran into a problem. What I have now.
Creating the array:
std::vector<Bullet*> bulls;
In the display function:
for (unsigned int i=0; i<bulls.size(); i++) {
[Code] ....
But when I try to compile this it gives an error:
cannot convert ‘Bullet::Get_prevx’ from type ‘GLfloat (Bullet::)() {aka float (Bullet::)()}’ to type ‘GLfloat {aka float}’
View 1 Replies
View Related
Jul 7, 2012
I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that?
View 7 Replies
View Related
Feb 9, 2013
So I'm trying to do a homework assignment, where I read a uml about a bank program, and just create it. Here is the UML. So while working on Bank, the top one, i've come up with this so far.
Code:
#include <iostream>
#include <string>
using namespace std;
class bank
{ string name;
int routingNum;
[Code] ....
I'm assuming that's what the uml wants. However I can't seem to access that private class. Idk why. I declared it as an object in the main.
View 4 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
Jan 16, 2013
it seems that I cannot define a method of an inner nested class if it is a private class. for example:
class outter {
class nested {
void foo ( void ) {} // okay - but is this inline?
} void inner::foo( void ) {} // not okay - cannot define inside another class
} void outter::inner::foo( void ) {} // not okay - 'nested' class is private!
what I want to know is, is there another way to define an inner class's method? and if not, is it eternally doomed to be inline because it has to be declared inside it's own class declaration?
View 5 Replies
View Related
Sep 19, 2014
I've been working on a program that uses a reference counting class that I've written which works fine for objects that inherit from it however I now need to have the following setup:
class SBComponent : private Refcounted { /*stuff*/}
class ProxiedComponent : public SBComponent, private Refcounted {/*stuff*/}
The compiler gives the following warnings
warning: direct base ‘Refcounted’ inaccessible in ‘ProxiedComponent’ due to ambiguity
And then several repeats of the error:
error: request for member ‘bk’ is ambiguous
Back *b = bk<ProxiedComponent>();
bk is a templated function that's defined in Refcounted and returns a pointer of type <template arg>::Back (ProxiedComponent::Back in this case).
I don't understand why the call to bk is ambiguous as although there's two instances of Refcounted (there needs to be with the way I've designed it so I can't make it virtual) it's inheritance is private in both cases so there should only be one instance of it visible in ProxiedComponent.
View 7 Replies
View Related
Aug 9, 2013
The question says that:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
[Code].....
But then the 2nd part of the question itself says that "Acno entered by the user". So we need to identify/search for an account with that Account Number. How can we do this without comparing Acno with S.Acno? I know that S.Acno is not accesible as it is a private member but then how to compare Acno without even using a member function(public) that can return the value of Acno?
View 11 Replies
View Related
Jan 15, 2015
I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using alias:
namespace ns {
class __wrapper
{
private:
[Code].....
I was hoping to see a "__wrapper::__tklass is private" error message in the first using statement as well as during the instantiation of ns::tklass, but no error is issued. I tried this on gcc-4.9.2, clang-3.5.0 and visual_studio 2013 express.
why exactly doesn't the compiler flag tklass as an error? Is it allowed by the standard? If so, wouldn't that be a serious access violation?
View 2 Replies
View Related