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


ADVERTISEMENT

C++ :: Cannot Access Private Member Declared In Class (header File)

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

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++ :: 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++ :: 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++ :: Accessing A Vector That Was Created In A Separate Header File?

Oct 26, 2014

i have this vector:

#ifndef new_thing_Inventory_h
#define new_thing_Inventory_h
#include <vector>
#include <string>
using namespace std;
class Inventory {

[code]....

so i know i need to use .push_back or .pop_back, but the program im using dosn't even recognize that inventory is a created vector.

View 6 Replies View Related

C++ :: Cannot Access Private Member Declared In Class

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

C++ :: Boost Threading - Cannot Access Private Member Declared In Class

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

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++ :: 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 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 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/C++ :: Hangman Header Files - Globally Declared Array

Mar 4, 2014

I need writing my header files for my program of hangman. I've written what I could, but when I try parts out on their own to see if they work, I get errors. I haven't written my driver yet since I wanted to get this header working first. The first function needs to take the name of the file and read in its contents into the globally declared array. The second function takes no arguments and returns a word from the word list chosen at random. I guess my question is, would what I have so far work, or am I completely off? Here's what I have so:

randword.h
#ifndef _randword_h
#define _randword_h
//static char words[100][50];
/*
*Function: InitDictionary
*This function reads in the dictionary of words and puts them into an array. */
void InitDictionary(void);

[Code] ....

View 14 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++ :: Cannot Access Private Member Declared In One Class But Can In Another Class

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

C :: Accessing Static Functions In Header Via Macro

May 11, 2013

Just wandering, will something like this work? header

Code:

#define func(arg) func_(arg) ...
static void func_(arg); code Code: #include "header"
int main()
{
func(arg);
}

View 1 Replies View Related

C :: Parsing Binary Data File By Casting A Buffer - Accessing Double From Struct

Jan 4, 2014

I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.

Code:

typedef struct InvoiceRow {
uint INVOICE_NUMBER;
...
double GROSS;
...
char VAT_NUMBER[31];
} InvoiceRow;

If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...

Code:

typedef struct example {
double d;
}

example;
example *blah = (example*)row->GROSS;
printf("%f", blah->d);

View 7 Replies View Related

C++ :: Zero Init Data Member Of Class In Header File

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

C++ :: Scanning A Header File To Save Image Data

Jun 11, 2013

I'm trying to read from a header file to take the image dimensions and other variables but I don't understand how to iterate through the "key" so it saves each line.

This is what the header file will always look like:

!INTERFILE :=
!imaging modality := nucmed
!version of keys := 3.3
;
!GENERAL DATA :=

[Code].....

Ideally it would just skip the keys that aren't wanted and keep moving through the lines. Should there be a for loop for the key (and if so, how does that work with pointers?) or should this method just be scratched...

View 6 Replies View Related

C :: Create New Data Type Data Declared As Int

Feb 1, 2015

I'm trying to create a new data type Data declared as int. What comes up to my mind is to create a structure like

Code:

typedef struct Data {
int number;
} Data;

but when I need to use the data I need to go through the structure. Is this a good way to declare a new data type?

View 2 Replies View Related

C++ :: Returning Datatype Other Than The Declared Function Header Datatype

Jun 6, 2013

I have designed a class called matrixType that has some overloaded operators (+, -, *, and <<); the arithmetic operator functions of which are overloaded as member functions of the class. As an alert mechanism, I want a message displayed when two matrices of dissimilar sizes are added/subtracted OR when two incompatible matrices are being multiplied. Displaying this error message is not the problem. However, I want a scheme where on detecting two matrices’ incompatibility, the operator function returns the error message (a string datatype) instead of what would be an erroneous result (the expected matrixType object).

In other words, what I may be essentially asking is: Is it possible for a function, say,

matrixType matrixType::operator+(const matrixType& otherMatrix) const
{
.
.
.
}

to return a dataType (like a string) other than the expected matrixType?

View 1 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++ :: 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++ :: Private Data Member Is Accessed And No Error

Apr 24, 2014

Here in below code, the private data member is accessed directly using dot operator IN COPY CONSTRUCTOR and the program runs without error.

#include <cstdlib>
#include <iostream>
using namespace std;
class array {
int *p;
int size;
public:
array(int sz)

[code]....

View 1 Replies View Related







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