Poll of the Day > Jamie is bad at coding #50 (c++ pointers)

Topic List
Page List: 1
Yellow
07/08/17 11:05:51 PM
#1:


Hey so I'm working with C++, and I'm pretty awkward with the pointers.

I need a tuple<string, double>, except the double needs to reference another double.

Right now I have
tuple<std::string, double&>

in the declaration, and

AddExternalValue(std::string str, double &value)
{
...
some new tuple = std::make_tuple(str, value)
...
}


Except it's not taking the make_tuple part. There's probably a really simple answer to this for anyone with a basic understanding of pointers.

@Sahuagin
@helly
@TheWorstPoster
@other coders I'm not thinking about
---
... Copied to Clipboard!
TheWorstPoster
07/08/17 11:07:14 PM
#2:


Sorry.

Don't know C++
... Copied to Clipboard!
Zeus
07/08/17 11:08:16 PM
#3:


tbh, I wanted to learn C++ but it wasn't being offered during either semester I could have taken it.
---
(\/)(\/)|-|
In Zeus We Trust: All Others Pay Cash
... Copied to Clipboard!
Yellow
07/08/17 11:12:01 PM
#4:


TheWorstPoster posted...
Sorry.

Don't know C++

I figured you wouldn't because you're new to it all.

Zeus posted...
tbh, I wanted to learn C++ but it wasn't being offered during either semester I could have taken it.

It's a more advanced language, I don't think they let you take it until you've done a lot of prerequisites. I like it, but I'd still avoid it if I had any other choice. I really prefer C#
---
... Copied to Clipboard!
Masamune_DS
07/08/17 11:19:42 PM
#5:


It's been a while since I've done C++.

http://www.cplusplus.com/reference/tuple/make_tuple/

Going by this example, have you used the "auto" keyword?
---
If a wizard turns me into Dawn then I would sooo touch myself every single night. ~ SoulGainDestiny
... Copied to Clipboard!
Yellow
07/08/17 11:27:44 PM
#6:


Masamune_DS posted...
It's been a while since I've done C++.

http://www.cplusplus.com/reference/tuple/make_tuple/

Going by this example, have you used the "auto" keyword?

Don't think that would make a difference. All auto does is auto complete some declaration code for you, it's not really making a difference.

Actually, my tuple is in a vector array, so I need to set it with

external_values.push_back(std::make_tuple(str, value));

But that's harder to read and mostly irrelevant to the problem.
---
... Copied to Clipboard!
Yellow
07/08/17 11:42:22 PM
#7:


I figured it out! Hooray!

vector<tuple<std::string, double*>> external_values;

...

bool MathParser::AddExternalValue(std::string str, double* value)
{
...
some new tuple = std::make_tuple(str, value)

(actually written as "external_values.push_back(std::make_tuple(str, value));")
...
}


Then I can change the value with this

*std::get<1>(external_values[GetExternalValueIndex(variable)]) = value;

Now isn't that a beautiful piece of code.
---
... Copied to Clipboard!
Sahuagin
07/08/17 11:45:21 PM
#8:


I don't know modern C++ all that well, though I know "classic" C++ pretty well

what error are you getting?

Masamune_DS posted...
http://www.cplusplus.com/reference/tuple/make_tuple/

it sounds like make_tuple should infer the type automatically, so if you're having a type problem, then the types you're giving it don't match the types you're expecting to get.

notice that they use std::ref to convert an int into an int reference. you may have to do that if you have a double and not a double reference. (references are denoted with a & symbol)
---
... Copied to Clipboard!
Sahuagin
07/08/17 11:48:20 PM
#9:


Yellow posted...
I figured it out! Hooray!

drat. 8)
---
... Copied to Clipboard!
Yellow
07/08/17 11:52:40 PM
#10:


I had to declare my tuple as
tuple<std::string, double*>

Then I had to give it the pointer to the tuple with

double* val_ref = &value;
some tuple = std::make_tuple(str, val_ref);

The problem was this is actually my first time using pointers in c++ so I was just fumbling around, but having figured that out I feel like I "get it" now.

@Sahuagin posted...
Yellow posted...
I figured it out! Hooray!

drat. 8)

Yeah sorry. :)
---
... Copied to Clipboard!
green dragon
07/08/17 11:57:30 PM
#11:


Yellow posted...
I'm pretty awkward

I've noticed
... Copied to Clipboard!
helly
07/08/17 11:58:38 PM
#12:


why was I tagged

i dont know Jack about coding c+
---
Vote to keep the ability to hide the tabs on the Xbox One Dashboard
https://xbox.uservoice.com/forums/363186--new-ideas/suggestions/19542517
... Copied to Clipboard!
Yellow
07/09/17 12:03:20 AM
#13:


green dragon posted...
Yellow posted...
I'm pretty awkward

I've noticed

What gave it away

helly posted...
why was I tagged

i dont know Jack about coding c+

I thought you did for some reason.
---
... Copied to Clipboard!
Sahuagin
07/09/17 12:25:23 AM
#14:


Yellow posted...
The problem was this is actually my first time using pointers in c++ so I was just fumbling around, but having figured that out I feel like I "get it" now.

pointers are pretty tricky and dangerous. they hold literal memory addresses which "point at" objects of the particular type. the * notation takes a memory address and dereferences it (reads from the address). you can do this with arbitrary values, which means you can read (or write to) arbitrary memory. which can end your program pretty quickly.

this is a great Q/A about the kinds of things you can but shouldn't do with them:
https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/
---
... Copied to Clipboard!
Topic List
Page List: 1