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


ADVERTISEMENT

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++ :: Defining Member Functions Outside Class Definition

Jan 3, 2014

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class Calc {

[Code] ....

when i built it, it showed the following errors:

1>------ Build started: Project: rough, Configuration: Debug Win32 ------
1> rough.cpp
1>e:c programs
ough
ough
ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier
1>e:c programs

[Code] ....

Need sorting out the errors!!!

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++ :: 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++ ::  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++ :: 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 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++ :: 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++ :: Private Global Variable To Class?

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

C++ :: Create Assignment Operator For A Class That Uses Pointer For Its Private Variable?

Mar 30, 2013

i am trying to create the assignment operator for a class that uses a pointer for it's private variable. The error is saying expected constructor, deconstructor, or type conversion before "operator. (which is the assignment operator. I have tried everything i could think of or find online and nothing has worked. below is the code for the assignment operator in the .h file and the .cpp file.

//Assignment constructor
indexList &operator=(const indexList <T> &rhs);
template <class T>
indexList<T>::indexList operator=(const indexList <T> &rhs) {
if(this != &rhs) {
numberOfElements = rhs.numberOfElements;

[Code]...

View 1 Replies View Related

C++ :: Class Member Variable Initialization?

Dec 18, 2013

Is it possible to initialize class member variables in their definition statement instead of using a constructor?

Is the code bellow correct?

class rectangle
{
float a=0;
float b=0;
public:
string color="Red";
...
};

Which C++ Standard allows it?

View 2 Replies View Related

C/C++ :: Pointing Variable To A Member Function Outside A Class

Apr 13, 2012

In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.

I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????

for example

class A {
public:
int add(int x) {
return x+x;

[Code] .....

View 1 Replies View Related

C/C++ :: Defining Member Function In Different CPP File

Mar 27, 2014

I am work on building a simple parse tree and the layout of my code look like this:

Headers
pt_node.hiterator.hparsetree.h

Source files
node.cppparsetree.cppmain.cpp

I am still relatively new to C++ , and have been advised to include function definition for the member function of both pt_node class and iterator class in the node.cpp file

I particular I have declare the following iterator.h:

inline bool operator ==(const tree_iterator& rhs);

which is defined in node.cpp as:

inline bool tree_iterator::operator==(const tree_iterator& rhs) {
return (node ==rhs.node);
}

However on building I receive the following error:

undefined reference to `dnkmat001::tree_iterator::operator==(dnkmat001::tree_iterator const&)'

Why is this occurring and what measure can I take to fix my code

View 14 Replies View Related

C++ :: Defining A Constant Data Member?

Dec 21, 2012

Programe #1
// file.h
class File {
public:
static const int var = 9;
};

[Code]....

Program#1 is running fine, but program#2 gives linker error:

error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj

I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?

View 3 Replies View Related

C++ :: Store Reference To Const Object In Class As A Member Variable?

May 27, 2014

i want to store reference to a const object in my class as a member variable, as follow:

I basically want a readonly reference to |Data| in Device object.

Code:

class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}

This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!

What is the alternative? Is pointer the nearest? which of the following should be used?

Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????

View 7 Replies View Related

C++ :: Defining Limits Over A Variable?

Oct 6, 2014

In a part of my code I'm defining limits over a variable which is in type of:

fftw_complex *i_phi

Then it is initialized as:

i_phi = (fftw_complex*) fftw_malloc(nx*ny* sizeof(fftw_complex));

The limits are defined as follows.

if ( creal(phi[i]) < 0.0 ) i_phi [i] = 0.0;
if ( creal(phi[i]) > 1.0) i_phi [i] = 1.0;
}

[Code]....

Printed numbers are showing a lot of fluctuation around limits eg, 1.00542, 1.0002 and -2.45829e-12.

I really have no idea why the limits are not applied for a lot of cells but my guess is that it's a problem from the CREAL function. Also I've changed the CREAL with __REAL__ but still the same problem shows up.

View 1 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 :: Using Counter Variable With OpenMP

Oct 14, 2014

I'm trying to write a small brute force application in C on Ubuntu 14.04.1 using Code::Blocks with gcc-4.8.2. It's nothing special and just for practice.The focus is on a parallel procedure that generates strings by use of OpenMP. Every time a string is generated, the counter variable should be incremented. At the end, regardless of whether the search string was matched or not, it outputs the correct number of all generated strings (counts).It works fine so far.Here is my source code example:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
/* Global variables */
const char Literals[] = "abcdefghijklmnopqrstuvwxyz";
const int NLiterals = sizeof( Literals ) - 1; // NLiterals = 26 Bytes.
const char SearchString[] = "test"; // I.e. until 'test' there are 355414 counts.
char MatchString[16];
}

[code]...

This should speed up the process of generating strings. But if I do so, always when the program has a match the number of counted strings is wrong. Here are some possible "wrong" outputs: Code: Match: 'test'! 40710 tries. <-- number of tries (counts) is wrong! or Code: Match: 'test'! 40375 tries. <-- number of tries (counts) is wrong! But if the program doesn't have a match (i.e. when I change SearchString to something like the following line), then the number of counts is correct: Code: const char SearchString[] = "test0"; The only thing I have to do before, is to change the follow line from:

Code:

printf( "
No match! %u tries.
", Count / 8 ); to Code: printf( "
No match! %u tries.
", Count );

But also it can happen that there is no output. Like an infinite or endless loop. How can I improve the source code in order that the correct number of counts is always displayed and what can be the reason for the endless loops time after time? Both issues appears only by the use of Code: #pragma omp for schedule( dynamic ).

View 5 Replies View Related

C/C++ :: Passing Member Functions Member Variable To Another Variable

Aug 31, 2014

So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:

Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()

Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.

#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

[code]....

View 10 Replies View Related

C++ :: Defining Global Variable - Linker Error

Jun 18, 2012

If I DEFINE a global variable in two source files which belong to the same project, then there is a linker error "multiply defined symbols". But if I DEFINE a global variable in two source files which belong to the different projects, then it compiles fine. Why?

View 8 Replies View Related







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