C++ :: Why Private Member Template Accessible Outside Class

Jan 15, 2015

I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using alias:

namespace ns {
class __wrapper
{
private:

[Code].....

I was hoping to see a "__wrapper::__tklass is private" error message in the first using statement as well as during the instantiation of ns::tklass, but no error is issued. I tried this on gcc-4.9.2, clang-3.5.0 and visual_studio 2013 express.

why exactly doesn't the compiler flag tklass as an error? Is it allowed by the standard? If so, wouldn't that be a serious access violation?

View 2 Replies


ADVERTISEMENT

C++ ::  How To Make Private Member Accessible Outside Class

Aug 9, 2013

The question says that:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

[Code].....

But then the 2nd part of the question itself says that "Acno entered by the user". So we need to identify/search for an account with that Account Number. How can we do this without comparing Acno with S.Acno? I know that S.Acno is not accesible as it is a private member but then how to compare Acno without even using a member function(public) that can return the value of Acno?

View 11 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++ :: Vector As Private Class Member

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

C++ :: Access To Private Member Map Outside Class

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

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++ :: 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++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

#include <vector>
#include <iostream>
using namespace std;

[Code].....

View 19 Replies View Related

C/C++ :: Defining Class Counter - Member Variable Should Be Private

Oct 23, 2014

The question is: Define the class Counter. An instance of this class is used to count things, but the counter should never be less than 0 (non negative number). The member variable should be private. I realize what I'm suppose to be using but can't implement the member functions needed..

int main(){
int value;
cin >> value;
Counter myCounter(value);
for (int i = 1; i <= MAXLOOP; i++) {
myCounter.increment();

[Code] ....

View 3 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++ :: 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++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C++ :: Template Function As Member Of Class

Apr 15, 2014

I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?

Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )

[Code] .....

View 4 Replies View Related

C/C++ :: How To Call Member Functions With Template Based Class

Sep 27, 2014

This week we are learning to use templates, and I don't understand how to call my member functions with my template based class. I tried the standard convention of calling member functions, but I keep getting an error saying name following"::" must be a class or namespace name. I'm thinking my problem lies with my typename T, but I am unsure. Line 16 is where I am getting tripped up.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template<class T>
T Set
{
public:

[Code]...

View 2 Replies View Related

C++ :: Error Returning Template Class From Member Function

Apr 30, 2012

When I do this:

// header file:
#include <list>
using namespace std;
class myClass {
list<int> myMethod();
};

// cpp file:

list<int> myClass::myMethod() {
}

In the cpp file, 'myMethod' is underlined in red and when I hover over it, it says:

"std::list<int, std::allocator<int>> myClass::myMethod()

Error: declaration is incompatible with "<error-type> myClass::myMethod()""

But when I make it as a standalone function, outside a class, with no pre-declaration, there is no problem.

View 8 Replies View Related

C++ :: How To Make Class Globally Accessible

Aug 8, 2013

I have a class like this:

file one :

Code:
class X : public Y {
public:
virtual int query(int, int);
// constructor
X(int, int);

[Code] .....

And i construct my M and Nby calling :

Code:
X Y(a,b); and afterwords by calling

Code:
result = Y (c,d) i get my result.

The problem is I need to be able to call result = Y (c,d) from outside my main function and get results but i don't know how to do this. So I want to be able to do something like this.

Code:
#include <iostream>
int g;
void func2(int *h){
*h = g;

[Code] .....

View 7 Replies View Related

C++ :: Private Member Gets Called?

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

C++ :: Access Private Member Variable

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

C/C++ :: Private Member Variable Keeps Being Overridden

Jul 10, 2014

I have a this program of storing students in a 2-3-4 tree. I have a template class called hw6_234tree and another class called Student. Now my private member variable (m_root) keeps being reinitialized every time I process a new line. For example the first student I run through my m_root->A = Student A...(m_root is a private variable of type Node* inside of the 2-3-4tree class).The next pass through now my m_root->A == Student B. So essentially my first run through m_root->A = Student A. Then the next run m_root->A = Student B. My question is how can I keep this variable the same and stop it from reinitializing m_root->A to student B.

Here are a few snips of the code:

Function in main:

void ProcessLine(string line, hw6_234tree<Student>& tree)

Function call:

tree.Add(Student(id, name));

Private Variable:
Node* m_root;

A function for a struct named (Node) in the private section of class hw6_234tree that assigns variables in the struct to values:

Node(T* valueForA)
{
A = valueForA;
B = C = NULL;
L = M1 = M2 = R = NULL;
}

The start of my Add function:

bool Add(T& toCopyAndAdd)
{
if (m_root == NULL)
{
m_root = new Node(&toCopyAndAdd);
return true;
}
return false;
}

View 5 Replies View Related

C++ ::  Friend Function Can't Able To Access Private Member

Jan 21, 2015

Am trying to write table object into file. Here's the source code

.hpp file

class Table {
private:
int table_no;
std::string table_type;
bool engaged;
std::time_t start_time;
double total_sec;

[Code] ....

When i compile the above code i get the following error...

table.hpp: In function ‘std::ifstream& operator>>(std::ifstream&, Table&)’:
table.hpp:19:7: error: ‘int Table::table_no’ is private
table.cpp:91:12: error: within this context
table.hpp:20:15: error: ‘std::string Table::table_type’ is private
table.cpp:92:12: error: within this context ...........

View 4 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

C/C++ :: Object Of Arrays As A Private Data Member?

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

Visual C++ :: Constantly Calling Ostrstream - Private Member Access Violation

Sep 22, 2014

Code:
std::ostrstream oss;
oss << "path for " << unit << "
" << path;
puts(oss.str());

[Code] .....

Today, I just received this new fresh error, I was constantly using them, but just come to know it is a private access violation as the last error of my program. Did I use it in the wrong way?

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++ ::  how To Declare Template Function Inside Template Class

Dec 5, 2013

I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :

my code is something like :

template<typename _Type, std::size_t _Size>
class array {
public :

[Code] ......

but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?

View 6 Replies View Related

C++ :: Use Of Class Template Requires Template Argument List

Nov 6, 2013

Error1error C2955: 'DoubleLinkedListInterface' : use of class template requires template argument listdoublelinkedlist.h10
Error2error C2244: 'DoubleLinkedList<T>::DoubleLinkedList' : unable to match function definition to an existing declaration doublelinkedlist.cpp7

Error3 .cpperror C2244: 'DoubleLinkedList<T>::~DoubleLinkedList' : unable to match function definition to an existing declaration 12

.h

#pragma once
#include "DoubleLinkedListInterface.h"
#include "Node.h"
#include <iostream>

[Code]....

View 4 Replies View Related







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