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


ADVERTISEMENT

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++ :: Why Class Members Private By Default

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

C++ :: How Could Object Access Its Private Data Members From Outside

Nov 12, 2013

How does an object access its private data members in copy constructor.

The relevant part of the code: Code: C::C(const C &obj)
{
x = obj.x;
y = obj.y;
}

Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?

Here are the complete code:

Code:
#include <iostream>
using namespace std;
class C{
public:
C(int,int);
C(const C &);

[Code] .....

View 7 Replies View Related

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++ :: 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 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 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++ :: Iterating Through Struct Members?

Apr 4, 2013

Suppose I have a struct with 20 members. I want to assign each of those structs with values. Instead of accesing by explicit convention, i.e.:

Code: struct.member1 = 1;
struct.member2 = 2;
...
...
...
struct.member20 = 6;

Is there any way to encapsulate it in a for loop and use an iterator variable to indicate the member? i.e.:

Code: for (int i = 0; i < 20; i++)
struct.i = i;

The above is pseudocode but Im sure you get what Im trying to do?

View 2 Replies View Related

C :: Passing Struct Members To Execlp

Oct 2, 2014

I am trying to pass multiple args to g_signal_connect through a struct and it's members. I think the problem lies with execlp. I am trying to pass the struct members to it but I keep getting errors. No matter how I format execlp and the struct args, I always get an error. The current error I receive is Bad address. Like I have said before, no matter how I format execlp, I receive some sort of error.

Code: /*
Compile me with:
gcc -Wall sshfs-gtk -o sshfs-gtk `pkg-config --cflags --libs gtk+-2.0`
*/
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>

static void destroy( GtkWidget *widget, gpointer data ) {

[Code] .....

View 4 Replies View Related

C++ :: Simultaneously Assign Value To Struct Members?

Feb 25, 2013

If I have a struct of some vector members, like

struct {
vector<double> a;
vector<double> b;
vector<double> c;
// ...... continue
} struct_test

How can I assign a value (push_back()) to all the vector members simultaneously, instead of do it one by one?

View 6 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++ :: Accessing Inside Structure Via Struct Pointer To Struct Pointer

Jun 5, 2012

"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);

[Code] ....

These codes give these errors:

error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'

These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"

I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.

View 3 Replies View Related

C :: How To Generate List Of Struct Members Using Header File As Input

Jun 25, 2014

I need to create a list of all members of a struct using the sturct definition as input. The struct is NOT part of the program, but is the input to the program.

The struct that I am using changes over time as features are added. I need the complete, fully qualified field names to then generate a table with all names with their offsets, type and length.

This information then allows me to create readers of the data that will run on different architectures, compilers and operating systems.

The struct currently has 800+ lines and uses typedef and embedded structs.

Perhaps this could be done creating LEX, YACC, Perl, SED, AWK or other language system.

View 6 Replies View Related

C++ :: Accessing Non-static Members Inside Static Member Functions

Sep 11, 2013

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?

View 7 Replies View Related

C++ ::  Accessing Struct Data In A Queue

Mar 24, 2013

I have a queue of structs. I would like to access one of the two pieces of data at the front of the queue that each struct maintains.

Here is some of my code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <queue>
using namespace std;

[Code] ....

What I am asking is how do I get the priority of the struct item at the front (lane1.front()), in this case?

View 8 Replies View Related

C/C++ :: Segfault When Accessing Member Of A Struct

Mar 18, 2014

The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,

[code]
loadDB(db, configFile);
printf("%d",db->relationCount);
fflush(stdout);

View 5 Replies View Related

C/C++ :: Accessing Elements Of Struct Array

Dec 9, 2014

I've been working for some number of days on this code to take information about movies from both a file and the user, store the infos in an array of structs, and at the end, write all the info out to a file. I'm having some problems with an error message reading:

"prog.c:102:11: error: subscripted value is neither array nor pointer nor vector"

this error occurs in many lines (which I will label specifically below -- they are everywhere where I am trying to access/modify an individual element of a struct element of the array).

A few examples of where I am having the problems are lines:
39, 52-55, 70, 72, and 86 (and more of the same exact variety).

I am obviously rather systematically doing something wrong, but I am quite certain all of these are the exact same mistakes.

I pull up also 2 or 3 other errors, but I don't think they are related and should be able to fix them quickly once I work out this conundrum.

#include <string.h>
#include<stdlib.h>
#include <stdio.h>
int moviecount =0;
typedef struct{
int year;

[Code] .....

View 5 Replies View Related

C++ :: Accessing Pointed-to Value In A Struct Vector Of Pointers?

Apr 30, 2013

I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:

vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:

for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {

if (1) { //checking value of variable not included in snippet

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}

The problem is in these two lines, I think:

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);

These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?

View 4 Replies View Related

C :: Linear Search Function Accessing String In A Struct?

Apr 5, 2013

I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.

My problem is accessing the array to search through.

Here is my code:

Code:
#include "stdio.h"
const int IDLEN=30; //All constant values defined
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
//First structure defined
struct TransistorRec {

[Code]......

The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'

View 11 Replies View Related

C++ :: Ioquake3 Engine - Accessing Limited Struct Fields

Feb 29, 2012

I'm currently working on the ioquake3 engine . The ioquake3 engine is separated into 2 different main threads at runtime: the gamecode and the engine. Both are communicating but not all information and my problem resides here.

In the gamecode, there's a struct called gentity_t which contains a lot of fields:

Code:
typedef struct gentity_s gentity_t;
struct gentity_s {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
// DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER
// EXPECTS THE FIELDS IN THAT ORDER!
//================================
struct gclient_s*client;// NULL if not a client

[Code] ....

This whole entity is passed to the engine at runtime, but only the first two fields are declared for the engine:

Code:
// the server looks at a sharedEntity, which is the start of the game's gentity_t structure
typedef struct {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
} sharedEntity_t;

My problem is that I need to access the health field of gentity_t from the engine. Technically, this is possible, but the health field is not declared in sharedEntity (which is the same memory address than gentity_t in the gamecode), so this is not straightforward.

I am looking for an elegant way to do this, and my constraint is that I must not edit the gamecode, only the engine.

The solutions I've thought:

- Just copy the whole gentity_t fields into sharedEntity_t. This would work I think but would be redundant, and I would like to avoid copying this huge set of fields.

- Include the two headers files declaring the gentity_t and sharedEntity_t structs, and create a Getter and a Setter functions that would cast a gentity_t over a sharedEntity_t and return/set a field. The problem is that I can't simply include them because they are both including some common headers files and this produce a recursive include error (and I can't modify the files to add a check, these are normally in the gamecode).

- Directly access the health field using a clever memory pointer, but I don't even know if that's possible given the huge number of fields prior health with many different types?

View 2 Replies View Related

C++ ::  Storing Static Class Members Of Dynamic Variable Type In DLL

Oct 15, 2013

How I can implement it.

Tickable.h

#include <list>
#ifdef TICKABLE_EXPORTS //Automatically defined by MSVS
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#pragma comment(lib, "Tickable.lib")
#endif

class DLL Tickable{

[Code] ....

error LNK2001:
unresolved external symbol "private: static class std::list<class Tickable*,SKIPPED BITS> Tickable::subs" HUGE_SYMBOL_LIST
PATHTickable.obj

I know with such a tiny and insignificant class the dll export seems pointless but this class is actually intended to be a .lib ONLY. But it is derived from by .dll style classes, and through inheritance this error is the exact same as what appears in the derived class, I just imagine that the cut down version would be easier to work with.

Is it possible to hold either a static variable in a dll which is of a dynamic type, OR would it be possible to reference an external variable which is static throughout the instances and this variable can be chucked away in a namespace of mine somewhere?

I suppose my only other option (if this is possible) would be to define a maximum instance number and create a standard array of pointers but this could both waste so much memory when not in use and cause problems if I need more memory.

View 5 Replies View Related







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