Why doesnt this work

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
You need to use different name for the variable and the function.
oh wow, I knew that *he says sarcastically*
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.
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 
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.