C++ :: How To Access Member Variables From Main
Nov 10, 2013
my clsLocalStudent inherits from clsStudent. How to i set my accounts information (accountNumber, accountHolderID . . . . .)?
Code:
#include <iostream>
#include "clsInterest.h"
#include "clsDate.h"
[Code]......
View 3 Replies
ADVERTISEMENT
Apr 16, 2014
I am not able to access the class variable noof_vertex in the function merge , the error is the variable is private . below is the code :
#include <iostream>
#include <ctime>
#include <cstdlib>
[Code]....
View 1 Replies
View Related
Oct 22, 2013
I am trying to return 2 numbers from my function to main(). They are both read in from an input file but it is not working out.
#include <fstream>
#include <iostream>
using namespace std;
ofstream outfile;
void heading(int);
int stuid(int,int);
[Code] ....
View 4 Replies
View Related
Mar 9, 2015
I need making my main function to run while not having any if or for statements. It can only declare variables and functions. Since my main function has command line arguments, how to so.
// This program counts all the words in a given file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE* txtFile = NULL; // File Pointer
char str[1000000];
[Code] ....
View 5 Replies
View Related
Dec 16, 2012
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 = ....
}
View 4 Replies
View Related
Feb 3, 2015
I have the main() - which has entries like
Code:
main() {
int ss, ret, z;
float yy;
ret = function1(&yy, &ss, &z);
//int function1(flat *cc, int *dd, int *k) - is it correct?
[Code]...
View 5 Replies
View Related
Mar 6, 2015
So I need to make a main function have no if/for/etc. statements so I need to move it to another function and call it in main. The problem is that it's a command line argument function so I'm confused on how it works. Here's an example:
Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("The program name %s", argv[0]);
if (argc == 2) {
printf("Argument supplied is %s", argv[1]); }
else if (argc > 2) {
printf("Too many arguments");}
else {
printf("One argument");}
}
How can i make this into two functions with main only declaring variables and calling other functions?
View 2 Replies
View Related
Mar 15, 2013
I am a beginner with C++, taking a class right now. The lab this week is to create a user defined class and have it accesses in a separate .h header file from the main.
I think I'm finding my way through it, but I'm getting a complie error that makes no sense to me:
1> Resistor.cpp
1>c:users omdocumentsschoolcomp 220week 2lablab 2.2lab 2.2
esistor.h(11): error C2146: syntax error : missing ';' before identifier 'resistor'
1>c:users omdocumentsschoolcomp 220week 2lablab 2.2lab 2.2
[Code] .....
Here is the first portion of the .h file:
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>
class CResistor{
double res1, res2, res3, percentage, Nominal, Tolerance;
[Code] ....
View 16 Replies
View Related
Jun 25, 2013
On linux, I can compile DLLs (shared objects) as well as executables that use them. I can even access globals and classes that are defined in the EXE from the DLL, and vice versa, simply with the 'export' keyword. flawlessly.
The Problem: But on Windows (using MinGW), no matter what I do, I'm completely unable to access global variables which defined in the EXE, from the DLL. On Linux, this is no sweat, but what's Windows' problem?
I also need to extend classes in the dll with base class method definitions defined in the exe.
Ive heard that on Windows, you need to use declspec(dllimport) and declspec(dllexport). I can compile with CygWin+MinGW/g++4.5.3 as well as "Pure Windows" with MinGW/g++4.7.2 *without* the declspecs. So what's the decljunk for? Is this really just something for MSVC or other compilers?
Here's some Windows code to show what the problem is. The DLL's global variable is accessible to the EXE just fine, but the EXE's global variable is not accessible to the DLL - compilation complains it is an undefined reference.
main.cpp
#include "myLib.h"
#include <stdio.h>
int exe;
[Code].....
edit: I tried using --enable-runtime-pseudo-reloc --allow-shlib-undefined options when compiling the DLL and G++ complains that --allow-shlib-undefined is an unrecognized option.
View 1 Replies
View Related
Apr 21, 2014
Say I made a vector matrix:
vector<vector<int> > matrix;
and the matrix look likes this:
0,0
0,1
0,2
...
How would I make it get a random piece of this matrix? So for example, how would I get it to access a set of variables like 1,3 like as if it was just an array with stored data in it?
View 3 Replies
View Related
Jan 10, 2015
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 ?
View 4 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
May 19, 2013
I 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 Replies
View Related
Jul 5, 2013
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;
}
View 4 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
Dec 4, 2014
Alright, so I'm making a windows form with a few hundred buttons, and one button will change color based on data the program receives over a socket. This is what I have, and it kind of works, but I don't want to have to make another if statement for all 260 buttons.
void hitmiss(std::string u){
std::string^ ind = reccdata2();
if (u == "button6"){
if (ind == "1"){
this->button6->BackColor = System::Drawing::Color::Red;
this->textBox2->Text = L"hit";
[Code] .....
What I want to do is make it more like
void hitmiss(System::String^ u){
std::string^ ind = reccdata2();
if (ind == "1"){
this->u->BackColor = System::Drawing::Color::Red;
this->textBox2->Text = L"hit";
[Code] ....
But it returns the error that u is not a part of Form1.
View 3 Replies
View Related
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
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
Feb 28, 2014
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 Related
Mar 21, 2012
Code:
class CObjects {
int m_CurrentTime;
int m_Steps;
AStarList* OPEN;
AStarList* CLOSED;
std::vector<AStarNode *>solution;
[code]....
CCB is derived from CrowdEntity and in turn is derived from CObjects Inside CObjects, I declared AStarList *OPEN; Why would howmany become garbage (cdcdcdcd) when I reference it in GetBestNode()
View 9 Replies
View Related
Jul 31, 2013
I have written a class "FileSet " in program's Document class.
This class contains other Four classes as
"Machine1" , "Machine2", "Machine 3" and "Machine 4".
Now my problem is Machine 1 can not access variables from Document class or other variables from FileSet.
How to access them ?
View 9 Replies
View Related
Dec 4, 2014
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. 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] ....
View 1 Replies
View Related
Jan 18, 2013
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?
View 11 Replies
View Related
Feb 18, 2014
I'm struggling with the whole stack/heap thing. I totally see the need to create objects on the free store to outlive functions, however, I don't see the purpose of "new" when working with classes.
Code:
class Person{
public:
Person(std::string strName, int number);
~Person(void);
Resource r;
[Code] .....
Now, what is the difference? Why would I need to create a Resource on the free store? Both variables named r live until the destructor runs, right?
View 1 Replies
View Related
Apr 8, 2015
C++
Create a Triangle class that has the following member variables:
side1 - a double
side2 - a double
side 3 - a double perimeter area
The class should have the following member functions:
- default constructor that sets the value of all 3 sides of a triangle to 0.0
- a constructor with arguments that accepts values for the three sides of a triangle (member variables) as arguments
- setDimensions - a function that allows the value of the three sides to be entered by the user through the keyboard
- testSides - a function that determines if the 3 values entered can actually be the sides of a triangle. If they do not create a triangle, print the values entered and an appropriate message
--The sum of any two side lengths of a triangle must always be greater than the length of the third side: so side 1 + side 2 > side 3 and side 1 + side 3 > side 2 and side 2 + side 3 > side 1 ( all three must be true for the 3 values to make a triangle)
- getSide1 - a function that returns the value of side 1, getSide2 - a function that returns the value of side 2, getSide3 - a function that returns the value of side 3
- getArea - a function that returns the area of a triangle: The formula for the area of a triangle (when the height is not known) is: A = sqrt (p(p-side1)(p-side2)(p-side3)) where p = (side1+side2+side3)/2
- getPerimeter - a function that returns the perimeter of a triangle: Perimeter = side1 + side2+ Side 3
- A displayTriangleInfo function that displays side1, side2, side3, area, and perimeter for a triangle object.
After testing your code for a single object, create an array of 5 triangles. Use a for loop and the setDimensions function to allow the user to set the values for the 3 sides of a triangle, test the vales entered to determine if the 3 create a triangle. If they do create a triangle than use the getArea and getPerimeter functions to calculate the Area and Perimeter for the triangle and use the displayTriangleInfo function to display all of the data for that triangle. If the three values do not create a triangle then print the 3 numbers entered and an appropriate message. In either case the loop should then move on and get the data for the next triangle from the user.
View 7 Replies
View Related
Jun 22, 2013
Suppose I have two classes, MyClassX and MyClassY, each with two member variables, as defined below. I create an object instance of each class, and then create a pointer to each member variable for each object:
Code:
class MyClassX
{
public:
int a;
double b;
MyClassX(int _a, double _b)
[code]....
After converting the hexadecimal to decimal, it appears that with MyClassX, pxb is 8 bytes from pxa, whereas for MyClassY, pya is only 4 bytes from pyb. This makes sense for MyClassY, because the first member variable to be stored is an int, and so will occupy 4 bytes. However, why should this be any different for MyClassX, which also has an int as the first member variable, so shouldn't this also occupy 4bytes?
The reason I have come across this problem is that I am looking into streaming objects to memory and then loading them again. (I know boost can do this, but I am trying it out myself from scratch.) Therefore, this is causing an issue, because I cannot just assume that the size of memory occupied by an object is just the sum of the sizes of its member variables. MyClassX is 8 bytes larger than MyClassY, even though the intuition is that it should only occupy 4 bytes more due to a double being replaced by an int.
View 4 Replies
View Related