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
ADVERTISEMENT
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
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
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
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
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 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
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 30, 2013
I came across the below code snippet in a project and was not sure how value of variable "response" is computed. Here as we can see, pic_data holds two one dimensional arrays but "response" access both the single dimensional array as two dimensional array.
Code:
#define MAX 100
#define MAXBUF 100
u32 response;
u32 index;
}
[code]....
View 3 Replies
View Related
Feb 9, 2014
I'm confused about accessing elements from arrays. Supposed I have an array of pointers to char:
char *names = { "John", "Rose", "Steven" };
To access one of the strings, should I use names[ 0 ][ i ], where i is an index in the set ( 0, 1, 2 ), or should I use names[ i ]? I would think it would be the first option, because this array has 1 dimension that contains others arrays, right?
View 8 Replies
View Related
Mar 31, 2013
We are learning how to use composition. In this project there is the Main (), resistor .cpp and .h, capacitor .cpp and .h, and filter .cpp and .h. I have 4 arrays built in the resistor.cpp that I want to print through the main(). I put the arrays in the constructor and have watched them initialize with the proper values, but as soon as the program leaves the constructor the values are wiped away and the elements are left with the value " -858993460 " in all of the them.
Here is the code:
Filter Main()
#include "Capacitor.h"
#include <conio.h>
#include "Filter.h"
#include <iostream>
#include "Resistor.h"
#include <windows.h>
#include <iomanip>
[Code] .....
View 9 Replies
View Related
Mar 1, 2014
I'm working on a project where I have the user enter storm data about hurricanes for multiple seasons. I need to make separate arrays for:
1. the month, wind speed, and min. press.
2. the category of the storm
What I'm having trouble understanding is how to make the software take an input, assuming from a scanf, and put it in an array. Then taking that value and output-ing it back out when needed.
So here is piece of my code. Note: Disregard the commented sections of the code, my current project is building off a previous one, where in the last project we were using loops, now we are using arrays.
case 1:
{
if (year > 0) //begins counting
{
/*num_trop_storms = 0*/ num_hurricanes = 0, year = 0, wind_speed = 0,
hcat1 = 0, hcat2 = 0, hcat3 = 0, hcat4 = 0, hcat5 = 0, hrun = 1, totalhurr = 0;
knots = 0, cummwindspeed = 0.0;
[Code] ....
In the code above, how exactly the scanf takes the inputs from the user and then stores them for me to access. So, I want my array to be 3 columns, for months , wind_speed, & minpress. The array has a undetermined amount of rows. I think if I figure out the first array, I can handle the second.
View 7 Replies
View Related
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
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
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
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
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
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
Apr 3, 2013
I've created a class called Voter with a private member variable ID, also I have a variable in my main function to be ID as well. I'm trying to compare the two ID's but when I do so:
if (ID == V.ID)
I get the error - 'std::string Voter::ID' is private within this context.
I know that because it's private I can't access it, but how do I?
View 3 Replies
View Related