Why doesnt this work

Feb 23, 2019 at 10:52pm
Error is std::string test = test(); where test(); has the error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>;
#include <string>;

int main();
std::string test();

int main() {
	std::string test = test();
}

std::string test() {
	std::string Life = "hi";
	return Life;
}
Last edited on Feb 23, 2019 at 10:52pm
Feb 23, 2019 at 10:54pm
You need to use different name for the variable and the function.
Feb 23, 2019 at 10:57pm
oh wow, I knew that *he says sarcastically*
Feb 24, 2019 at 12:41am
there are times when you can use the sane name, but its extremely difficult to follow code written that way, and if careless, you can also get unintentional recursion where it compiles, but goes nuts.
Feb 24, 2019 at 10:15am
Well, what I said is technically a lie. You can use the same name but then you need to use a qualified function call.

 
std::string test = ::test(); // OK 
Feb 24, 2019 at 11:22am
What's this a prototype for the main function?? int main();
You don't need that unless you want to call the main function from another function, and if you do want to call the main function from another function, the world will end (quite literally, so don't do that it will only cause confusion).
Topic archived. No new replies allowed.