C++ :: Accessing Private Variables In Structures

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


ADVERTISEMENT

C/C++ :: Accessing Private Arrays?

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

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 View Related

C++ :: One Object Accessing Private Data Of Other?

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

C++ :: Accessing Private Members From Inside Class

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

C++ :: Accessing Private Members From Header File

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

C++ :: Accessing Private Members Of Same Struct Type

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

C++ :: Accessing Data Declared Private Within Header File?

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

C++ :: Accessing Private And Protected Member Functions Of Class?

Mar 30, 2013

how to access the private and protected member functions of the class.....

View 5 Replies View Related

C++ :: Const Method Accessing Private Data Member Giving Access Violations

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

C++ :: Bit-checking Across Multiple Classes Private Variables

Oct 6, 2013

I am writing a bit-check function just to make it easier on myself to check status flags in my classes. I use char variables and each bit represents something on or off. Since I have numerous classes that will use this functionality, it makes sense to write and compile the code only one time rather than for each class. I was thinking of writing the function and including it as a "friend" function to each class that needs it. Is that an appropriate way to do it?

View 2 Replies View Related

C++ :: Passing Both Private And Public Variables-functions?

Mar 6, 2015

How to properly pass both private and public variables/functions from one class to another?

View 3 Replies View Related

C++ :: How To Change Private Variables In Header Files

Oct 28, 2014

How would I change the private variables in the header files and the code in the cpp files for the primary indexes so they use a dynamic array or vector instead. For the primary index, the initial vector size will be 8.

header

#ifndef MY_PRIMARY_INDEX_H
#define MY_PRIMARY_INDEX_H
#include
#include
#include
#include

class PrimaryIndex

[Code] ....

View 3 Replies View Related

C++ :: Accessing Variables From Other Files

Sep 26, 2014

I don't have in depth code or anything. I tried this but can't seem to wrap my head around it.

Code: //header.h
namespace test {
int arr[5];

[Code] ....

Also tried putting int arr[5] in a Test class within test.h.

I have 2 structs in another file, the main, and want to make an instance of the arr variable, in a separate header, for each.

View 2 Replies View Related

C++ :: Accessing Variables Of Another Class?

Aug 3, 2013

I've been attempting to create a game with curses and I keep running into the problem of scope. I want to change or use variables of a different class in a different header file. But I don't know if I should use pointers, references or neither. Should I be programming in a manner that doesn't make it necessary to use variables outside of their class?

View 1 Replies View Related

C++ :: Accessing Variables From Another Project

Apr 4, 2013

I have a Visual C++ solution file that contains 3 projects. i want to access the variables declared in a function in a project from a function in another project. Function declarations are in .h file and expansion in .cpp files. Can i use friend class or any other suitable method.

View 6 Replies View Related

C++ :: Unable To Access Private Variables Belonging To Object Class

Nov 21, 2013

I'm unable to access private variables belonging to the object class Date, which my overloaded >> operator is a friend of. I can't see anything in my code that would be causing this error. The .h file and the definition of the problematic overloaded operator from the implementation file are below:

#ifndef DATE_H
#define DATE_H
#include <string>
using namespace std;
class Date {
public:
// Initializes a date to the default value of January 1, 1970.

[Code] .....

The error message states that the vars (month, day, year) are declared as private in the header file and then a reference is made to the lines where I attempt to access these in the .cpp file and it reads: "in this context".

View 5 Replies View Related

C++ :: Accessing Class Variables In Main

Apr 16, 2014

I am not able to access the class variable noof_vertex in the function merge , the error is the variable is private . below is the code :

#include <iostream>
#include <ctime>
#include <cstdlib>

[Code]....

View 1 Replies View Related

C++ :: Accessing Classes Member Variables Nested Inside Another Class

Feb 22, 2013

I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...

class Package{
public:
Person Sender;
Person Reciever;
int weight;
double cost;
friend ostream &operator<<(ostream &out, Package &pack);

[Code] .....

So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?

View 2 Replies View Related

C++ ::  Why Can't Get Access To Private Function

Aug 11, 2014

I've written a simple class given below. I had set the values through setab() function but the add()function didn't work.

Compiler shows CPP/class.cpp|20|error: ‘int Calc::add()’ is private|

#include <iostream>
using namespace std;
class Calc

[Code]....

View 5 Replies View Related

C/C++ :: Why To Use Private Fields In A Class

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

C/C++ :: Data In Private Class

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

C++ :: Private Member Gets Called?

Jul 12, 2012

I was trying some virtual mechanism then This came to my mind

#include <iostream>
using namespace std;
class A
{

[Code].....

now My concerns is that though the function f() in B was private it gets called by the pointer of class A as it is a virtual function

so is this a violation of access control?

View 4 Replies View Related

C++ :: Unable To See Enclosed Private Class

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

C :: How To Create Private Fields In A Struct

Nov 6, 2014

How can you actually create private fields in a C struct? I got the concept of VTable and inheriting methods aswell overriding them, but private variables can be obtained with static keyword in sourcefile. If you have variables in a struct, you can access them as soon as you got a ref to the struct. The functions are easy:

Code:

struct classA {
pointerToFunction *p;
} static void* thePrivateFoo() { }
void* publicFoo() {
thePrivateFoo(); /* or something like that */
} ...
/*in init code somewhere in the c file */ classAInstance->p = publicFoo;

But I was thinking about the variables... How is this achievable? I was thinking of a struct with only get/set functions and with no datamembers at all. All vars to be static outside the struct. But this kind of destroys the encapsulation.

View 4 Replies View Related

C++ :: Private Global Variable To Class?

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







Copyrights 2005-15 www.BigResource.com, All rights reserved