Returning Strings from class files - ColossalOctopus - Mar 13, 2019
class file
Code #include "Numbertostring.h"
#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std;
Numbertostring::Numbertostring()
{
}
Numbertostring::big(string number) {
number = "hey";
}
Main function
Code #include <iostream>
#include <windows.h>
#include <string.h>
#include "Hey.h"
#include <math.h>
#include "Numbertostring.h"
using namespace std;
int main()
{
Numbertostring bo;
int remainder;
string number,bi,rande;
bo.big(number);
cout << number << endl;
}
In the main function, when I execute cout, the string, number, doesn't have a value although it is being set in that function being called above it. Because of this, I'm guessing the string that bo.big makes doesn't return to the main function because when I put the cout in the big function it works. Is there a way to return a string from a function in a different class file back to the main function?
RE: Returning Strings from class files - halocedark - Mar 14, 2019
(Mar 13, 2019 08:20 PM)ColossalOctopus Wrote: class file
Code #include "Numbertostring.h"
#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std;
Numbertostring::Numbertostring()
{
}
Numbertostring::big(string number) {
number = "hey";
}
Main function
Code #include <iostream>
#include <windows.h>
#include <string.h>
#include "Hey.h"
#include <math.h>
#include "Numbertostring.h"
using namespace std;
int main()
{
Numbertostring bo;
int remainder;
string number,bi,rande;
bo.big(number);
cout << number << endl;
}
In the main function, when I execute cout, the string, number, doesn't have a value although it is being set in that function being called above it. Because of this, I'm guessing the string that bo.big makes doesn't return to the main function because when I put the cout in the big function it works. Is there a way to return a string from a function in a different class file back to the main function?
First and foremost, you need to prototype class functions in the header file, and declare functions contents in a cpp file (which you already did).
Back to your problem, yes you can return a string from a different header file, sinse you included the header file called "Numbertostring.h", now put (#include <iostream>) above (#include <string.h>).
I assume your function big is a return type function which returns a string, i cannot see a return type in your cpp file of the header file (Numbertostring.h).
Your function should return a string type sinse you want to COUT the return value in the main function, you can modify you header class like this:
In Header File:
Code class Numbertostring
{
public:
Numbertostring(); // contructor;
string big(string number);
}
In Cpp File:
Code Numbertostring::Numbertostring()
{
}
string Numbertostring::big(string number) { // i added string so the function return a string type
return number = "hey"; // i added return to return the number value
}
RE: Returning Strings from class files - 0xDEAD10CC - Mar 14, 2019
(Mar 14, 2019 12:10 AM)halocedark Wrote: First and foremost, you need to prototype class functions in the header file, and declare functions contents in a cpp file (which you already did).
Back to your problem, yes you can return a string from a different header file, sinse you included the header file called "Numbertostring.h", now put (#include <iostream>) above (#include <string.h>).
I assume your function big is a return type function which returns a string, i cannot see a return type in your cpp file of the header file (Numbertostring.h).
Your function should return a string type sinse you want to COUT the return value in the main function, you can modify you header class like this:
In Header File:
Code class Numbertostring
{
public:
Numbertostring(); // contructor;
string big(string number);
}
In Cpp File:
Code Numbertostring::Numbertostring()
{
}
string Numbertostring::big(string number) { // i added string so the function return a string type
return number = "hey"; // i added return to return the number value
}
Why would you assign it to a temp to return yet another copy? None of that is proper. You shouldn't be trying to help him out when you don't know what his problem is.
On another note std::string does not come from the <string.h>/<cstring> header (https://en.cppreference.com/w/cpp/string/basic_string). You've basically just thrown a bunch of includes in there at random though.
However, the problem here is you guys don't know the difference between passing by value and passing by reference.
Code #include <iostream>
#include <string>
void func(std::string &str)
{
str = "hey";
}
int main()
{
std::string str;
func(str);
std::cout << str << std::endl;
}
The problem isn't with strings between files, it's a reference and value issue.
|