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
ADVERTISEMENT
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
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
Mar 19, 2014
#include <iostream>
#include <string>
using namespace std;
class book {
private:
string bookname;
double bookprice;
public:
book(string k="calculus",double b=25.5)
{ bookname=k;
bookprice=b;
}
View 2 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 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
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
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
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
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 Replies
View Related
Feb 8, 2014
I'm currently trying to access a variable contained within a base object from another completely different object and I continually get error messages. I'll attempt to put up the important code since it's contained within fairly large classes. From the Base .cpp file. ObjectPosition:
void ObjectPosition::init(float x,float y, float speed, int boundx, int boundy, float hit, int lives, bool live) {
ObjectPosition::x = x;
ObjectPosition::y = y;
ObjectPosition::speed = speed;
ObjectPosition::boundx = boundx;
ObjectPosition::boundy = boundy;
ObjectPosition::hit = hit;
ObjectPosition::lives = lives;
ObjectPosition::live = live;
}
This is the initialization function for the BaseObject. All objects are inheriting these variables which are Protected within the ObjectPosition class.Then they are initialized within the Pig class thus wise:
void Pig::binit(float sx,float sy, ALLEGRO_BITMAP *simage) {
//Sets all ObjectPosition Variables
ObjectPosition::init(800,900,10,80,40,40,10,true);
smaxFrame = 4;
scurFrame = 0;
[code]...
I tried to initialize the boundx through the pig via pinitx but I get errors and I can't access through the pig to the object position to the boundx.
View 10 Replies
View Related
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
Aug 20, 2014
I'm trying to get my head around threading and gui applications for fun, and I've ran into a problem I'm not too sure how to google it correctly or I don't understand the answers given. Basically I'm trying to access GUI item properties (add to a listview). I've been on stackoverflow alot, and I've noticed that accessing these properties is quite difficult. At least, I'm not really understanding how it is supposed to be done.
The following line gives me a null value.
frm.lstChat.Items.Add(newList);
From what I understand I need to access my form object instead of creating a new object window... but I'm not sure how to reference the initial form application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ModBot {
[Code] ....
View 3 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
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
Feb 11, 2013
I was wondering what the best way was to access data outside the function it was created in. I have the user input a data in a custom namespace function, and now im trying to access all those data points in a different function.
View 9 Replies
View Related
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
Jan 24, 2014
I've got a Dev Express hierarchical grid and I need to which row is in focus in the level that's in focus. In the code below I need to replace "gridControl1.FocusedView.GetRow(0)" with "gridControl1.FocusedView.GetRow(x)" where x is the row number in the focused row but I can't find a property for it.
private void gridControl1_Click_1(object sender, EventArgs e)
{
//int i = gridView1.GetFocusedDataSourceRowIndex();
[Code]....
View 1 Replies
View Related
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
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
Apr 15, 2014
All i need is to create a "read" function to read all the data for a Player Object.
So Far i made a display function to display all the data for the "Player" object, but i don't know how to make a read function.
Here is my program:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include "player.h"
[Code] .....
View 3 Replies
View Related
May 24, 2014
How do you create an object (like in the title) something more simple than a struct? I wanna know that cuz I'm writing a function that could return a boolean and an integer at the same time.
View 2 Replies
View Related
Jan 29, 2015
I've downloaded a Sage 50 2014 data object and added a reference to it in my project. I'm able to connect to the Sage Account package using:
SDOEngine sdoEngine = new SDOEngine();
WorkSpace workSpace = sdoEngine.Workspaces.Add("MyCompany Limited");
try
[Code].....
View 4 Replies
View Related
Dec 14, 2013
I am writing a program that manages a group of tool bins. This group is handled as an object that is an array of two element structures called InvBin. I initialize the bins with data from a file which contains the descriptions and initial quantities. I also have functions to add or subtract items from a bin and a function to display a report of the description and quantity of all of the bins.
The add and remove functions work correctly based on the cout statement in the functions, however when I display the report, it displays the initial quantity instead of the new quantity. In addition, when I use the add and remove functions again on the same bin, they use the initial quantity.
These are the add and remove functions and the report function from the main program.
Code:
//Adds an item to a bin
void addItem(HANDLE screen, BinManager tools, int &count) {
int binNum;
int addNum;
system("cls");
[Code].....
View 2 Replies
View Related