-5

I have this code (I put just a part of it):

    class TMTrackAnalyzer : public edm::EDAnalyzer {
    public:
      # declare public stuff here
    private:
      # declare private stuff here
      for(int i=1;i<=10;i++){
        cout<<i;
      }
    };

And I get this error:

expected unqualified-id before 'for'
for(int i=1;i<=10;i++){

I checked that all the brackets are correctly closed before and after the for loop.

What did I do wrong? Thank you!

JohnDoe122
  • 638
  • 9
  • 23
  • 1
    You can't have a `for` loop in a class definition. It belongs in a function body. – François Andrieux Aug 04 '17 at 13:08
  • 1
    You can't just place a `for` loop there. – DimChtz Aug 04 '17 at 13:08
  • why do you not make a method for that loop? – retinotop Aug 04 '17 at 13:08
  • 8
    You can't just run a for loop inside the class. It needs to be in a function. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Aug 04 '17 at 13:08
  • So I should make a method where I call the for loop and call that method in the class definition? – JohnDoe122 Aug 04 '17 at 13:10
  • Run loops inside a class? Interesting! When would you expect it'd be executed? At the implementation of the class? – iBug Aug 04 '17 at 13:11
  • @BillKet **Yes. Definitely.*** – iBug Aug 04 '17 at 13:11
  • 1
    depends. Does it make sense for he function to be a member of the class. Unlike some other languages you can have free standing functions. – NathanOliver Aug 04 '17 at 13:11
  • @BillKet NO! You don't call methods in a header file. – Error - Syntactical Remorse Aug 04 '17 at 13:11
  • 1
    What are you _actually_ trying to achive? At which moment do you want the loop to run? – Jabberwocky Aug 04 '17 at 13:12
  • @BillKet what is this for loop for? in this example it appears to just be printing the value of i, so depending on what you want the for loop to do will depend on where you place it, If for example it was initializing an array of data members it may just be best to place it in the constructor, if it is printing out the values of a list of data members, then it may be best in a function relative to the class, if it is just doing what it is doing in your example, you can put it in an anonymous namespace in the cpp and just call it from a class function... – AresCaelum Aug 04 '17 at 13:15
  • @Eddge I want to declare some variables in that for loop. Something like this for (unsigned int i = 0; i <= 10; i++) { const string tn = tnames[i]; map< ObjectType, TH1F*> hisNumInputStubs_[tn] ;} – JohnDoe122 Aug 04 '17 at 13:49
  • @Eddge If I declare them one by one, it works. I just want the for loop, so that I don't write each of them. – JohnDoe122 Aug 04 '17 at 13:51
  • @AlexG Then how can i declare variables in a for loop? Do I just need to add them by hand? – JohnDoe122 Aug 04 '17 at 13:57
  • Add an array of them? – k_kaz Aug 04 '17 at 14:05

1 Answers1

0

You would benefit greatly from reading the basics of c++ classes. They are extremely powerful but they have rules that need to be followed.

Check out this post to see how header files work and why we use them: Header Files

TMTrackAnalyzer.h:

#include <string>

class TMTrackAnalyzer : public edm::EDAnalyzer {
public:
    //declare public stuff here
    TMTrackAnalyzer(int n) {num = n;} //constructor
private:
    string getPrintString();   
    int num; //data member of TMTrackAnalyzer class
    # declare private stuff here

};

TMTrackAnalyzer.cpp:

#include "TMTrackAnalyzer.h"

string TMTrackAnalyzer::getPrintString()
{
    string temp = "";
    for(int i = 1; i <= num; i++){
      string = string + i + "\n";
    }
    return string
}

Main.cpp:

#include "TMTrackAnalyzer.h"
#include <iostream>
using namespace std;  //not ideal but works for the example
int main()
{
    TMTrackAnalyzer tm(10); //call constructor
    cout << tm.getPrintString();
    return 0;
}
  • Thank you for the idea. However in that for loop I want to declare some class members (the cout<< was just as an example). So can I still do what you suggested in this case? Won't the tm object be initialized, without those private members, that I need to call in other functions, too? – JohnDoe122 Aug 04 '17 at 14:11
  • 1
    @Eddge Nothing is wrong with using it in a regular `.cpp` but main is not just a regular `.cpp`. Something I just recently learned [on this question](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Error - Syntactical Remorse Aug 04 '17 at 14:14
  • @AlexG I've read that before, however I still see no issue with `using namespace` in a cpp file, if you are updating your libraries then those new implemetations would most likely be added to the release notes of the library, and can allow you to take measures before updating to prepare for that. – AresCaelum Aug 04 '17 at 14:17
  • @AlexG yes, they are data members. So how can I declare them in the header. Isn't this what I am doing now? (Sorry I am new to C++) – JohnDoe122 Aug 04 '17 at 14:20
  • 1
    @BillKet a `for` loop is not a data member. A `string` `int` etc is a data member. Ill change the answer. – Error - Syntactical Remorse Aug 04 '17 at 14:21
  • @AlexG ok, I understand. But I still don't understand how can I solve my problem, without a for loop? – JohnDoe122 Aug 04 '17 at 14:36
  • @AlexG sorry I am still confused. Based on what you did, I have to call a function member of the class, in order to declare some private members of the class. Did I get this right? – JohnDoe122 Aug 04 '17 at 14:44
  • @BillKet You really should do a c++ tutorial on classes bill. Mudding your way through this won't help. Correct you need to call the `constructor` to initialize a `private` data member. You could also create `void setNum(n)` to change that value. If this seems hard than you really need to read up on it. These are the basic principles of c++ classes that a beginner in c++ should know. – Error - Syntactical Remorse Aug 04 '17 at 14:49