Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this class definition:
class event {
public:
wstring type;
int pos;
int sen;
event(const string &t) : type(util::string2wstring(t)) {}
~event() {};
};
and the global variable:
list<event> events;
This variable was initialize in one function and after that I want to modify differents values of this events like:
for(auto ei : events) {
ei.pos = (*w).get_position();
ei.sen = sen;
cout << "pos: " << ei.pos << " in sentence " << ei.sen << endl;
++w;
++sen;
}
event ei = events.front();
cout << "pos2: " << ei.pos << " in sentence2 " << ei.sen << endl;
Then the first cout print the correct values of the new pos and sen but the second (outside the for scope) print the oldest values.
If I print the values in other function the oldest values are printed.
I think that the problem is no reference access to the global variable but is it true?
And the most important, how can I fix it?
Thanks for your time,
Regards.
The problem is that ei in your range-for loop is a value and not a reference. That means it is a copy of the element in the container, and modifying a copy of course does not modify the original.
To use references you need to specify it:
for(auto& ei : events) { ... }
// ^
// Note ampersand here
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
After reading this answer I decided to try it. To my surprise, the following code is working, and the reference is correctly reseated. Why's that?
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 1;
int& ref{a};
ref = b;
cout << ref << endl;
return 0;
}
The reference ref itself was not reset. It still references the variable a. This statement
ref = b;
changed the value of the referenced variable a. To be sure insert this statement
std::cout << "a = " << a << '\n';
You can consider references as an aliases for variables they refer.
A reference is just an alias to the variable it refers to. Once a reference has been bound, it cannot be rebound. Anything you do to a reference afterwards actually happens instead to the variable it refers to. If you read from a reference, it reads from the variable instead. If you assign a value to a reference, it assigns the value to the variable instead. If you take the address of a reference, it takes the address of the variable instead.
So, these statements:
ref = b;
cout << ref << endl;
are really just doing this instead:
a = b;
cout << a << endl;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have an array initialised like the following:
int example[5][5];
example[5][5] = 55;
And a function:
void example_function(auto inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}
And I am calling it into the function like this:
example_function(example, 5);
As you can see, I have the parameter for the function as auto when it is really using an integer array.
When I use typeid(table).name() to get the type of the array example, it outputs the type as A5_A5_i where the fives are from the initialisation (e.g. int example[3][4][5] would output A3_A4_A5_i)
When using typeid(table).name() on inArray after changing the type of the parameter from int to auto, I get the type name as PA5_i which is different to the one mentioned above.
How can I get a suitable type for a parameter in my function, and is there a better way to do this
If the array passed to the function is known beforehand, in this case as int example[5][5];, you can use the following,
void example_function(int (&inArray)[5][5], int z){
cout << "example text = " << inArray[z][z] << endl;
}
Here we take the array by reference to avoid array decay.
If the size might vary at runtime, use std::vector.
std::vector<std::vector<int>> example;
void example_function(std::vector<std::vector<int>> inArray, int z){
cout << "example text = " << inArray[z][z] << endl;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Im just starting to learn C++. While writing a simple calculator, I found that when calling a void function, it doesn't print when it should. I have simplified the code to better represent my problem.
#include <iostream>
using namespace std;
void helloguys()
{
cout << "test";
}
int main()
{
cout << "This is a ";
void helloguys();
cout << " guys.";
}
I expected to get "This is a test guys.", but all I got is "This is a guys."
The compiler never reported any kind of problems.
Try removing void before your function call:
int main()
{
cout << "This is a ";
helloguys();
cout << " guys.";
}
helloguys(); will call the function.
void helloguys(); is a function prototype, not a call.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
so I have a C++ bool function that I've written that looks like this:
bool EligibileForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
bool eligible = false;
if (CompanyUsed==CompanySubscribed)
eligible = true;
return (eligible);
}
Now in my main() this function is called as the only parameter for an if statement:
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << "\nAfter your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ".\n";
}
The main function was written by my teacher and we wrote the other functions, so this if statement isn't supposed to be changed.
So I understand what the if statement is trying to accomplish, by basically saying "if (true) do this..." since the EligibleForDiscount will return a boolean value.
However, g++ is giving me an error with the if statement, telling me that EligibleForDiscount is not declared in this scope.
But I'm not trying to use it as a value but as a call to a function.
It may be because of two reasons:
You misspelled the function name when called : if (EligibleForDiscount(CompanyUsed, CompanySubscribed)) should be written like your implementation of the function, which is EligibileForDiscount.
This can happen if you forgot to declare the prototype of the function, which is an indicator to the program that you're going to use that function. You simply need to write somewhere before you use the function bool EligibileForDiscount(const char , const char)
One of these should work!
Because : EligibileForDiscount != EligibleForDiscount with an additional "i", just a typo.
p.s. you can write EligibleForDiscount like this:
bool EligibleForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
return CompanyUsed==CompanySubscribed;
}