C++ :: Cannot Dereference Variable In A Set

Jun 29, 2014

I get errors C2100 (illegal indirection) and C2088 ("<<": illegal for class) while trying to use the myPrint function (irrespectively of whether the set for printing contains pointers or not):

template <class T = std::string>
void myPrint(const std::set<T>& mySet) {
std::cout << "Size of vector is " << mySet.size() << ", Pointers: ";
bool pp = false; std::string str = "no";

[Code] ....

The errors point to the line "if (pp) { std::cout << *(*i) << ","; }", but I cannot figure out what's wrong with it...

View 4 Replies


ADVERTISEMENT

C :: Dereference A Pointer In A Function

Feb 25, 2015

Code:
void dereference(int* a, int* b)
{
a=b;
}

int main(int argc, char **argv)

[Code] ....

Why isn't f and d the same after calling "dereference(f,d);"

View 8 Replies View Related

C++ :: Cannot Dereference Iterator At The End Of List?

Apr 8, 2014

Why this code works
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 4 elements

but this not work?
Elenco e1;
e1.add(Persona("a","b"));
e1.add(Persona("c","d"));
e1.add(Persona("e","f"));
e1.remove(2); //list of 3 elements

This is remove method:
Persona Elenco:: remove(int pos){
list<Persona> ::iterator iter=l.begin();
for(int i=0 ;i<pos;i++){
iter++;
}
return *(l.erase(iter)); //erase ritorna un iterator
}

View 4 Replies View Related

C++ :: How To Make Sure Dereference To Vector Is Valid

Oct 6, 2014

I have this piece of code in parts of my path finding algorithm

for( int head; head < q.size(); ++ head ){
walk& w = q[head];

// do manything with w
if( some_condition ) q.push_back( walk( w.x + 1, w.y, head ) );
}

However I notice that sometimes w is cannot be dereferenced. It can but it throws junk number at me. Perhaps the vector is changing it size and move the whole array to a different location. Is there anyway to make sure that w is always valid ?

I just want to use w because of shorter typing and cleaner look not because of performance. I also refrain from using macro.

View 8 Replies View Related

C++ :: System For Managing Radio Station - Crashes When Tries To Dereference The Iterator

Jul 28, 2013

My assignment is to write a system for managing a radio station. The code is composed of four classes:

Song: each song has a name, a composer and a singer, and has a few segments: INTRO,VERSE,CHORUS,TRANSITION, while each represents a different length string of chars.
Playlist: a multiset of songs, and a pointer to a RadioStatistics instance (see below).
RadioStation: a set of Songs (will represent the station database), and a list of Playlists, each playlist holds a few songs from the database.
RadioStatistics: can only be instantiate once, this object gather statistics; it has two maps: one that counts how many times a song was played, and second that counts how many times each singer was played. (the key is the song/singer name, and the value is the counter).

The RadioStation has a constant that defines a limit to how many times a song is allowed to be played. whenever a song reaches this limit (meaning, it was played too much), the program needs to skip to the next song in the database.

so, I run this test from main, and the program crashes (or more accuratly get stuck, since the console stays open and the program keeps working until I stop it).

I made a few changes and run the debugger a few times, and was able to focus on the problem.

Song.h:
Code:
#ifndef SONG_H_
#define SONG_H_
#include<iostream>
#include<string>

[Code] ....

I ran a step by step debugger, and found out the problem lays with line 90 in RadioManager.cpp, when the while loop runs its fourth iteration. It crashes when it tries to dereference the iterator, while it points to the fourth playlist in the list.

And here's some more weird stuff: when I comment out line 73 in main.cpp - it works perfectly fine! (line 73 in particular! commenting out any other line in main.cpp didn't worked around the bug!)

View 13 Replies View Related

C++ :: Type Conversion - Float Or Double Variable Into Unsigned Char Variable And Back

May 10, 2013

I would like to convert a float or double variable into unsigned char variable and back.

float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);

I am not getting the same value back.. why?

View 2 Replies View Related

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

C++ :: Storing Variable Size Pointers Array As Class Variable

Mar 2, 2013

This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.

