Error: Function not defined

Hey all,

I am a very beginner with ROOT and C++, so my question may be quite easy…
For homework I have to write an easy macro with “cin” and “cout”.
My problem is, that ROOT always tells me, that my Function ist not defined. As I got two examples from my prof. that bring the same problem, I don´t really know, what to do…
Thanks in advance!!

Here is the code:

#include <iostream.h>
using std::cin;
gRoot->Reset;


void Frage();

int Frage() {
        int ntimes;
        std::cout << "Enter ntimes:";
        std::cin >> ntimes;
    };

int main()
{
    int i = 0;
    while (i < ntimes) {
        cout << "Hello World" << endl;
        i++;
    };
}
Error: Function blatt1_aufgabe2.C() is not defined in current scope 
*** Interpreter error recovered ***

a) You want to include iostream instead of iosteam.h (the .h header is an ancient non-standard header): #include <iostream>
b) you define a void Frage() and an int Frage() - you cannot overload on return types!
b1) your int Frage() is missing a return statement
c) in main, there is no variable called ntimes
d) why gRoot->Reset; (why at all, why without parentheses?)
e) seems more a fundamental C++ problem than a ROOT problem!
Finally: which version of ROOT are you using? When I try to compile your code, I get:

root_forum.C:1:10: fatal error: 'iostream.h' file not found
#include <iostream.h>
         ^~~~~~~~~~~~

At first, thanks a lot for your reply!!

a) Perfect, changed!!
b) That´s quite clear, I just played around and forgot to change it again…
c) How do I have to define ntimes, so that main() gets the value from Frage()?
Is it just like :

int Frage() {
        int ntimes;
        std::cout << "Enter ntimes:";
        std::cin >> ntimes;
        return ntimes;
    };

int main()
{
    int i = 0;
    int ntimes;
    while (i < ntimes) {
        cout << "Hello World" << endl;
        i++;
    };
    return 0;
}

d) We were told to “always use this at the beginnning of a macro”, so I just did copy-and-paste…
e) I am using version 5.34, I think its the most current version for windows…

Hey again,

I took some time and was able to get my programm running in a c++ shell…
But nervertheless Root gives me an error:

**Error: Function ist not defined in current scope (0)**
***** Interpreter error recovered *****

I am using verion 5.34/36 on windows. Please give me a hint what I am doing wrong. I thought, normal c++ code should be working in root?!?
My code is:

#include <iostream>
using std::cin;
using std::cout;

struct Frage {
    int ntimes;
    Frage() {
        std::cout << "Enter ntimes: ";
        std::cin >> ntimes;
    }
};

Frage f;
int main()
{
    int i = 0;
    while (i < f.ntimes) {
        std::cout << "Hello World" << "";
        i++;
    };    
}

You’ve named your file ist.C, right? For such a file, ROOT expects to find a function ist() that it will call when you invoke .x ist.C or root -l -b -q ist.C. I.e. please rename your function currently called main() and you should be all set!

Thanks a lot, Alex!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.