C++ :: Store A Reference Variable As Member Variable Of Interface Object

May 1, 2013

I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.

LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).

I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip

I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.

View 11 Replies


ADVERTISEMENT

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++ :: Undefined Reference Error When Accessing Static Variable Inside Member Function

Feb 10, 2013

I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,

utilities.h
-----------
class utilities {
private:
static int num_nodes;

public:
void parse_details(char* );

[Code] ....

I get a compilation error in the function void utilities::parse_details(char* filename)

which says: undefined reference to `utilities::num_nodes'

compiler: g++

View 2 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++ :: Store Function To A Variable And Call It Using That Variable?

Oct 15, 2013

I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like

struct item {
int ID;
int special; // for function
};

item Key;
Key.special = UseKey(KEY_KING);

// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this

if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;

View 5 Replies View Related

C++ :: Non-static Member Reference Must Be Made Relative To A Specific Object

Mar 4, 2012

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.

View 2 Replies View Related

C :: How To Store First String Into A Variable

Feb 11, 2013

Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.

Code:

#include <strings.h>
#include <stdio.h>
int main () {
//Declared variables
int i;
}

[code]....

View 3 Replies View Related

C++ :: How To Reference Static Variable In Another File

Apr 15, 2014

I've got a static variable in the master file called :

static dtNavMesh *g_navMesh;

I just need one copy of this object.

In another module, I need to reference this global variable.

extern dtNavMesh *g_navMesh;

View 5 Replies View Related

C++ :: Assigning Reference Variable To Another Of Same Type

Jun 4, 2012

What is wrong with assigning a reference variable to another reference variable of the same type? I guess I have not understood references very well.

1- In below code, the initialization list gets error because agent "object reference variable" cannot be initialized with a reference variable of the same type.

Code:
class Intention {
public:
Intention(Agent& agent,int Id, string name);

[Code] ....

In other places I have the same problem. In below code the assignment gets error (no overloaded function for assigning a reference to another reference?)

Code:
void Agent::setWorld(World& world) {
this->world = world;
}

//Note: this->world has a type of World&

2- In this other one, I want to assign reference of a vector member to a reference variable of the same type.

Code:
vector<Intention> intentions;
...
Intention& currentIntention = intentions[intentionsIterator]

View 6 Replies View Related

C++ :: How To Pull Out A Value In String And Store In Variable

Oct 7, 2014

Like if the user enters "38 F" how do I take out the 38 only and store it in a variable? So far my program goes like this:

string text;
double temperature;

cout << "Enter temperature: ";
getline(cin, text); // user enters 38 F
temperature = text // store 38 from string into double

View 1 Replies View Related

C++ :: Pointer - Pass Reference Variable Into Function

Oct 4, 2013

I don't understand how my code not run.

#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;

[Code] ....

View 3 Replies View Related

C++ :: Why Return Reference Doesn't Alter The Other Variable

Jun 1, 2014

Suppose i have a function:

int & f1(int & p) {
return p;
}
void main() {
int a,b;
b = 10;
a = f1(b);
a = 11;
cout<<b<<endl;
}

why when i change a, b doesnt change? a is supposed to be an alias of b right?

View 5 Replies View Related

C++ :: How To Store Variable In Memory When Program Is Closed

Nov 3, 2013

How do you store a variable in memory so that it isn't changed when the program closes? I don't have any experience with this and am just wondering how it is possible. I am creating a program and want it to store your preferences and scores. In a simple program, everything is just reset and I don't want this for my program. How do I store a variable so that it stays the same, but can be changed even when the program is turned off?

View 2 Replies View Related

C++ :: Store 100th Fibonacci Number Into A Variable?

Feb 24, 2013

Is it even possible to store the 100th Fibonacci number (beginning with the numbers 1 and 1) into a variable? I know the number is pretty huge, and wondered if there is a data type to hold a number that big.

View 6 Replies View Related

C++ :: How To Store Entire Content Of File In Variable

Jan 28, 2013

I would like to store the entire content of a file in a single c-string variable or in a standard string class variable. Is there a function that will do this for me? I am familiar with functions that get one character or one line at a time but I don't think I've encountered a function that gets the entire file. Does this function keep or disregard the end-of-line character? If no such function exists, I would write my own function to create and return such a variable.

View 4 Replies View Related

C/C++ :: Store Initial Size Of List In Int Variable?

Nov 24, 2014

I've been trying to store the inital size of a list in an int variable, so I can access it later in case I modify the list size. For example, I did the following:

std::list<AType *> myList;
myList.push_back(anATypeobject);
int initListSize = myList.size(); //initial list size
myList.push_back(anotherATypeobject);
myList.push_back(yetanotherATypeobject);
while(myList.size > initListSize)
myList.pop_back();

What this is supposed to do is to get an initial size of a list, and then be able to return to that initial size. However, when I try to do it in my code, initListSize always change if myList.size() changes. Is there a way to change that?

View 4 Replies View Related

C++ :: How To Read The Integer Member Variable

Oct 16, 2014

this program is not giving to chance to enter the ooplevel value.

Code:

#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the

[Code]...

View 3 Replies View Related

C++ :: Static Variable In Member Function

Aug 27, 2014

I need to keep a static variable in a member function of a class that I have many objects of. I've had some trouble with it, and when I read up I found that such variables are static across all instances. Is there any way around this?

View 3 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++ :: 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++ :: Member Variable Aliasing A Function?

Dec 26, 2014

So, one can do stuff like this using #defines:

#include <iostream>
#include <array>
#define x arr[0]
#define y arr[1]
#define z arr[2]
class Point {

[code]....

... that is, to be able to reference the same data by "member variables" as by referencing a stl container. But defines are the devil's work - adding in a "#define x arr[0]" is a dangerous statement. I'd really like some nice clean C++ method (C++11 or C++14 are just fine) to do this without defines, but so far I'm drawing a blank. If arr wasn't an STL container, if we just wanted a pointer-based array, I could do it this way:

class Point
{
...
float x __attribute__ ((aligned (sizeof(float))));
float y __attribute__ ((aligned (sizeof(float))));
float z __attribute__ ((aligned (sizeof(float))));
float*const arr = &x;
};

... but you obviously can't do that if arr is an STL container.

The best I've come up with is to make x, y, and z function pointers, but then you can't call them like p.x, you have to call them like *p.x(), it's not very clean and I'd expect some added overhead. One could go even uglier and make x, y, and z be instances of some custom class with overridden operators that reference arr[], but that seems like it'd be just getting ridiculous in terms of overhead (both coding and performance)

View 4 Replies View Related

C++ :: Static Constant Member Variable

Jun 6, 2013

What is the problem with the following code is? It compiles with Visual C++ 2012 but does not with g++:

//a.h

#ifndef Loaded
#define Loaded
using namespace std;
class MyClass{
public:
static const int MyStaticValue = 200;

[Code] ....

If I try to compile this using the command

g++ a.cpp b.cpp

I get an "undefined reference to 'MyClass::MyStaticValue'" error for the line "A = MyClass::MyStaticValue;" in main(). The strange thing is that if I change the line to "A = (int) MyClass::MyStaticValue;" it works fine and the output is

200
200

as expected.

The code also compiles under g++ if I move the defintion of MyStaticValue from a.h to a.cpp by const int MyClass::MyStaticValue = 200;

View 5 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++ :: Aliasing Member Variable Names?

Oct 15, 2014

I have been playing about with Vertices and Colors and have ended up with this:

template < std::size_t SIZE, typename T >
struct Pack
{

[Code].....

Is there anyway to make an alias for Pack's data field? Something that would allow me to write:

Color3f my_color;
my_color.red = 0.1f;

View 3 Replies View Related

C++ :: Take Values From Text File And Store Them In Variable And Use For Later Calculation

Nov 8, 2013

I have a text file in the form

5502 5202.3
1523 2536.1
1254 1256.2
17846 8956.2 and so on

left one is time and right one is pressure, I need to show the time value as it is but i need to use the pressure value to calculate air speed. I don't know how to use the strings to pick up the list properly. I did the calculation formula for the air speed but i can't pick the pressure value up from the text file. here's what i did:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code].....

View 3 Replies View Related

C++ :: Read Input From User And Then Store In Structure Variable

Apr 6, 2014

My program is designed to read input from the user and then store that input in a structure variable. Whenever i use the cin.getline() function, it comes up with a semantic issue. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DESCRIPTION_SIZE = 100, DATE_SIZE = 20;
struct inventory {
string description[DESCRIPTION_SIZE];

[Code] .....

I left out the other functions. The error reads "no matching member function for call to 'getline'.

View 6 Replies View Related







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