#include <iostream>
using namespace std;
class A {
public:
int ** pointer;

[Code] ....

I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.

View 6 Replies View Related

C++ :: Write A Loop Assigning Variable X To All Positions Of String Variable

Sep 8, 2013

I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.

"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.

char our_string[15] = "Hi there!";

(Please note this is a 'C-string', not C++ standard string.)"

View 5 Replies View Related

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 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/C++ :: Assign One Variable Address To Another Variable

Feb 3, 2014

I've been experimenting with pointers and am getting the below error.

'error: cannot convert 'int**' to 'int*' in assignment'

I thought it was ok to assign a variable address to another variable. Line 18 is where I get the error.

I am trying to show the progression of memory as I increment it as I have done on line 17 and again, I don't know why I don't see a progression through memory locations when output to the console on line 20.

Here's the code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {

int *tt = new int;
int *p = new int;

[Code] .....

View 6 Replies View Related

Visual C++ :: Creating A Variable Using A Variable?

Jun 16, 2013

Any way to create a variable using a variable in the name? So E.g. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?

View 10 Replies View Related

C/C++ :: Change Enum Type Variable To String Type Variable?

Aug 10, 2014

How to change an enum type variable to a string type variable?

View 2 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 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 :: Set Max For A Variable?

Nov 28, 2013

Is it possible to increment a number but have it print out at a maximum value I assign if it reaches that number? For example, if I have 99 tickets total and only 5 of those tickets are still available, and a person requests 10, how can I give them the 5 available and make sure it doesn't go over the max (99)?

Code:

if (strcmp(word2, "RAFFLE") == 0) {
fscanf(ifp, "%d", &purchased_raffle_tix[p]); //scans purchase
fscanf(ifp, "%d", &person[q]); //scans purchaser
num_raffle_sold += purchased_raffle_tix[p];
available_tix = 99 - last_raffle;
if (available_tix >= 1)

[Code] ....

It would out put with the number of the tickets that the purchaser received:

RAFFLE TICKETS 0 - 29 given to PERSON 22
RAFFLE TICKETS 30 - 69 given to PERSON 209
RAFFLE TICKETS 70 - 95 given to PERSON 123
RAFFLE TICKETS 96 - 99 given to PERSON 211
NO RAFFLE TICKETS given to PERSON 27

View 5 Replies View Related

C++ :: Why Can't Access The Variable

Jul 17, 2014

Code:
#include <iostream>
using namespace std;
void f();
extern int x;
int main() {

[Code] .....

x is declared outside the functions and defined inside main(). Then why this code produces a compile error?

x is already declared so it can be used in f(); and when I call f(), x is already defined. Then why can't f() sets the value of x (in main) to 10?

View 3 Replies View Related

C++ :: How To Check Which Variable Has No Value

Nov 12, 2013

I am writing a console program for a class. I have satisfied the assignment, but I want to clear up what is mostly a cosmetic problem. The program prints a form to the console and places the cursor at a location on the form where the user inputs data. The problem occurs when the user presses the enter key without entering data. The cursor goes to the beginning of the next line. If the user enters data after this, the program functions correctly. I want to know how I can reposition the cursor if the user enters no data.

This is the code that reads one of the values:

Code:

void getHousing(HANDLE screen, MonthlyBudget &inputBudget) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
while (!validateEntry(screen, inputBudget.housing)) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cout << SEVEN_SPACES << endl;
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
}
}

validateEntry checks that the entered value is >= 0 SEVEN_SPACES is a string of seven spaces to cover up the previous entry.

View 3 Replies View Related

C :: Declare That A Variable Will Be Used Later?

Mar 6, 2015

I remember that the syntax exists I just don't remember what it is.I want to write a conditional function () prior to main (). in that function I'm using a variable that isn't introduced until main (). How do I let C know that I will be int'ng this function prior to the point where it is going to be accessed so that it doesn't freak out? alternatively is it possible to int the variable in both main() and my conditional function so that it exists? I imagine this would cause an issue because I would essentially be int'ng it twice.

I've done the cursory google searches however I don't think I'm using the correct keywords to get the results I'm looking for. If necessary I would be more than happy to post the code it's just a simple 40 line homework assignment.

View 6 Replies View Related

C :: Variable In While Loop

Feb 6, 2013

I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.

Code:

#include <stdio.h>
int main(void)
{
int sum = 0, temp = 1;
}

[code]...

View 4 Replies View Related

C :: Variable F Is Being Used Without Being Initialized

May 6, 2013

I have declared and initialized where needed but keep getting a Debug Error: Variable F is being used without being initialized.

Code:

#include "stdafx.h"
#include<stdio.h>
void Signboard (void)
{
/* This function prints the Signboard onto the output page */
int n;
}

[code]....

View 4 Replies View Related

C++ :: Variable Mean Is Being Used Without Being Initialized

Jan 27, 2013

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
int studentdetails_mean();
double standard_deviation();

[Code] .....

its showing Run-Time Check Failure #3 - The variable 'mean' is being used without being initialized.

View 11 Replies View Related

C++ :: Variable Not Storing The Value?

Feb 5, 2014

So i'm creating bank system. Where I have function that reads the text file (balance), and I make a deposit, which adds what was in the balance = deposit + balance.

But this is what is happening. In my text file i have number 10. Function balance works fine and reads 10 from the text file.

When I make a deposit add a value to the balance, if i add 7, the new balance is 17. And i check the text file shows me is 17. Which is correct.

The problem is here. If i make another deposit, without closing the program, for example i add 5 to the balance, the new balance should be 22 = 17(balance) + 5(deposit), because 17 was store on the text file and it was the balance that was store on the text file on the last time. But it shows me 15, it adds the balance that the program first started which was 10(balance) + 5(deposit), but should had had be 17 + 5.

When I close the program the value on the text file, is 15, that was the last sum that i did.

int main()
{
double balance = 0;
balance = currentBalance(balance);
menu(balance);

[Code].....

View 18 Replies View Related

C++ :: What Happens When Reassign Variable

Apr 16, 2013

If I have an instance MyClass Object, already declared and initialized, and I write over it, Object = // different MyClass object , does the deconstructor get called on the first instance before assigning the second instance to Object?

Somewhat related (but more general), if I have a statically allocated object, what is the syntax for manually calling a deconstructor? I've never seen it before; is this even possible?

View 2 Replies View Related

C++ :: Variable Xxx Is Not A Type Name

Feb 8, 2015

I'm attempting to pass a couple of variables over to my Item.cpp class, that is description and item_price. However under item.set(description,item_price), i get three errors. One under the . (period) saying : expected an identifier and two more under description and item_price stating that variable " xxx " is not a type name.

Main.cpp

#include <iostream>
#include "item.h"
using namespace std;
using namespace items;
int main(){
int n;

[Code] .....

View 11 Replies View Related







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