Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to print std::string with printf and this my code. But it does not print the string I assigned.
Header File
#include "cocos2d.h"
#include <iostream>
class Cards : public cocos2d::CCLayer{
public:
virtual bool init();
virtual void load();
std::string TotalCards[52];
}
#include "Cards.h"
bool Cards::init(){
if ( !CCLayer::init() ) {
return false;
}
TotalCards[0] = "ClubsA";
TotalCards[1] = "HeartsB";
TotalCards[2] = "Diamonds4";
return true;
}
void Cards::load(){
printf("Hey I am HERE\n");
for (int i=0 ; i<3; i++) {
printf("CARD NAME %s\n", TotalCards[i].c_str());
}
it prints just
CARD NAME
CARD NAME
CARD NAME
Make sure that Cards::init() is called and returns true before calling Cards::load. The array accessed by Cards::load will consist of three empty strings in case Cards::init() is not called, or if it returns false.
Related
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a task to create function Factory(const std::string name) that returns pointer to function without arguments that prints name. Also I should use only native language methods (without lambda functions and etc). Could you give me example?
Annotations in the code. I deliberately did not make it print to std::cout directly but instead it will return the string. Adapt it as you please.
#include <iostream>
struct bork { // the object to hold the text to return
std::string text; // the text to return
// constructor
bork(const std::string& in) : text(in) {}
// the operator that makes the object behave like a function
std::string operator ()(void) const { return text; }
// a factory method to create a "bork"
static bork make_bork(const std::string& text) {
return bork(text);
}
};
int main() {
auto a = bork::make_bork("howdy");
auto b = bork::make_bork("world");
std::cout << a() << "\n";
std::cout << b() << "\n";
}
You cannot create a function in a function. The only way is to know all the strings there will appear and having a functions for each possible string, and then select and return the proper function. Or else you can use objects :-)
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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
Working on an implementation of the Shunting Yard algorithm and I keep getting this error (In function 'void InputString()': 'inString was not declared in this scope') when trying to compile (what little) code I have - not entirely sure what could be causing it.
#include <iostream>
#include <string>
#include <stack>
#ifndef SHUNTINGYARD_H
#define SHUNTINGYARD_H
class ShuntingYard {
public:
void InputString();
void OutputString();
int precedence(char A, char B);
void ShuntingAlgorithm();
private:
std::string inString;
std::string outString;
std::stack<char> operatorStack;
std::stack<char> tokenStack;
};
#endif // SHUNTINGYARD_H
void InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
I'm sure I'll feel really dumb when I find/someone explains the solution, but I can't figure it out at the moment.
when you do -
void InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
It is only a bare function and it is not a member of the class ShuntingYard.
Hence it is not recognizing "instring"
So make InputString as a member of the class and then define it like below-
void ShuntingYard::InputString() {
std::cout << "Please enter an expression: ";
std::cin >> inString;
}
NOTE: Always write #endif //SHUTTINGYARD_H at the end of the file. It makes your code more standard.
Try adding the class name as well as the :: operator before the function's implementation:
void ShuntingYard::InputString() {
This tells the compiler that you are implementing a function called InputString() of the class ShuntingYard. Otherwise it thinks you are trying to declare a separate function that is separate from ShuntingYard, and thus does not know about its private variables.
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.