C++ :: Cannot Access Protected Members Of Parent Class
Oct 22, 2014
I am doing C++ data structures exercises, and I am still learning some of the more basic concepts. I have a parent class:
template<class T>
class linkedListType {
public:
protected:
int count;
nodeType<T> *first;
nodeType<T> *last;
private:
};
Derived class:
#include "linkedListType.h"
template<class T>
class orderedLinkedList: public linkedListType<T> {
public:
void mergeList(orderedLinkedList<T> &list1, orderedLinkedList<t> &list2) {
first = list1.first;
...
} private:
};
There is more code in my mergeList() function, but I included the line that is giving me problems. The error that my CodeBlocks compiler is giving me is that 'first' was not declared in this scope.
Strangely enough, if I change it to this->first, the error disappears.
1. Why does it not recognise 'first'?
2. Why would this->first work at all? Is the 'this' object a smart pointer?
View 1 Replies
ADVERTISEMENT
Dec 30, 2013
I have `MainShop`(base class) then `SwordShop` and `BowShop`(derived classes) and another derived class called `Inventory`. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?
//MainShop.h
#pragma once
class MainShop {
private:
//some variables
protected:
vector <string> WeaponInventory;
[code]......
Here every time I run it, it goes straight to the `else` and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items.
View 3 Replies
View Related
Jul 12, 2013
It's hard to give a precise title but here is the question in detail: I have a class, something like this:
Code:
class classA{
public:
void fnA();
...
};
and another class that contains objects of classA:
Code:
class classB{
public:
classA A1;
classA A2;
classA A3;
vector<classA*> vA;
...
};
classB B1;
Now is it possible to access B1.vA from B1.A1.fnA() through some kind of pointer chain like this->parent->vA ? If so
View 7 Replies
View Related
Oct 14, 2014
I mainly code plugins for Cinema 4D .... I used to code in COFFEE (a javascript like language, only for Cinema 4D) and python. But, recently, I started coding in C++.
I start my development on a Mac, using Xcode. But then, to generate Windows versions of my plugins, I take my source code to Visual Studio, perform the necessary adjustments, and create Win32 and Win64 code.
Lately, I was trying to devise a scheme to protect my plugins from being pirated. Yes, I know it is virtually impossible, but if I could make it harder for hackers to crack my plugins, it would be fine.
I made it work fine in python and in C++, but only in Xcode.
In Windows it is difficult. I only need to be able to access a username/password protected folder in my server and check if there is a file there. I don't even need to read the content of the file (but it would be nice to be able to do that too).
I started out using the cURL library but it is so complicated to make it work. I need 32 and 64 bit versions. Then I also have to include three .dll files in the folder of the application (that means that I would have to ask to the people who buy my plugins to copy three additional files to the main application folder... not a good thing )
So, any way to access a username/password protected folder on the net? (the username/password is generated in my code so, of course, I know what are the username/password to provide).
Also, I would need a way for the code to be completely self-contained, meaning that the users would not have to manually add any other files (libraries), besides the folder containing my plugin.
View 14 Replies
View Related
Apr 17, 2013
From my book:
"A static function might have this prototype:
static void Afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following:
aBox.Afunction(10);
The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:
CBox::Afunction(10);
where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."
Why exactly cant Afunction access non-static members?
View 7 Replies
View Related
Mar 17, 2013
I have CAN Dll program for my application,which was separately used.Now I have included the Drivers program into my application Program and I am having this error System Access Violation Exception:Attempted to read or write protected memory.i am attaching the Dll code and Application code which is throwing this error.
[CODE]
The CAN Dll Code
int receive_data(unsigned char *data_output, int *MsgId, XLportHandle g_xlPortHandle){
XLstatusxlStatus, xlStatus_new;
XLevent xlEvent;
unsigned intmsgsrx=RECEIVE_EVENT_SIZE;
char *local_data ="11111111";
DWORD status;
xlStatus_new = xlSetNotification(g_xlPortHandle, &h, 1);
[code]...
My Application Code which is the receiver thread for accessing the messages got onto the CAN bus.
DWORD WINAPI Rcv_Msg(LPVOID param){
int*MsgId = new int;//msg id from CAN
intRcvVal;//0 = there is data in the queue; 1 = there is no data
unsigned int uMsgId;
*MsgId = 0;
unsigned char CanData[8];
[code]...
View 1 Replies
View Related
Sep 8, 2014
How to access the name and file name members on line 42 and 43?
Code:
typedef struct {
char * name;
char * filename;
} FILES;
FILES * files[256];
}
[code]...
How to access the name and filename from within function?
*files[count].name = chTmp;
View 9 Replies
View Related
Sep 11, 2014
files.c
Code:
#include "include/types.h"
#include "include/gaussian.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
[Code]....
I need to access the members .name and .filename to allocate memory and copy strings in there.
Code:
files[count]->name=malloc(64);
files[count]->filename=malloc(64); SIGSEGV error
How to make the reference working?
View 12 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
Feb 5, 2015
Code:
template <size_t s>
class foo {
public:
foo()
{
}
template <size_t l, size_t r>
[Code] .....
The above doesn't want to compile.
error C2995: 'foo<l+r> operator *(const foo<s> &,const foo<r> &)' : function template has already been defined
see declaration of 'operator *'
see reference to class template instantiation 'foo<s>' being compiled
with
[
s=4
]
If I change the operator to return a foo<s> it does compile (but that's not the behaviour I need).
if I move the operator to outside the class (removing the 'friend') it does compile and behaves how I need, but I can't access non-public members so I can't write the implementation correctly.
View 6 Replies
View Related
Oct 24, 2014
I don't understand why my compiler gives me this error when I'm trying to run this code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Airplane
{
public:
Airplane();
~Airplane();
[Code]...
The variable is protected. Yeah, that's right. But shouldn't a derived class be able to reach it? Or is it only in a function that the derived class is able to reach protected variables and isn't able to reach protected variables in the constructor?
View 2 Replies
View Related
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 Replies
View Related
Jan 5, 2015
In the project I'm currently working on I define a class that only inherits from a parent class and takes one argument.
Does this class need to be defined in the header or source file? I read different answers around the internet.
Or is it better to always split definition and logica, even for something like an operator?
View 1 Replies
View Related
Aug 27, 2014
I am currently having an issue with a piece of code that I am writing in which I need to use a vector of a child class as a parameter in a function in the parent class. Below is an example of my code:
#include "child.h"
#include <vector>
class parent {
parent();
function(std::vector<child> children);
// rest of class here
}
When I do this my program doesn't compile. However if I try to forward declare, as shown in the following example, it once again refuses to compile:
#include <vector>
class child;
class parent{
parent();
function(std::vector<child> children);
// rest of class here
}
This time, it refuses to compile because it needs to know the full size of the class child in order to create the vector. How to being able to access the child is essential for my program, so what should I do?
View 3 Replies
View Related
Jul 15, 2014
I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();
[Code] ......
View 3 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
Jul 14, 2014
Firstly I don't really know if this is possible.
This is my Class Diagram: [URL]...
github: [URL]...
I want to redefine the price object of the Book Class. However price is defined at Products Class.
I want the price value change according to the marker value, which is a Book attribute.
If the marker is blue, price gets a value of 10 (e.g.), if it has another value, price is equal to 20.
View 10 Replies
View Related
Apr 5, 2013
From parent class, I mean the class whose obj has been made in the class after that.
#include <iostream>
using namespace std;
class a{
int x,y;
public:
a(int u = 0, int v = 0);
void setXY(int,int);
[code].....
View 1 Replies
View Related
Mar 20, 2013
Below is simplified code consists of two classes, namely Parent and Child.
Child is inherited from Parent.
All member functions of class Parent are declared virtual, and they have been overridden in the class Child.
Code 1:
#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {
[Code] ....
The output of the code:
Child::showID() -- ID is 1804289383
Child::showID() -- ID is 846930886
Child::showID() -- ID is 1681692777
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1957747793
Parent::operator=() invoked.
Child::showID() -- ID is 1804289383
Child::showID() -- ID is 846930886
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1714636915
Child::showID() -- ID is 1957747793
Question:
Why is Parent::operator= invoked instead of Child::operator= ..?
Isn't it already declared virtual and hence would be overridden..?
I need to invoke Child::operator= instead. How to achieve this?
View 12 Replies
View Related
Jul 8, 2013
In my program, I create controls by deriving base objects of them I've made. These controls are then are attached within the OnCreate() function via a method I've created. For example:
Code:
class tChat: public TextBox {
public:
void OnKeyDown(UINT &KeyCode) {
if (KeyCode == VK_RETURN) {
MessageBox(NULL, "Pressed enter!", NULL, 0);
[Code] ....
The use of AddControl() feels quite redundant and is only their to parse a pointer to txtChat's Parent. I'm trying to see if it's possible to remove this line and automatically associate txtChat to fMain.
Currently my hierarchy looks like:
[User's derived Form] -> [MDIForm or Form] -> [FormBase] -> [Object]
[User's derived Control] -> [TextBox, etc..] -> [Control] -> [Object]
The user can then derive the Form and Controls and use their virtual OnEVENT functions to handle all the messages they expose.
So far my first concept is using the order-of-creation based on base-class constructor's being fired to determine which object is associated with what.
If I create a copy of a class (i.e. a Form-derived object), first the Form's constructor is fired, and then the constructor's of any class-based member-variables are fired. Is this a safe assumption? I imagine the only time this could be affected is by another thread creating the object of a Form or Control derivative?
If this assumption is true, I could save the 'this' pointer from the FormBase constructor, and then associate it with each Control via the base Control class' constructor? Then to ensure thread-safety, I could map the current FormBase pointer to the local thread id to ensure no conflict if multiply threads are creating forms at the same time?
I've created some mock-up code before trying to implement this into my main code. The following keeps track of the current Form being created by using a ThreadId-based map. When a control is created it gets the FormBase pointer based of it's ThreadId calling. The control then calls an Attach() function of it's parent Form using the pointer it just got, and parses a pointer to the control. The Form then adds the control's pointer to a list. When the Form eventually parses WM_CREATE, it automatically pulls the controls from the list and fires their virtual Create() functions to build them.
Mock-up:
Code:
#include <Windows.h>
#include <string>
#include <map>
#include <list>
class FormBase;// Forward declaration
class FormMap;// Forward declaration
class Object {};// Base Object
[Code] ....
Is this plausible to use? I imagine C++ does not have many "guarantees" about how it creates objects and when. But I thought it would be safe that it would never create member-variables before the class of them is first created?
View 2 Replies
View Related
Mar 24, 2014
This keeps giving me the error
ecg.h:18:11: error: field "next" has incomplete type
How do I do what I need? It does the same thing whether I use a class or a struct. This is C++ code
struct ECG_node {
double voltage;
clock_t time;
ECG_node next;
[Code] .....
View 3 Replies
View Related
Oct 19, 2014
I am making a very basic parent/child class based program that shows polymorphism. It does not compile due to a few syntax errors reading "function call missing argument list. Lines 76 and 77, 81 and 82, and 86 and 87.
#include<iostream>
using namespace std;
class people {
public:
virtual void height(double h) = 0;
virtual void weight(double w) = 0;
[Code] ....
View 4 Replies
View Related
Jun 28, 2013
I am new to c++ programming and i have written a simple class program to display the name and duration of the project
#include<iostream>
class project {
public:
std::string name;
int duration;
};
[Code] ....
Now i am trying to the write the same program with the usage of member functions using set and get member functions. i have tried the following
#include<iostream.h>
class project {
std::string name;
int duration;
// Member functions declaration
[Code] ....
I am not sure whether above code logic is correct, how to proceed with it.
View 3 Replies
View Related
Sep 16, 2013
I want to define a class, which will have two members, for example, vaporPressureStatus and vaporPressure
enum vpStatus_t {nonesense, unknown, known, saturated};
class pore_t {
public:
vpStatus_t vpStatus;
double vaporPressure;
};
when vpStatus is nonsense and unknown, the vaporPressure should not have a value; and if I calculate out a value for vaporPressure, the vpStatus can be set as known.
I am wondering if there is any set, pair or other structure can hold this two members together, so that when I change one's value, the other guy will also change accordingly.
View 3 Replies
View Related
Jun 24, 2014
I've been creating an API and I'm now stuck on callbacks. There are many APIs that allow callbacks to class members(e.g. Windows API) using a void pointer to the object. I've searched the internet for hours and I can't find one example of how to use the "hidden object parameter" of an class method pointer that doesn't use std::function/bind or boost::function/bind. Any information on how API's like Windows API are able to use class methods as callbacks
View 6 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