Jan 4, 2019 at 2:34pm UTC
Hello I keep having the same problem. Every time i run the program, it keeps poping out :"function-definition is not allowed before token".
#include <string>
#include <iostream>
using namespace std;
void computeFeatures(string);
int main()
{
string text="C++ is fun";
void computeFeatures(string text)
{
cout<<endl<<"String: "<<text<<endl;
}
cout<<"Size: "<<text.size();
cout<<"Capacity: "<<text.capacity();
cout<<"Empty?: "<<text.empty()<<endl;
computeFeatures(text);
text+="for everyone";
computeFeatures(text);
text="C++ Fun";
computeFeatures(text);
text.clear();
computeFeatures(text);
return 0 ;
}
Jan 4, 2019 at 2:40pm UTC
You can't put functions inside of functions.
But C++ does have lambdas which looks a lot like functions that can be defined inside a function, but I think you should avoid them when you can to keep your sanity, and to prevent yourself from misusing them.
This isn't necessary in this case, but consider formatting
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on Jan 4, 2019 at 2:41pm UTC
Jan 4, 2019 at 4:05pm UTC
@dena1992
What you were wanting, is this, I assume..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <string>
#include <iostream>
using namespace std;
void computeFeatures(string);
int main()
{
string text="C++ is fun" ;
computeFeatures(text);
text+=" for everyone" ;
computeFeatures(text);
text="C++ Fun" ;
computeFeatures(text);
text.clear();
computeFeatures(text);
return 0 ;
}
void computeFeatures(string text)
{
cout<<endl<<"String: " <<text<<endl;
cout<<"Size: " <<text.size()<<endl;
cout<<"Capacity: " <<text.capacity()<<endl;
cout<<"Empty?: " <<text.empty()<<endl;
}
Last edited on Jan 4, 2019 at 4:08pm UTC