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 :-)
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 15 days ago.
Improve this question
I am trying to create multiple files according to a filename in cpp. Using ofstream for that, I could not achieve it for now.
I'd appreciate if anyone can help me with that.
I am writing down here:
static std::ofstream text1;
static std::ofstream text2;
class trial{
public:
if(situation == true) {
document_type = text1;
}
if(situation == false) {
document_type = text2;
}
document_type << "hello world" << "\n";
}
ofstream object as variable.
Assignment copies the objects, and it's not possible to create copies of streams. You can only have reference to streams, and you can't reassign references.
Instead I suggest you pass a reference to the wanted stream to the trial constructor instead, and store the reference in the object:
struct trial
{
trial(std::ostream& output)
: output_{ output }
{
}
void function()
{
output_ << "Hello!\n";
}
std::ostream& output_;
};
int main()
{
bool condition = ...; // TODO: Actual condition
trial trial_object(condition ? text1 : text2);
trial_object.function();
}
Also note that I use plain std::ostream in the class, which allows you to use any output stream, not only files.
You can't use statements at class scope, only declarations.
In any case, you need to use a reference variable for what you are attempting, eg:
std::ofstream& document_type = situation ? text1 : text2;
document_type << "hello world" << "\n";
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 5 years ago.
Improve this question
I have an issue on operator overload in C++, Consider the following class
CharacterString.h file
class CharacterString {
private:
char* __charString;
public:
CharacterString();
~CharacterString();
void operator =(const char* sFileName);
};
//CharactorString.cpp file
CharacterString::CharacterString() { }
CharacterString::~CharacterString() { }
void CharacterString::operator=(const char* sFileName)
{
this->__charString = (char *)sFileName;
}
In the main function, the following code works fine.
CharacterString fileName;
fileName = "Hello, World";
However, the bellow code causing shows compiler error
CharacterString* fileName;
fileName = new CharacterString();
fileName = "Hello, World";
printf("%s", fileName);
enter image description here
If you declare fileName as a pointer:
CharacterString* fileName;
Then if you want to access the object, you must first dereference it by using * or -> so:
*fileName = "Hello, World";
fileName->operator=("Hello, World");
The printf() call is completely incorrect and it works only by accident (because CharacterString object is put on the stack in the same binary format as char *). You should add a getter method for string:
class CharacterString {
private:
char* __charString;
public:
CharacterString();
~CharacterString();
char *getString() { return __charString; }
void operator =(const char* sFileName);
};
Then you can write:
printf("%s", fileName->getString());
Try to make e.g. ~CharacterString() virtual to change the binary representation of the object and immediately it stops working without getString().
Last (off topic), please note that symbols starting with underscores are reserved and should not be used in "user code".
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 9 years ago.
Improve this question
I am familiar with creating classes and using dot notations to access functions for the objects. I am unfamiliar with the notation of doing the same but with pointers. Please take a look at my code and suggest how I change the code in the class definition, not main.
main:
Pet * p = new Pet( "Sunset", "Cat", "meow..." );
cout << "The " << p->getVariety( ) << " " << p->getName( ) << " speaks by saying " << p->spea( ) << endl;
class:
class Pet
{
public:
Pet( );
Pet( std::string name, std::string variety, std::string says )
{
using namespace std;
myName = name;
myVariety = variety;
howIspeak = says;
}
std::string getName( ) const
{
return myName;
};
std::string getVariety( ) const
{
return myVariety;
};
virtual void speak( );
protected:
std::string myName;
std::string myVariety;
std::string howIspeak;
};
I guess, it's the
virtual void speak( );
in
cout << "The " << "..." << p->speak( ) << endl;
which causes the error message. You cannot output a void value to std::ostream.
Presumably, it should have been
virtual std::string speak();
Please take a look at my code and suggest how I change the code in the class definition, not main.
The using namespace std; is useless in there, just remove it.
Then, you are having virtual methods and protected members, which suggests that you are willing to inherit from that class: either don't or define a virtual destructor as well.
Also, the speak member function you are calling from the main, returns void, which is not a type you want to pass to operator<< of std::cout.
And finally: why are you using nake pointers and dynamic allocation? Don't use it unless you are absolutely forced to, and even then, use std::shared_ptr or std::unique_ptr (or any other of the smart pointers family) instead:
std::unique_ptr<Pet> ptr(new Pet( "Sunset", "Cat", "meow..." ));
After that line, ptr will behave almost like any other pointer, except it will clean himself up (will call delete) when you are done with it (when it leaves the scope).
I would suggest reading an introductory book on C++, "C++ Primer (4th Edition)" by Lippmann et al. or finding an on-line tutorial on C++. The problem you are solving requires more understanding than can be related in an SO Q and A.
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.