Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When I declare anything inside an if statement it doesn't propagate out of it, moreover, if I have a variable outside, and i redeclare it inside if statements it lost it once the code ends the statement, how can I manage to globalize the scope of an if statement.
Redeclaring a variable in an inner scope creates a new variable.
#include <iostream>
int main()
{
int i = 1;
if (true)
{
int i = 42; // variable is re-declared
} // lifetime of inner i ends here
std::cout << i; // expected output 1
}
You can reference a variable in an inner scope that was declared outside without re-declaring it.
#include <iostream>
int main()
{
int i = 1;
if (true)
{
i = 42; // variable from outer scope is used
}
std::cout << i; // expected output 42
}
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 1 year ago.
Improve this question
Why can't the print() function access the msg variable?
#include <iostream>
void print()
{
std::cout << msg << std::endl;
}
int main()
{
std::string msg{"Hello"};
print();
}
Error: 'msg' was not declared in this scope
A C++ program always begins execution at the main () function.
In the main function therefore, we declare a variable named msg of type string and use it as an argument to call the print () function. Immediately this happens, the code 'steps out' into the print function and the string variable is printed out to console.
void print(std::string some_msg){
std::cout <<some_msg << std::endl;
}
int main()
{
std::string msg{"Hello"};
print(msg);
}
I hope this clarifies.
This is because the msg variable that you have declared in the main function is a local variable and can be accessed only within the main function.
You can either define a global variable so that you can access it from any function, or you can pass msg as a parameter to the print function.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have created function and call above the main() function. It is successfully call the function in GCC compiler on Linux platform. I don't understand, how main function call my own function.
#include <iostream>
using namespace std;
int myFunc();
int ret = myFunc();
int main()
{
cout << ret << endl;
}
int myFunc()
{
int i = 10, j = 20, k;
k = i+j;
return k;
}
Global variables are initialized before main is called. Therefore the call to myFunc happens before main is called. Your main function doesn't call myFunc at all.
It would have been very obvious if you used a debugger and set breakpoints in the myFunc and main functions, and looking at the call stack.
As Some programmer dude explained, it is being called before the main function.
To not be confused, I suggest that you explicitly call the myFunc() in the main function:
#include <iostream>
using namespace std;
int myFunc();
int main()
{
int ret = myFunc();
cout << ret << endl;
}
int myFunc()
{
int i = 10;
int j = 20;
return i+j;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
struct naming
{
int numline;
string numname;
} naming = {{1,"ONE"},{2,"TWO"}};
cout<<naming.numline<<":"<<naming.numname<<std::endl;
return 0;
}
This error occurs:
main.cpp:10:33: error: braces around scalar initializer for type int
} naming = {{1,"ONE"},{2,"TWO"}};
You have
struct naming { … } naming = …
which means you're creating a single naming object. But your initializer
{{1,"ONE"},{2,"TWO"}}
doesn't match that intent.
Looks like you're trying to initialize a collection of naming objects. If that's the case you should make it a std::vector<naming> instead of a single object:
struct naming { … }; // definition of naming
std::vector<naming> namings = {{1, "ONE"}, {2, "TWO"}}; // collection of objects
Then you can access the individual naming objects like so:
// access the first element:
std::cout << namings.at(0).numline << ":" << namings.at(0).numname << std::endl;
// access the second element:
std::cout << namings.at(1).numline << ":" << namings.at(1).numname << std::endl;
Since you want to store two values , you will have to create an array of structure type.
#include <iostream>
using namespace std;
int main()
{
struct naming
{
int numline;
string numname;
} naming[] = {{1,"ONE"},{2,"TWO"}};
cout<<naming[0].numline<<":"<<naming[0].numname<<std::endl;
cout<<naming[1].numline<<":"<<naming[1].numname<<std::endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there some way I can have a global variable (in this case a vector) retain its contents throughout any functions? I'm trying to see if I can do this:
vector<string> collected_input; //global
void some_function{
string bla = "towel";
collected_input.push_back(bla); //collected_input gains "towel"
}
void some_otherfunction{
string xyz = "zyx"
collected_input.push_back(xyz); //collected_input gains "zyx"
}
int main(){
// print the contents of the collected_input vector
}
What you have shown will work just fine, provided main() is calling some_function() and some_otherfunction():
#include <ostream>
#include <vector>
#include <string>
using namespace std;
vector<string> collected_input;
void some_function()
{
string bla = "towel";
collected_input.push_back(bla);
}
void some_otherfunction()
{
string xyz = "zyx"
collected_input.push_back(xyz);
}
int main()
{
some_function();
some_otherfunction();
for (vector<string>::iterator iter = collected_input.begin();
iter != collected_input.end();
++iter)
{
cout << *iter << '\n';
}
return 0;
}
The code you posted will achieve what you are looking for. Your have a single instance of a vector (collected_input), which is used across multiple functions. Your vector is effectively global, and in fact it is possible for other source files to access it by declaring a vector of the same name using the extern keyword, although this is highly recommended against.
Of course, right now your program does nothing because your main() function does not contain any code. If you were to call both of your functions from main() and then print the vector, you will find that both functions successfully operated on the vector.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I started with working code (not written by me obviously), I saw them make a class object dataflash which made sense to me, but then after that they used:
DataFlash::ID id;
Which is obviously because they needed an object of that struct, but the fact they went back to the DataFlash class bugged me, not sure why, but I thought "No, no, you should be using the object we just made now" and promptly changed it to what I have below, which produces the following error:
error: invalid use of ‘struct main()::DataFlash::ID’
Well that's no fair, I'm basically doing the same thing to my eye, why is this invalid use? Are structs (and nested classes I assume too) useless once they are in an object?
#include <iostream>
using namespace std;
int main()
{
class DataFlash
{
public:
struct ID
{
uint8_t manufacturer; /**< Manufacturer id **/
uint8_t device[2]; /**< Device id **/
uint8_t extendedInfoLength; /**< Extended device information**/
};
};
DataFlash dataflash;
dataflash.ID id;
return 0;
}
The code defines several layers of nested scope, the original code is doing scope resolution with the scope resolution operator ::
Your example uses a member access operator . and the struct ID simply is not a member, it lives in the scope of your type, a type and value is not the same thing.
As a footnote, enumerations can be accessed in both ways due to enum's scoping rules.
#include <iostream>
struct object {
enum identifier {
a = 2,
b = 16,
};
identifier id;
};
int main(int argc, char* argv[]) {
object obj;
std::cout << object::a << std::endl;
std::cout << obj.b << std::endl;
}