c++ beginner

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 ;
}





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
@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
Yeah,thanks a lot.
Topic archived. No new replies allowed.