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.
Related
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
}
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
This is the class.
#include <iostream>
#include <string>
std::string strName = "ABC";
class BlueOut
{
public:
void printName() { std::cout << strName << std::endl; }
};
Now i create a object of this class
BlueOut blueout;
And i call the function printName() of the object in lambda
auto a = [&]() { blueout.printName(); };
But the function does not gets executed.
In this line,
auto a = [&]() { blueout.printName(); };
the part [&]() { blueout.printName(); } is called a lambda expression. You bind it to some variable a. Now you have a function object a created by a lambda expression. In order to see the effect, this has to be invoked:
a();
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 4 years ago.
Improve this question
My question is actually regarding already asked question. I have tried the answer given by #r3mus n0x also have seen some SO questions which did not help me to get a clear idea about the above situation.
In the given post lacks MCVE, therefore I have tried a bit and came up with the following code and with the same error what #user10213044 mentioned in his/her post.
Error msg
error C2065: 'm_func': undeclared identifier
My qestion:
Q1: Can we really store the pointer to some of the member functions of a class(like in the following example) into it's on private member(ex. vector array)? If so what is the reason for the above error msg?
Q2: I have also tried to write inside the for loop:
classFuncPtr fun = bindClassPtr->m_func; // compiles
fun(str); // error
gave me: Error msg
error: must use '.*' or '->*' to call pointer-to-member function in 'fun (...)', e.g. '(... ->* fun) (...)'
fun(str); // error
which I could not understand. Can anybody tell me what went wrong in this case?
The second attempt was similar to the following case which we use for normal functions pointer case.
typedef void(*FuncPtr)(const std::string&);
FuncPtr Lambda = [](const std::string& str) { std::cout << str << std::endl; };
Lambda(std::string("Hellow World"));
Here is the code I tried:
#include <iostream>
#include <vector>
#include <string>
#include <memory>
class MyClass;
typedef void (MyClass::*classFuncPtr)(const std::string&); // function ptr to MyClass::member functions
struct MyBind // bind struct
{
classFuncPtr m_func;
explicit MyBind(const classFuncPtr& func): m_func(std::move(func)) {}
};
class MyClass
{
std::string m_var;
std::vector<std::unique_ptr<MyBind>> my_binds_;
public:
MyClass() // constructor
{
my_binds_.emplace_back( std::make_unique<MyBind>( std::move(&MyClass::do_this) ));
my_binds_.emplace_back( std::make_unique<MyBind>( std::move(&MyClass::do_that) ));
}
// two functions to bind
void do_this (const std::string& str) { std::cout << "From do this: " << str << std::endl; }
void do_that (const std::string& str) { std::cout << "From do that: " << str << std::endl; };
void handle_input(const std::string& str)
{
for (const std::unique_ptr<MyBind>& bindClassPtr: my_binds_)
{
// how to print passed string str here?? (Q1)
(bindClassPtr->*m_func)(str);
/*
classFuncPtr fun = bindClassPtr->m_func; // compiles (Q2)
fun(str); // error
*/
}
}
};
Your first attempt fails because there's no variable in scope named m_func.
Your second attempt fails because a pointer-to-member requires an object to be called on.
The correct syntax is:
classFuncPtr fun = bindClassPtr->m_func;
(this->*fun)(str);
Live Demo
The pointer contained in your MyBind objects isn't actually bound to anything. It's a pointer to a member of MyClass, so you have to provide it an instance of MyClass to work on.
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
I've been searching for a while on why this does not work. I have not gotten a clear answer.
Can anyone explain why trying to access this boolean variable and comparing it to another boolean variable won't work?
I also tried setting the rhs of the comparison to 0, and that got rid of the boolean/int error, but I'm still getting the error.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
setWorking(true);
}
//Mutator
void setWorking(bool x) { working = x; }
//Accessor
bool getWorking() { return working; }
private:
bool working;
};
int main() {
MyClass alpha;
if (alpha.getWorking == true) {
cout << "its working\n";
}
else {
cout << "not working\n";
}
return 0;
}
In main function
if (alpha.getWorking == true)
should be
if (alpha.getWorking())
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 7 years ago.
Improve this question
class skClass {
public:
void setName(string x) {
name = x;
}
string getName() {
return name;
}
private:
string name;
};
int main() {
skClass sk;
sk.setName = ("Mr Bashir Sentongo");
cout << sk.getName() << endl;
return 0;
}
Methods are called by passing parameters within parenthesis (()), not with the assignment operator (=):
I.e., you should replace
sk.setName = ("Mr Bashir Sentongo");
With
sk.setName("Mr Bashir Sentongo");