C++ :: How To Access A Specific Member If Keys Are Same
May 19, 2013I understand what a map is and all, but what purpose does multimap serve?How can you access a specific member if the keys are the same?
View 8 RepliesI understand what a map is and all, but what purpose does multimap serve?How can you access a specific member if the keys are the same?
View 8 RepliesI 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.
Code:
class A
{
std::map<std::string, Unit*> aMap;
class B
[Code] .....
This code snippet results in "A non-static member reference must be made relative to a specific object". When I make callA() static, this error goes away, but there is problem with aMap.
In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.
void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}
I have the following scenario :
Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}
I need to access mem_ptr (pointer to a class member) through :
a. object (t1)
b. pointer to the object (ptr).
How can i do that ?
my clsLocalStudent inherits from clsStudent. How to i set my accounts information (accountNumber, accountHolderID . . . . .)?
Code:
#include <iostream>
#include "clsInterest.h"
#include "clsDate.h"
[Code]......
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.
I have some doubt regarding class data member accessing in another file.Follwing code showing error.
class A://file a.cpp
{
public:
int add;
int sub;
};
//file b.cpp
extern class A
void cal()
{
A::add=A::sub;
}
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?
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?
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 ...........
Suppose I make a class, something like having the constructor being invoked first makes sense, I don't have a problem with that. But, how could I limit access to functions until certain functions are called? Perhaps this isn't built into the language so you can't. And maybe this problem never comes up. For example if you have a set() and get() functions, if they are both public functions, there doesn't seem to be a way for the compiler at least now if set() never gets called you shouldn't call get(). I just see this as error prone if you need to use libraries, you have to know not to do it from documentation instead of something the compiler can check.
View 11 Replies View RelatedI need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem. My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members. I understand that compiler should know that this class instances will exist during all program execution. I have tried to set static member class Foo instance in another class , but this didn't solve the problem. How to correctly implement signal handling in such case.
Here is example of my code
class MyContainer {
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] ....
I have a question regarding composition and accessing members "deep" inside the composed structure. For example;
class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};
void myInt(int newInt) {this->m_myInt = newInt;};
[Code] ....
Now, from somwhere I have access to an object of type B where I want to update the A::m_myInt. How would you do this without "breaking" the whole purpose of private/public members?
B myB;
myB.m_a.myInt(3); // Not allowed, and not desireable
I thought about implementing access through functons kind of like;
A & B::a() {return this->m_a;};
myB.a().myInt(3);
but I'm worried that this exposes my B::m_a-object too much. This would allow
myB.a() = A();
, right?
The following is a more desireable way of acces, but doesn't work for updating;
A const & B::a() {return this->m_a;};
myB.a().myInt(3); //Disallowed? myInt(int) is non-const.
What about this? Is this a good way of doing it?
class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};
[Code] ....
I guess it works? It would lead to a lot of data shuffling in case of larger sub-components.I would really like to do the following without exposing my components so much:
B myB;
myB.a().myInt(3);
Can it be done?
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] ....
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....
I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
What is correct approach to do this. How to correctly implement signal handling in such case.
Here is example of my code
Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] .....
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?
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].....
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 RelatedHow do I make a specific character show up a specific amount of times?
Like I am generating a random number then I need to make "|" show up that many times on the screen.
how can I allow pressing some keys on the keyboard in C++? I mean, when you are in the console and you are only allowed to press numbers before pressing enter, for example.
View 6 Replies View RelatedI found this method
Code:
TCHAR win_getch()
(below) on a website which is used for printing the characters as the keys on keyboard are hit i-e without hitting the ENTER key. This method is used in a while loop like this
Code:
while ((c = win_getch()) != 13)
{}
I wanted to know why is the character compared to
Code:
13
i-e
Code:
if((c = win_getch()) != 13) then do something
Code:
/**
* This function will simulate the getch() function from conio.h.
* Note that this does not work for special keys like F1-F12, arrow keys,
* even the ESC key does not seem to work with this function.
* @return TCHAR read from input buffer.
*/
TCHAR win_getch() {
DWORD mode;
TCHAR theChar = 0;
[code]....
My program uses a menu that easily facilitates a person to navigate trough out it but im stuck on a part and I cant go to menu3 with the arrows (only use c code not c++)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
void sleep(long int interval) {
for(int i=0;i<interval*1000;i++);}
[Code]...
I need a container which has multiple keys. The number of keys, which are related to a value, is not constant, but it is small. (less than 10). I have tried to do this with std::map, but things are working only with a constant number of keys per value.
Something like this
key1 ----|
key2 ----|--------- value1
key3 ----|
key4 ----|--------- value2
key5 ----|
I was developing an exercise program for myself (note: not a project in school) to enhance my programming skills in C++ then I encountered a "logic bug" (that's how I call it :D ) I think. I googled stuffs I need to know and ended up to post my question here.
Here's the scene:
1. When using GetAsyncKeyState I can capture Function Keys [F1-F12, etc..]
2. While doing and having fun to my exercise program, I suddenly noticed that whatever key I pressed from the keyboard {example: asdfkj], it keeps the keys entered and brings to a "field" that accepts user input.
What you should do...
1. Press any key (example: asdfisdjfisdjff) then proceed to press F1. And see what happens. I don't know how to erase what I have entered before pressing F1.
here's the code.
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void gotoxy();
void gotoxy( int column, int line ) {
[code]....